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;
144 std::vector<T> bytes;
148 using iterator =
typename std::vector<T>::iterator;
160 VectorOf(std::initializer_list<T> values) : bytes(values) {
180 memcpy(this->
data(), p,
sizeof(T)*s);
194 return BottleTagMap <T>();
204 return reinterpret_cast<const char*
>(this->
data());
209 return reinterpret_cast<char*
>(this->
data());
217 {
return bytes.empty() ? nullptr : &(bytes.at(0)); }
225 {
return bytes.empty() ? nullptr : &(bytes.at(0)); }
252 bytes.erase(first, last);
263 std::fill(bytes.begin(), bytes.end(), def);
280 bytes.push_back(elem);
289 bytes.push_back(std::move(elem));
297 template<
typename... _Args>
300 return bytes.emplace_back(std::forward<_Args>(args)...);
338 return this->
data()[i];
348 return this->
data()[i];
360 {
return this->
size();}
367 return bytes.capacity();
375 std::fill(bytes.begin(), bytes.end(), 0);
387 std::string
toString(
int precision=-1,
int width=-1)
const
389 std::string ret =
"";
391 const size_t buffSize = 256;
393 std::string formatStr;
396 formatStr =
"% .*lf\t";
397 for (c=0;c<
length();c++) {
398 snprintf(tmp, buffSize, formatStr.c_str(), precision, (*
this)[c]);
403 formatStr =
"% *.*lf ";
405 snprintf(tmp, buffSize, formatStr.c_str(), width, precision, (*
this)[c]);
412 for (c=0;c<
length();c++) {
413 snprintf(tmp, buffSize, formatStr.c_str(), (*
this)[c]);
419 return ret.substr(0, ret.length() - 1);
437 if ((first<=last)&&((
int)last<(
int)this->
size()))
440 for (
unsigned int k = first; k <= last; k++) {
441 ret[k - first] = (*this)[k];
458 if (position + v.
size() > this->size()) {
461 for (
size_t i = 0; i < v.
size(); i++) {
462 (*this)[position + i] = v(i);
472 std::fill(bytes.begin(), bytes.end(), v);
481 return bytes == r.bytes;
488 return bytes.begin();
502 return bytes.begin();
516 return bytes.cbegin();
#define BOTTLE_TAG_FLOAT64
#define BOTTLE_TAG_FLOAT32
int BottleTagMap< unsigned char >()
int BottleTagMap< unsigned short int >()
int BottleTagMap< int >()
int BottleTagMap< short int >()
int BottleTagMap< float >()
int BottleTagMap< char >()
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
void erase(iterator first, iterator last)
Remove one or more elements from the vector.
std::string toString(int precision=-1, int width=-1) const
Creates a string object containing a text representation of the object.
void erase(iterator pos)
Remove an element from the vector.
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 initialize the element to a default value.
const_iterator begin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
std::string toFormattedString(int precision=-1, int width=-1) const
const_iterator end() const noexcept
Returns a const iterator to the end of the VectorOf.
size_t capacity() const
capacity
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.
VectorOf< double > Vector
The main, catch-all namespace for YARP.