7#ifndef YARP_SIG_VECTOR_H
8#define YARP_SIG_VECTOR_H
29template<
class T>
class VectorOf;
58 virtual void resize(
size_t size) = 0;
119 std::vector<T> bytes;
123 using iterator =
typename std::vector<T>::iterator;
135 VectorOf(std::initializer_list<T> values) : bytes(values) {
155 memcpy(this->
data(), p,
sizeof(T)*s);
169 return BottleTagMap <T>();
179 return reinterpret_cast<const char*
>(this->
data());
184 return reinterpret_cast<char*
>(this->
data());
186#ifndef YARP_NO_DEPRECATED
188 " or cbegin() if you need the iterator")
195 " or begin() if you need the iterator")
207 {
return bytes.empty() ? nullptr : &(bytes.at(0)); }
215 {
return bytes.empty() ? nullptr : &(bytes.at(0)); }
234 std::fill(bytes.begin(), bytes.end(), def);
251 bytes.push_back(elem);
260 bytes.push_back(std::move(elem));
268 template<
typename... _Args>
271 return bytes.emplace_back(std::forward<_Args>(args)...);
309 return this->
data()[i];
319 return this->
data()[i];
331 {
return this->
size();}
338 return bytes.capacity();
346 std::fill(bytes.begin(), bytes.end(), 0);
358 std::string
toString(
int precision=-1,
int width=-1)
const
360 std::string
ret =
"";
362 const size_t buffSize = 256;
364 std::string formatStr;
367 formatStr =
"% .*lf\t";
368 for (c=0;c<
length();c++) {
369 snprintf(tmp, buffSize, formatStr.c_str(), precision, (*
this)[c]);
374 formatStr =
"% *.*lf ";
376 snprintf(tmp, buffSize, formatStr.c_str(), width, precision, (*
this)[c]);
383 for (c=0;c<
length();c++) {
384 snprintf(tmp, buffSize, formatStr.c_str(), (*
this)[c]);
390 return ret.substr(0,
ret.length() - 1);
404 if ((first<=last)&&((
int)last<(
int)this->
size()))
407 for (
unsigned int k = first; k <= last; k++) {
408 ret[k - first] = (*this)[k];
425 if (position + v.
size() > this->size()) {
428 for (
size_t i = 0; i < v.
size(); i++) {
429 (*this)[position + i] = v(i);
439 std::fill(bytes.begin(), bytes.end(), v);
448 return bytes == r.bytes;
455 return bytes.begin();
469 return bytes.begin();
483 return bytes.cbegin();
#define BOTTLE_TAG_FLOAT64
int BottleTagMap< int >()
int BottleTagMap< double >()
An interface for reading from a network connection.
An interface for writing to a network connection.
This is a base class for objects that can be both read from and be written to the YARP network.
static Type byName(const char *name)
A Base class for a VectorOf<T>, provide default implementation for read/write methods.
virtual size_t getListSize() const =0
virtual char * getMemoryBlock()=0
virtual size_t getElementSize() const =0
virtual void resize(size_t size)=0
virtual const char * getMemoryBlock() const =0
bool write(yarp::os::ConnectionWriter &connection) const override
Write vector to a connection.
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
virtual std::string getFormatStr(int tag) const
virtual int getBottleTag() const =0
std::string toString(int precision=-1, int width=-1) const
Creates a string object containing a text representation of the object.
size_t getListSize() const override
VectorOf(std::initializer_list< T > values)
Initializer list constructor.
void resize(size_t size) override
Resize the vector.
VectorOf & operator=(VectorOf< T > &&other) noexcept=default
yarp::os::Type getType() const override
int getBottleTag() const override
const VectorOf< T > & operator=(T v)
Set all elements of the vector to a scalar.
void push_back(T &&elem)
Move a new element in the vector: size is changed.
void push_back(const T &elem)
Push a new element in the vector: size is changed.
iterator begin() noexcept
Returns an iterator to the beginning of the VectorOf.
const T & operator()(size_t i) const
Single element access, no range check, const version.
bool operator==(const VectorOf< T > &r) const
True iff all elements of 'a' match all element of 'b'.
T * data()
Return a pointer to the first element of the vector.
VectorOf< T > subVector(unsigned int first, unsigned int last) const
Creates and returns a new vector, being the portion of the original vector defined by the first and l...
const_iterator cend() const noexcept
Returns a const iterator to the end of the VectorOf.
~VectorOf() override=default
VectorOf< T > & operator=(const VectorOf< T > &r)=default
VectorOf(VectorOf< T > &&other) noexcept=default
void resize(size_t size, const T &def)
Resize the vector and initilize the element to a default value.
const_iterator begin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
const_iterator end() const noexcept
Returns a const iterator to the end of the VectorOf.
size_t capacity() const
capacity
const T * getFirst() const
const_iterator cbegin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
T & operator[](size_t i)
Single element access, no range check.
const T * data() const
Return a pointer to the first element of the vector, const version.
size_t length() const
Get the length of the vector.
T & emplace_back(_Args &&... args)
Construct a new element in the vector: size is changed.
VectorOf(size_t s, const T &def)
Build a vector and initialize it with def.
char * getMemoryBlock() override
VectorOf(const VectorOf &r)=default
const char * getMemoryBlock() const override
void pop_back()
Pop an element out of the vector: size is changed.
typename std::vector< T >::const_iterator const_iterator
T & operator()(size_t i)
Single element access, no range check.
iterator end() noexcept
Returns an iterator to the end of the VectorOf.
void zero()
Zero the elements of the vector.
VectorOf(size_t s, const T *p)
Builds a vector and initialize it with values from 'p'.
const T & operator[](size_t i) const
Single element access, no range check, const version.
void reserve(size_t size)
reserve, increase the capacity of the vector to a value that's greater or equal to size.
size_t getElementSize() const override
typename std::vector< T >::iterator iterator
bool setSubvector(int position, const VectorOf< T > &v)
Set a portion of this vector with the values of the specified vector.
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
VectorOf< double > Vector
The main, catch-all namespace for YARP.