support more boards
parent
b9415cc798
commit
1104708583
@ -0,0 +1,553 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef ARX_RINGBUFFER_H
|
||||
#define ARX_RINGBUFFER_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#if defined(ARDUINO_ARCH_AVR)\
|
||||
|| defined(ARDUINO_ARCH_MEGAAVR)\
|
||||
|| defined(ARDUINO_ARCH_SAM)\
|
||||
|| defined(ARDUINO_ARCH_SAMD)\
|
||||
|| defined(ARDUINO_spresense_ast)
|
||||
#define ARX_CONTAINER_DISABLED
|
||||
#endif
|
||||
|
||||
#ifdef ARX_CONTAINER_DISABLED
|
||||
|
||||
#ifndef ARX_VECTOR_DEFAULT_SIZE
|
||||
#define ARX_VECTOR_DEFAULT_SIZE 16
|
||||
#endif // ARX_VECTOR_DEFAULT_SIZE
|
||||
|
||||
#ifndef ARX_DEQUE_DEFAULT_SIZE
|
||||
#define ARX_DEQUE_DEFAULT_SIZE 16
|
||||
#endif // ARX_DEQUE_DEFAULT_SIZE
|
||||
|
||||
#ifndef ARX_MAP_DEFAULT_SIZE
|
||||
#define ARX_MAP_DEFAULT_SIZE 16
|
||||
#endif // ARX_MAP_DEFAULT_SIZE
|
||||
|
||||
#ifndef ARX_TYPE_TRAITS_INITIALIZER_LIST_DEFINED
|
||||
#define ARX_TYPE_TRAITS_INITIALIZER_LIST_DEFINED
|
||||
namespace std
|
||||
{
|
||||
template<class T>
|
||||
class initializer_list
|
||||
{
|
||||
private:
|
||||
const T* array;
|
||||
size_t len;
|
||||
initializer_list(const T* a, size_t l) : array(a), len(l) {}
|
||||
public:
|
||||
initializer_list() : array(nullptr), len(0) {}
|
||||
size_t size() const { return len; }
|
||||
const T *begin() const { return array; }
|
||||
const T *end() const { return array + len; }
|
||||
};
|
||||
}
|
||||
#endif // ARX_TYPE_TRAITS_INITIALIZER_LIST_DEFINED
|
||||
|
||||
namespace arx {
|
||||
|
||||
namespace container
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
template<class T>
|
||||
T&& move(T& t){ return static_cast<T&&>(t); }
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T, size_t N>
|
||||
class RingBuffer
|
||||
{
|
||||
protected:
|
||||
|
||||
T queue_[N];
|
||||
int head_ {0};
|
||||
int tail_ {0};
|
||||
|
||||
public:
|
||||
|
||||
RingBuffer()
|
||||
: queue_()
|
||||
, head_ {0}
|
||||
, tail_{0}
|
||||
{
|
||||
}
|
||||
|
||||
RingBuffer(std::initializer_list<T> lst)
|
||||
: queue_()
|
||||
, head_(0)
|
||||
, tail_(0)
|
||||
{
|
||||
for (auto it = lst.begin() ; it != lst.end() ; ++it)
|
||||
{
|
||||
queue_[tail_] = *it;
|
||||
++tail_;
|
||||
}
|
||||
}
|
||||
|
||||
// copy
|
||||
explicit RingBuffer(const RingBuffer& r)
|
||||
: queue_()
|
||||
, head_(r.head_)
|
||||
, tail_(r.tail_)
|
||||
{
|
||||
for (size_t i = 0; i < r.size(); ++i)
|
||||
queue_[i] = r.queue_[i];
|
||||
}
|
||||
RingBuffer& operator= (const RingBuffer& r)
|
||||
{
|
||||
head_ = r.head_;
|
||||
tail_ = r.tail_;
|
||||
for (size_t i = 0; i < r.size(); ++i)
|
||||
queue_[i] = r.queue_[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
// move
|
||||
RingBuffer(RingBuffer&& r)
|
||||
{
|
||||
head_ = container::detail::move(r.head_);
|
||||
tail_ = container::detail::move(r.tail_);
|
||||
for (size_t i = 0; i < r.size(); ++i)
|
||||
queue_[i] = container::detail::move(r.queue_[i]);
|
||||
}
|
||||
|
||||
RingBuffer& operator= (RingBuffer&& r)
|
||||
{
|
||||
head_ = container::detail::move(r.head_);
|
||||
tail_ = container::detail::move(r.tail_);
|
||||
for (size_t i = 0; i < r.size(); ++i)
|
||||
queue_[i] = container::detail::move(r.queue_[i]);
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ~RingBuffer() {}
|
||||
|
||||
using iterator = T*;
|
||||
using const_iterator = const T*;
|
||||
|
||||
size_t capacity() const { return N; };
|
||||
size_t size() const { return tail_ - head_; }
|
||||
inline const T* data() const { return &(get(head_)); }
|
||||
T* data() { return &(get(head_)); }
|
||||
bool empty() const { return tail_ == head_; }
|
||||
void clear() { head_ = 0; tail_ = 0; }
|
||||
|
||||
void pop() { pop_front(); }
|
||||
void pop_front()
|
||||
{
|
||||
if (size() == 0) return;
|
||||
if (size() == 1) clear();
|
||||
else head_++;
|
||||
}
|
||||
void pop_back()
|
||||
{
|
||||
if (size() == 0) return;
|
||||
if (size() == 1) clear();
|
||||
else tail_--;
|
||||
}
|
||||
|
||||
void push(const T& data) { push_back(data); }
|
||||
void push(T&& data) { push_back(data); }
|
||||
void push_back(const T& data)
|
||||
{
|
||||
get(tail_++) = data;
|
||||
if (size() > N) ++head_;
|
||||
};
|
||||
void push_back(T&& data)
|
||||
{
|
||||
get(tail_++) = data;
|
||||
if (size() > N) ++head_;
|
||||
};
|
||||
void push_front(const T& data)
|
||||
{
|
||||
get(head_--) = data;
|
||||
if (size() > N) --tail_;
|
||||
};
|
||||
void push_front(T&& data)
|
||||
{
|
||||
get(head_--) = data;
|
||||
if (size() > N) --tail_;
|
||||
};
|
||||
void emplace(const T& data) { push(data); }
|
||||
void emplace(T&& data) { push(data); }
|
||||
void emplace_back(const T& data) { push_back(data); }
|
||||
void emplace_back(T&& data) { push_back(data); }
|
||||
|
||||
const T& front() const { return get(head_); };
|
||||
T& front() { return get(head_); };
|
||||
|
||||
const T& back() const { return get(tail_ - 1); }
|
||||
T& back() { return get(tail_ - 1); }
|
||||
|
||||
const T& operator[] (size_t index) const { return get(head_ + (int)index); }
|
||||
T& operator[] (size_t index) { return get(head_ + (int)index); }
|
||||
|
||||
iterator begin() { return ptr(head_); }
|
||||
iterator end() { return (queue_ + tail_); }
|
||||
|
||||
const_iterator begin() const { return (const_iterator)ptr(head_); }
|
||||
const_iterator end() const { return (const_iterator)(queue_ + tail_); }
|
||||
|
||||
iterator erase(iterator p)
|
||||
{
|
||||
if (p == end()) return p;
|
||||
for (T* pos = p + 1; pos != end(); ++pos)
|
||||
*(pos - 1) = *pos;
|
||||
--tail_;
|
||||
return p;
|
||||
}
|
||||
|
||||
void resize(size_t sz)
|
||||
{
|
||||
size_t s = size();
|
||||
if (sz > size())
|
||||
{
|
||||
for (size_t i = 0; i < sz - s; ++i) push(T());
|
||||
}
|
||||
else if (sz < size())
|
||||
{
|
||||
for (size_t i = 0; i < s - sz; ++i) pop();
|
||||
}
|
||||
}
|
||||
|
||||
void assign(const_iterator first, const_iterator end)
|
||||
{
|
||||
clear();
|
||||
const char* p = first;
|
||||
while (p != end) push(*p++);
|
||||
}
|
||||
|
||||
void shrink_to_fit()
|
||||
{
|
||||
// dummy
|
||||
}
|
||||
|
||||
void reserve(size_t n)
|
||||
{
|
||||
(void)n;
|
||||
// dummy
|
||||
}
|
||||
|
||||
void insert(const_iterator pos, const_iterator first, const_iterator last)
|
||||
{
|
||||
if (pos != end())
|
||||
{
|
||||
size_t sz = 0;
|
||||
{
|
||||
iterator it = (iterator)first;
|
||||
for (; it != last; ++it) ++sz;
|
||||
}
|
||||
iterator it = end() + sz - 1;
|
||||
for (int i = sz; i > 0; --i, --it)
|
||||
{
|
||||
*it = *(it - sz);
|
||||
}
|
||||
it = (iterator)pos;
|
||||
for (size_t i = 0; i < sz; ++i)
|
||||
{
|
||||
*it = *(first + i);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
iterator it = (iterator)first;
|
||||
for (; it != last; ++it)
|
||||
{
|
||||
push_back(*it);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
T& get(const int i)
|
||||
{
|
||||
if (i >= 0) return queue_[i % N];
|
||||
else return queue_[N - abs(i % N)];
|
||||
}
|
||||
|
||||
const T& get(const int i) const
|
||||
{
|
||||
if (i >= 0) return queue_[i % N];
|
||||
else return queue_[N - abs(i % N)];
|
||||
}
|
||||
|
||||
T* ptr(const int i)
|
||||
{
|
||||
if (i >= 0) return (T*)(queue_ + i % N);
|
||||
else return (T*)(queue_ + N - abs(i % N));
|
||||
}
|
||||
|
||||
const T* ptr(const int i) const
|
||||
{
|
||||
if (i >= 0) return (T*)(queue_ + i % N);
|
||||
else return (T*)(queue_ + N - abs(i % N));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template <typename T, size_t N>
|
||||
bool operator== (const RingBuffer<T, N>& x, const RingBuffer<T, N>& y)
|
||||
{
|
||||
if (x.size() != y.size()) return false;
|
||||
for (size_t i = 0; i < x.size(); ++i)
|
||||
if (x[i] != y[i]) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, size_t N>
|
||||
bool operator!= (const RingBuffer<T, N>& x, const RingBuffer<T, N>& y)
|
||||
{
|
||||
return !(x == y);
|
||||
}
|
||||
|
||||
|
||||
template <typename T, size_t N = ARX_VECTOR_DEFAULT_SIZE>
|
||||
struct vector : public RingBuffer<T, N>
|
||||
{
|
||||
vector() : RingBuffer<T, N>() { }
|
||||
vector(std::initializer_list<T> lst) : RingBuffer<T, N>(lst) { }
|
||||
|
||||
// copy
|
||||
vector(const vector& r) : RingBuffer<T, N>(r) { }
|
||||
|
||||
vector& operator= (const vector& r)
|
||||
{
|
||||
RingBuffer<T, N>::operator=(r);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// move
|
||||
vector(vector&& r) : RingBuffer<T, N>(r) { }
|
||||
|
||||
vector& operator= (vector&& r)
|
||||
{
|
||||
RingBuffer<T, N>::operator=(r);
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ~vector() {}
|
||||
|
||||
private:
|
||||
using RingBuffer<T, N>::pop;
|
||||
using RingBuffer<T, N>::pop_front;
|
||||
using RingBuffer<T, N>::push;
|
||||
using RingBuffer<T, N>::push_front;
|
||||
using RingBuffer<T, N>::emplace;
|
||||
};
|
||||
|
||||
|
||||
template <typename T, size_t N = ARX_DEQUE_DEFAULT_SIZE>
|
||||
struct deque : public RingBuffer<T, N>
|
||||
{
|
||||
deque() : RingBuffer<T, N>() { }
|
||||
deque(std::initializer_list<T> lst) : RingBuffer<T, N>(lst) { }
|
||||
|
||||
// copy
|
||||
deque(const deque& r) : RingBuffer<T, N>(r) { }
|
||||
|
||||
deque& operator= (const deque& r)
|
||||
{
|
||||
RingBuffer<T, N>::operator=(r);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// move
|
||||
deque(deque&& r) : RingBuffer<T, N>(r) { }
|
||||
|
||||
deque& operator= (deque&& r)
|
||||
{
|
||||
RingBuffer<T, N>::operator=(r);
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ~deque() {}
|
||||
|
||||
private:
|
||||
using RingBuffer<T, N>::capacity;
|
||||
using RingBuffer<T, N>::pop;
|
||||
using RingBuffer<T, N>::push;
|
||||
using RingBuffer<T, N>::emplace;
|
||||
using RingBuffer<T, N>::assign;
|
||||
using RingBuffer<T, N>::begin;
|
||||
using RingBuffer<T, N>::end;
|
||||
};
|
||||
|
||||
|
||||
template <class T1, class T2>
|
||||
struct pair
|
||||
{
|
||||
T1 first;
|
||||
T2 second;
|
||||
};
|
||||
|
||||
template <class T1, class T2>
|
||||
pair<T1, T2> make_pair(const T1& t1, const T2& t2)
|
||||
{
|
||||
return {t1, t2};
|
||||
};
|
||||
|
||||
template <typename T1, typename T2>
|
||||
bool operator== (const pair<T1, T2>& x, const pair<T1, T2>& y)
|
||||
{
|
||||
return (x.first == y.first) && (x.second == y.second);
|
||||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
bool operator!= (const pair<T1, T2>& x, const pair<T1, T2>& y)
|
||||
{
|
||||
return !(x == y);
|
||||
}
|
||||
|
||||
|
||||
template <class Key, class T, size_t N = ARX_MAP_DEFAULT_SIZE>
|
||||
struct map : public RingBuffer<pair<Key, T>, N>
|
||||
{
|
||||
using iterator = typename RingBuffer<pair<Key, T>, N>::iterator;
|
||||
using const_iterator = typename RingBuffer<pair<Key, T>, N>::const_iterator;
|
||||
|
||||
map() : RingBuffer<pair<Key, T>, N>() { }
|
||||
map(std::initializer_list<pair<Key, T>> lst) : RingBuffer<pair<Key, T>, N>(lst) { }
|
||||
|
||||
// copy
|
||||
map(const map& r) : RingBuffer<pair<Key, T>, N>(r) { }
|
||||
|
||||
map& operator= (const map& r)
|
||||
{
|
||||
RingBuffer<pair<Key, T>, N>::operator=(r);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// move
|
||||
map(map&& r) : RingBuffer<T, N>(r) { }
|
||||
|
||||
map& operator= (map&& r)
|
||||
{
|
||||
RingBuffer<pair<Key, T>, N>::operator=(r);
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ~map() {}
|
||||
|
||||
const_iterator find(const Key& key) const
|
||||
{
|
||||
for (size_t i = 0; i < this->size(); ++i)
|
||||
{
|
||||
const_iterator it = this->begin() + i;
|
||||
if (key == it->first)
|
||||
return it;
|
||||
}
|
||||
return this->end();
|
||||
}
|
||||
|
||||
pair<iterator, bool> insert(const Key& key, const T& t)
|
||||
{
|
||||
bool b {false};
|
||||
iterator it = find(key);
|
||||
if (it == this->end())
|
||||
{
|
||||
this->push(make_pair(key, t));
|
||||
b = true;
|
||||
it = this->end() - 1;
|
||||
}
|
||||
return {it, b};
|
||||
}
|
||||
|
||||
pair<iterator, bool> insert(const pair<Key, T>& p)
|
||||
{
|
||||
bool b {false};
|
||||
const_iterator it = find(p.first);
|
||||
if (it == this->end())
|
||||
{
|
||||
this->push(p);
|
||||
b = true;
|
||||
it = this->end() - 1;
|
||||
}
|
||||
return {(iterator)it, b};
|
||||
}
|
||||
|
||||
pair<iterator, bool> emplace(const Key& key, const T& t)
|
||||
{
|
||||
return insert(key, t);
|
||||
}
|
||||
|
||||
pair<iterator, bool> emplace(const pair<Key, T>& p)
|
||||
{
|
||||
return insert(p);
|
||||
}
|
||||
|
||||
const T& at(const Key& key) const
|
||||
{
|
||||
// iterator it = find(key);
|
||||
// if (it != this->end()) return it->second;
|
||||
// return T();
|
||||
return find(key)->second;
|
||||
}
|
||||
|
||||
T& at(const Key& key)
|
||||
{
|
||||
// iterator it = find(key);
|
||||
// if (it != this->end()) return it->second;
|
||||
// return T();
|
||||
return find(key)->second;
|
||||
}
|
||||
|
||||
iterator erase(const iterator it)
|
||||
{
|
||||
iterator i = (iterator)find(it->first);
|
||||
if (i != this->end())
|
||||
{
|
||||
return this->erase(i);
|
||||
}
|
||||
return this->end();
|
||||
}
|
||||
|
||||
iterator erase(const size_t index)
|
||||
{
|
||||
if (index < this->size())
|
||||
{
|
||||
iterator it = this->begin() + index;
|
||||
return this->erase(it);
|
||||
}
|
||||
return this->end();
|
||||
}
|
||||
|
||||
const T& operator[] (const Key& key)
|
||||
{
|
||||
const_iterator it = find(key);
|
||||
if (it != this->end()) return it->second;
|
||||
|
||||
insert(::arx::make_pair(key, T()));
|
||||
return this->back().second;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
using RingBuffer<pair<Key, T>, N>::capacity;
|
||||
using RingBuffer<pair<Key, T>, N>::data;
|
||||
using RingBuffer<pair<Key, T>, N>::pop;
|
||||
using RingBuffer<pair<Key, T>, N>::pop_front;
|
||||
using RingBuffer<pair<Key, T>, N>::pop_back;
|
||||
using RingBuffer<pair<Key, T>, N>::push;
|
||||
using RingBuffer<pair<Key, T>, N>::push_back;
|
||||
using RingBuffer<pair<Key, T>, N>::push_front;
|
||||
using RingBuffer<pair<Key, T>, N>::emplace_back;
|
||||
using RingBuffer<pair<Key, T>, N>::front;
|
||||
using RingBuffer<pair<Key, T>, N>::back;
|
||||
using RingBuffer<pair<Key, T>, N>::resize;
|
||||
using RingBuffer<pair<Key, T>, N>::assign;
|
||||
using RingBuffer<pair<Key, T>, N>::shrink_to_fit;
|
||||
};
|
||||
|
||||
} // namespace arx
|
||||
|
||||
template<typename T, size_t N>
|
||||
using ArxRingBuffer = arx::RingBuffer<T, N>;
|
||||
|
||||
|
||||
#endif // ARX_CONTAINER_DISABLED
|
||||
#endif // ARX_RINGBUFFER_H
|
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 Hideaki Tai
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
@ -0,0 +1,166 @@
|
||||
# ArxContainer
|
||||
|
||||
C++ container-like classes (vector, map, etc.) for Arduino which cannot use STL
|
||||
|
||||
## Note
|
||||
|
||||
`ArxContainer` is C++ container-__like__ classes for Arduino.
|
||||
All of the functions is not supported currently.
|
||||
Detail of these containers are described in Detail section.
|
||||
|
||||
## Supported Container Types
|
||||
|
||||
- `vector`
|
||||
- `map` (`pair`)
|
||||
- `deque`
|
||||
|
||||
|
||||
## Supported Boards
|
||||
|
||||
This library is currently enabled only if you use following architecture.
|
||||
Please use C++ Standard Template Library for other boards.
|
||||
|
||||
- AVR (Uno, Nano, Mega, etc.)
|
||||
- MEGAAVR (Uno WiFi, Nano Ecery, etc.)
|
||||
- SAMD (Zero, MKR, M0, etc.)
|
||||
- SPRESENSE
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
### vector
|
||||
|
||||
```C++
|
||||
// initialize with initializer_list
|
||||
arx::vector<int> vs {1, 2, 3};
|
||||
|
||||
// add contents
|
||||
for (size_t i = 4; i <= 5; ++i)
|
||||
vs.push_back(i);
|
||||
|
||||
// index access
|
||||
for (size_t i = 0; i < vs.size(); ++i)
|
||||
Serial.println(vs[i]);
|
||||
|
||||
// range-based access
|
||||
for (const auto& v : vs)
|
||||
Serial.println(v);
|
||||
```
|
||||
|
||||
### map
|
||||
|
||||
``` C++
|
||||
// initialize with initializer_list
|
||||
arx::map<String, int> mp {{"one", 1}, {"two", 2}};
|
||||
|
||||
// add contents
|
||||
mp.insert("three", 3);
|
||||
mp["four"] = 4;
|
||||
|
||||
// range based access
|
||||
for (const auto& m : mp)
|
||||
{
|
||||
Serial.print("{");
|
||||
Serial.print(m.first); Serial.print(",");
|
||||
Serial.print(m.second);
|
||||
Serial.println("}");
|
||||
}
|
||||
|
||||
// key access
|
||||
Serial.print("one = "); Serial.println(mp["one"]);
|
||||
Serial.print("two = "); Serial.println(mp["two"]);
|
||||
Serial.print("three = "); Serial.println(mp["three"]);
|
||||
Serial.print("four = "); Serial.println(mp["four"]);
|
||||
```
|
||||
|
||||
### deque
|
||||
|
||||
```C++
|
||||
// initialize with initializer_list
|
||||
arx::deque<int> dq {1, 2, 3};
|
||||
|
||||
// add contents
|
||||
for (int i = 4; i <= 5; ++i)
|
||||
dq.push_back(i);
|
||||
|
||||
// index access
|
||||
for (int i = 0; i < dq.size(); ++i)
|
||||
Serial.print(dq[i]);
|
||||
```
|
||||
|
||||
|
||||
## Detail
|
||||
|
||||
`ArxContainer` is C++ container-__like__ classes for Arduino.
|
||||
This library is based on `arx::RingBuffer` and `arx::xxxx` is limited-size container.
|
||||
`arx::RingBuffer` can be used as:
|
||||
|
||||
```C++
|
||||
ArxRingBuffer<uint8_t, 4> buffer;
|
||||
|
||||
buffer.push(1);
|
||||
buffer.push(2);
|
||||
buffer.push(3);
|
||||
|
||||
for(size_t i = 0; i < buffer.size(); ++i)
|
||||
Serial.println(buffer[i]);
|
||||
|
||||
buffer.pop();
|
||||
|
||||
for(auto& b : buffer)
|
||||
Serial.println(b);
|
||||
```
|
||||
|
||||
`arx::xxxx` are derived from `RingBuffer` and defined as:
|
||||
|
||||
``` C++
|
||||
namespace arx {
|
||||
template <typename T, size_t N = ARX_VECTOR_DEFAULT_SIZE>
|
||||
struct vector : public RingBuffer<T, N>
|
||||
|
||||
template <class Key, class T, size_t N = ARX_MAP_DEFAULT_SIZE>
|
||||
struct map : public RingBuffer<pair<Key, T>, N>
|
||||
|
||||
template <typename T, size_t N = ARX_DEQUE_DEFAULT_SIZE>
|
||||
struct deque : public RingBuffer<T, N>
|
||||
}
|
||||
```
|
||||
|
||||
So range-based loop cannot be applyed to `arx::deque` (iterator is not continuous because it is based on `RingBuffer`).
|
||||
|
||||
|
||||
### Manage Size Limit of Container
|
||||
|
||||
Global default size of container can be changed by defining these macros before `#include <ArxContainer.h>`.
|
||||
|
||||
``` C++
|
||||
#define ARX_VECTOR_DEFAULT_SIZE XX // default: 16
|
||||
#define ARX_MAP_DEFAULT_SIZE XX // default: 16
|
||||
#define ARX_DEQUE_DEFAULT_SIZE XX // default: 16
|
||||
```
|
||||
|
||||
Or you can change each container size by template argument.
|
||||
|
||||
``` C++
|
||||
arx::vector<int, 3> vs;
|
||||
arx::map<String, int, 4> ms;
|
||||
arx::deque<int, 5> ds;
|
||||
```
|
||||
|
||||
## Roadmap
|
||||
|
||||
This library will be updated if I want to use more container interfaces on supported boards shown above.
|
||||
PRs are welcome!
|
||||
|
||||
|
||||
## Used Inside of
|
||||
|
||||
- [Packetizer](https://github.com/hideakitai/Packetizer)
|
||||
- [MsgPack](https://github.com/hideakitai/MsgPack)
|
||||
- [MsgPacketizer](https://github.com/hideakitai/MsgPacketizer)
|
||||
- [ArduinoOSC](https://github.com/hideakitai/ArduinoOSC)
|
||||
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "ArxContainer",
|
||||
"keywords": "ringbuffer, vector, deque, map",
|
||||
"description": "C++ container-like classes (vector, map, etc.) for Arduino which cannot use STL",
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/hideakitai/ArxContainer.git"
|
||||
},
|
||||
"authors":
|
||||
{
|
||||
"name": "Hideaki Tai",
|
||||
"url": "https://github.com/hideakitai",
|
||||
"maintainer": true
|
||||
},
|
||||
"version": "0.3.6",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
name=ArxContainer
|
||||
version=0.3.6
|
||||
author=hideakitai
|
||||
maintainer=hideakitai
|
||||
sentence=C++ container-like classes (vector, map, etc.) for Arduino which cannot use STL
|
||||
paragraph=C++ container-like classes (vector, map, etc.) for Arduino which cannot use STL
|
||||
category=Data Storage
|
||||
url=https://github.com/hideakitai
|
||||
architectures=*
|
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2020 Hideaki Tai
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
@ -0,0 +1,57 @@
|
||||
# TeensyDirtySTLErrorSolution
|
||||
|
||||
dirty solution to use STL in teensy
|
||||
|
||||
## Introduction
|
||||
|
||||
Sometimes we want to use STL on Teensy, but Teensy 3.x in Arduino IDE does not support STL by default. Though STL is enabled on Teensy 4.x, but you have to enable it manually on Teensy 3.x. If you use PlatformIO, you can also use STL on Teensy 3.x.
|
||||
|
||||
| | Teensy 4.x | Teensy 3.x |
|
||||
| ----------- | ----------------- | ----------------- |
|
||||
| PlatformIO | Almost Compilable | Almost Compilable |
|
||||
| Arduino IDE | Almost Compilable | NOT Compilable |
|
||||
|
||||
|
||||
## Enable STL in Arduino IDE
|
||||
|
||||
To enable STL on Teensy 3.x using Arduino IDE, add `-lstdc++` to `build flags`, which is defined in `boards.txt`. Many STL can be used by adding this flag. You have to do this procedure every time you upgrade the version of Teensyduino.
|
||||
|
||||
| OS | default location |
|
||||
| ------- | --------------------------------------------------------------- |
|
||||
| macOS | `Arduino.app/Contents/Java/hardware/teensy/avr/boards.txt` |
|
||||
| Windows | `C:\Program Files (x86)\Arduino\hardware\teensy\avr\boards.txt` |
|
||||
|
||||
|
||||
#### Before
|
||||
|
||||
``` boards.txt
|
||||
teensy36.build.flags.libs=-larm_cortexM4lf_math -lm
|
||||
teensy35.build.flags.libs=-larm_cortexM4lf_math -lm
|
||||
teensy31.build.flags.libs=-larm_cortexM4l_math -lm
|
||||
```
|
||||
|
||||
#### After
|
||||
|
||||
``` boards.txt
|
||||
teensy36.build.flags.libs=-larm_cortexM4lf_math -lm -lstdc++
|
||||
teensy35.build.flags.libs=-larm_cortexM4lf_math -lm -lstdc++
|
||||
teensy31.build.flags.libs=-larm_cortexM4l_math -lm -lstdc++
|
||||
```
|
||||
|
||||
## Do you still have compile error?
|
||||
|
||||
Yes, maybe you still have compilation error in both 4.x and 3.x for some STLs... Then you should add this library (header file) to your project. Some dirty solution I found is included in this header file.
|
||||
|
||||
``` C++
|
||||
#include <TeensyDirtySTLErrorSolution.h>
|
||||
```
|
||||
|
||||
## Warning
|
||||
|
||||
This method is not recommended because STL is not enabled by default... (if you know the reason, please let me know). I am not responsible for any trouble that may occur as a result of the use of this library. Please use at your own risk.
|
||||
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
|
@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#ifdef TEENSYDUINO
|
||||
#ifndef TEENSYDUINO_DIRTY_STL_ERROR_SOLUTION
|
||||
#define TEENSYDUINO_DIRTY_STL_ERROR_SOLUTION
|
||||
|
||||
|
||||
#if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) // TEENSY 3.1/3.2, 3.5, 3.6
|
||||
|
||||
extern "C"
|
||||
{
|
||||
int _write() { return -1; }
|
||||
}
|
||||
|
||||
#elif defined(__IMXRT1062__) // TEENSY4.0
|
||||
|
||||
extern "C"
|
||||
{
|
||||
void *__exidx_start __attribute__((__visibility__ ("hidden")));
|
||||
void *__exidx_end __attribute__((__visibility__ ("hidden")));
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#error NOT SUPPORTED TEENSY VERSION
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
// ----- template -----
|
||||
//
|
||||
// #if defined(__MK20DX256__) // TEENSY3.1/3.2
|
||||
// #elif defined(__MK64FX512__) // TEENSY3.5
|
||||
// #elif defined(__MK66FX1M0__) // TEENSY3.6
|
||||
// #elif defined(__IMXRT1062__) // TEENSY4.0
|
||||
//
|
||||
// extern "C"
|
||||
// {
|
||||
// int _getpid() { return -1; }
|
||||
// int _kill(int pid, int sig) { return -1; }
|
||||
// int _write() { return -1; }
|
||||
// void *__exidx_start __attribute__((__visibility__ ("hidden")));
|
||||
// void *__exidx_end __attribute__((__visibility__ ("hidden")));
|
||||
// int _getpid();// { return -1; }
|
||||
// int _kill(int pid, int sig);// { return -1; }
|
||||
// int _write() { return -1; }
|
||||
// void *__exidx_start __attribute__((__visibility__ ("hidden")));
|
||||
// void *__exidx_end __attribute__((__visibility__ ("hidden")));
|
||||
// }
|
||||
//
|
||||
// // copied from https://github.com/gcc-mirror/
|
||||
// namespace std
|
||||
// {
|
||||
// void __throw_bad_alloc() { _GLIBCXX_THROW_OR_ABORT(bad_alloc()); }
|
||||
// void __throw_length_error(const char* __s __attribute__((unused))) { _GLIBCXX_THROW_OR_ABORT(length_error(_(__s))); }
|
||||
// void __throw_bad_function_call() { _GLIBCXX_THROW_OR_ABORT(bad_function_call()); }
|
||||
// void _Rb_tree_decrement(std::_Rb_tree_node_base* a) {}
|
||||
// void _Rb_tree_insert_and_rebalance(bool, std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, std::_Rb_tree_node_base&) {}
|
||||
// }
|
||||
|
||||
#endif // TEENSYDUINO_DIRTY_STL_ERROR_SOLUTION
|
||||
#endif // TEENSYDUINO
|
||||
|
Loading…
Reference in New Issue