YARP
Yet Another Robot Platform
Vector.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef YARP_SIG_VECTOR_H
11 #define YARP_SIG_VECTOR_H
12 
13 #include <cstring>
14 #include <cstddef> //defines size_t
15 #include <memory>
16 #include <string>
17 #include <vector>
18 
19 #include <yarp/os/Portable.h>
20 #include <yarp/os/ManagedBytes.h>
21 #include <yarp/os/Type.h>
22 
23 #include <yarp/sig/api.h>
24 #include <yarp/os/Log.h>
25 
29 namespace yarp {
30 namespace sig {
31 
32 class VectorBase;
33 template<class T> class VectorOf;
34 // Swig(3.0.12) crashes when generating
35 // ruby bindings without these guards.
36 // Bindings for Vector are generated
37 // anyways throught the %template directive
38 // in the interface file.
39 #ifndef SWIG
41 #endif
42 
43 } // namespace sig
44 } // namespace yarp
45 
46 
55 {
56 public:
57  virtual size_t getElementSize() const = 0;
58  virtual int getBottleTag() const = 0;
59 
60  virtual size_t getListSize() const = 0;
61  virtual const char *getMemoryBlock() const = 0;
62  virtual char *getMemoryBlock() = 0;
63  virtual void resize(size_t size) = 0;
64 
65  /*
66  * Read vector from a connection.
67  * return true iff a vector was read correctly
68  */
69  bool read(yarp::os::ConnectionReader& connection) override;
70 
75  bool write(yarp::os::ConnectionWriter& connection) const override;
76 
77 protected:
78  virtual std::string getFormatStr(int tag) const;
79 
80 };
81 
82 /*
83 * This is a simple function that maps a type into its corresponding BOTTLE tag.
84 * Used for bottle compatible serialization, called inside getBottleTag().
85 * Needs to be instantiated for each type T used in VectorOf<T>.
86 */
87 template<class T>
88 inline int BottleTagMap () {
89  /* make sure this is never called unspecified */
90  yAssert(0);
91  return 0;
92  }
93 
94 template<>
95 inline int BottleTagMap <double> () {
96  return BOTTLE_TAG_FLOAT64;
97  }
98 
99 template<>
100 inline int BottleTagMap <int> () {
101  return BOTTLE_TAG_INT32;
102  }
103 
120 template<class T>
122 {
123 private:
124  std::vector<T> bytes;
125 
126 public:
127  using iterator = typename std::vector<T>::iterator;
128  using const_iterator = typename std::vector<T>::const_iterator;
129 
130  VectorOf() = default;
131 
132  VectorOf(size_t size) : bytes(size) {
133  }
134 
139  VectorOf(std::initializer_list<T> values) : bytes(values) {
140  }
141 
147  VectorOf(size_t s, const T& def) : bytes(s, def) {
148  }
149 
156  VectorOf(size_t s, const T *p)
157  {
158  this->resize(s);
159  memcpy(this->data(), p, sizeof(T)*s);
160  }
161 
162  VectorOf(const VectorOf& r) : VectorBase(), bytes(r.bytes) {
163  }
164 
169  {
170 
171  if (this == &r) return *this;
172  bytes = r.bytes;
173  return *this;
174  }
175 
181  VectorOf(VectorOf<T>&& other) noexcept : bytes(std::move(other.bytes)) {
182  }
183 
190  VectorOf& operator=(VectorOf<T>&& other) noexcept
191  {
192  bytes = std::move(other.bytes);
193  return *this;
194  }
195 
196 
197  size_t getElementSize() const override {
198  return sizeof(T);
199  }
200 
201  int getBottleTag() const override {
202  return BottleTagMap <T>();
203  }
204 
205  size_t getListSize() const override
206  {
207  return bytes.size();
208  }
209 
210  const char* getMemoryBlock() const override
211  {
212  return reinterpret_cast<const char*>(this->data());
213  }
214 
215  char* getMemoryBlock() override
216  {
217  return reinterpret_cast<char*>(this->data());
218  }
219 #ifndef YARP_NO_DEPRECATED // since YARP 3.2.0
220  YARP_DEPRECATED_MSG("Use either data() if you need the pointer to the first element,"
221  " or cbegin() if you need the iterator")
222  inline const T *getFirst() const
223  {
224  return this->data();
225  }
226 
227  YARP_DEPRECATED_MSG("Use either data() if you need the pointer to the first element,"
228  " or begin() if you need the iterator")
229  inline T *getFirst()
230  {
231  return this->data();
232  }
233 #endif // YARP_NO_DEPRECATED
234 
239  inline T *data()
240  { return bytes.empty() ? nullptr : &(bytes.at(0)); }
241 
247  inline const T *data() const
248  { return bytes.empty() ? nullptr : &(bytes.at(0)); }
249 
254  void resize(size_t size) override
255  {
256  bytes.resize(size);
257  }
258 
264  void resize(size_t size, const T&def)
265  {
266  this->resize(size);
267  std::fill(bytes.begin(), bytes.end(), def);
268  }
269 
275  void reserve(size_t size) {
276  bytes.reserve(size);
277  }
278 
282  inline void push_back (const T &elem)
283  {
284  bytes.push_back(elem);
285  }
286 
291  inline void push_back (T&& elem)
292  {
293  bytes.push_back(std::move(elem));
294  }
295 
301  template<typename... _Args>
302  inline T& emplace_back(_Args&&... args)
303  {
304  bytes.emplace_back(std::forward<_Args>(args)...);
305  }
306 
310  inline void pop_back()
311  {
312  bytes.pop_back();
313  }
314 
320  inline T &operator[](size_t i)
321  {
322  return bytes[i];
323  }
324 
330  inline const T &operator[](size_t i) const
331  {
332  return bytes[i];
333  }
334 
340  inline T &operator()(size_t i)
341  {
342  return this->data()[i];
343  }
344 
350  inline const T &operator()(size_t i) const
351  {
352  return this->data()[i];
353  }
354 
355  inline size_t size() const {
356  return bytes.size();
357  }
358 
363  inline size_t length() const
364  { return this->size();}
365 
370  inline size_t capacity() const {
371  return bytes.capacity();
372  }
373 
377  void zero()
378  {
379  std::fill(bytes.begin(), bytes.end(), 0);
380  }
381 
391  std::string toString(int precision=-1, int width=-1) const
392  {
393  std::string ret = "";
394  size_t c = 0;
395  const size_t buffSize = 256;
396  char tmp[buffSize];
397  std::string formatStr;
398  if (getBottleTag() == BOTTLE_TAG_FLOAT64) {
399  if (width<0) {
400  formatStr = "% .*lf\t";
401  for (c=0;c<length();c++) {
402  snprintf(tmp, buffSize, formatStr.c_str(), precision, (*this)[c]);
403  ret+=tmp;
404  }
405  }
406  else{
407  formatStr = "% *.*lf ";
408  for (c=0;c<length();c++){
409  snprintf(tmp, buffSize, formatStr.c_str(), width, precision, (*this)[c]);
410  ret+=tmp;
411  }
412  }
413  }
414  else {
415  formatStr = "%" + getFormatStr(getBottleTag()) + " ";
416  for (c=0;c<length();c++) {
417  snprintf(tmp, buffSize, formatStr.c_str(), (*this)[c]);
418  ret+=tmp;
419  }
420  }
421 
422  if (length()>=1)
423  return ret.substr(0, ret.length()-1);
424  return ret;
425  }
426 
433  VectorOf<T> subVector(unsigned int first, unsigned int last) const
434  {
436  if ((first<=last)&&((int)last<(int)this->size()))
437  {
438  ret.resize(last-first+1);
439  for (unsigned int k=first; k<=last; k++)
440  ret[k-first]=(*this)[k];
441  }
442  return ret;
443  }
444 
454  bool setSubvector(int position, const VectorOf<T> &v)
455  {
456  if (position+v.size() > this->size())
457  return false;
458  for (size_t i=0;i<v.size();i++)
459  (*this)[position+i] = v(i);
460  return true;
461  }
462 
466  const VectorOf<T> &operator=(T v)
467  {
468  std::fill(bytes.begin(), bytes.end(), v);
469  return *this;
470  }
471 
475  bool operator==(const VectorOf<T> &r) const
476  {
477  return bytes == r.bytes;
478  }
479 
483  iterator begin() noexcept {
484  return bytes.begin();
485  }
486 
490  iterator end() noexcept {
491  return bytes.end();
492  }
493 
497  const_iterator begin() const noexcept {
498  return bytes.begin();
499  }
500 
504  const_iterator end() const noexcept {
505  return bytes.end();
506  }
507 
511  const_iterator cbegin() const noexcept {
512  return bytes.cbegin();
513  }
514 
518  const_iterator cend() const noexcept {
519  return bytes.cend();
520  }
521  void clear() {
522  bytes.clear();
523  }
524 
525  yarp::os::Type getType() const override {
526  return yarp::os::Type::byName("yarp/vector");
527  }
528 };
529 
530 
531 #ifdef _MSC_VER
532 /*YARP_sig_EXTERN*/ template class YARP_sig_API yarp::sig::VectorOf<double>;
533 #endif
534 
535 #endif // YARP_SIG_VECTOR_H
BottleTagMap
int BottleTagMap()
Definition: Vector.h:88
yarp::sig::VectorBase::getMemoryBlock
virtual char * getMemoryBlock()=0
yarp::os::Portable
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:29
yarp::sig::VectorOf::resize
void resize(size_t size) override
Resize the vector.
Definition: Vector.h:254
yarp::sig::VectorOf::getMemoryBlock
char * getMemoryBlock() override
Definition: Vector.h:215
yarp::sig::VectorOf::getMemoryBlock
const char * getMemoryBlock() const override
Definition: Vector.h:210
api.h
yarp::sig::VectorOf::operator=
const VectorOf< T > & operator=(T v)
Set all elements of the vector to a scalar.
Definition: Vector.h:466
yarp::sig::VectorOf::cbegin
const_iterator cbegin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
Definition: Vector.h:511
yarp::sig::VectorOf::resize
void resize(size_t size, const T &def)
Resize the vector and initilize the element to a default value.
Definition: Vector.h:264
yarp::os::Type
Definition: Type.h:24
yarp::sig::VectorOf::getBottleTag
int getBottleTag() const override
Definition: Vector.h:201
Portable.h
yarp::sig::VectorOf::VectorOf
VectorOf(size_t s, const T *p)
Builds a vector and initialize it with values from 'p'.
Definition: Vector.h:156
yarp::sig::VectorBase::getFormatStr
virtual std::string getFormatStr(int tag) const
Definition: Vector.cpp:98
yarp::sig::VectorOf::operator=
VectorOf & operator=(VectorOf< T > &&other) noexcept
Move assignment operator.
Definition: Vector.h:190
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yarp::sig::VectorOf::clear
void clear()
Definition: Vector.h:521
yarp::sig::VectorOf::end
const_iterator end() const noexcept
Returns a const iterator to the end of the VectorOf.
Definition: Vector.h:504
yarp::sig::VectorOf::operator()
T & operator()(size_t i)
Single element access, no range check.
Definition: Vector.h:340
yarp::sig::VectorOf::begin
const_iterator begin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
Definition: Vector.h:497
BOTTLE_TAG_INT32
#define BOTTLE_TAG_INT32
Definition: Bottle.h:23
yarp::sig::VectorOf::operator()
const T & operator()(size_t i) const
Single element access, no range check, const version.
Definition: Vector.h:350
yarp::sig::VectorOf< double >
Log.h
yarp::sig::VectorOf< unsigned char >::iterator
typename std::vector< unsigned char >::iterator iterator
Definition: Vector.h:127
yarp::sig::VectorBase::getListSize
virtual size_t getListSize() const =0
yarp::os::Type::byName
static Type byName(const char *name)
Definition: Type.cpp:174
yarp::sig::VectorOf::cend
const_iterator cend() const noexcept
Returns a const iterator to the end of the VectorOf.
Definition: Vector.h:518
ManagedBytes.h
Type.h
yarp::os::ConnectionWriter
An interface for writing to a network connection.
Definition: ConnectionWriter.h:40
yarp::sig::VectorOf::getListSize
size_t getListSize() const override
Definition: Vector.h:205
yarp::sig::VectorOf::capacity
size_t capacity() const
capacity
Definition: Vector.h:370
yarp::sig::VectorOf::pop_back
void pop_back()
Pop an element out of the vector: size is changed.
Definition: Vector.h:310
yarp::sig::VectorOf::emplace_back
T & emplace_back(_Args &&... args)
Construct a new element in the vector: size is changed.
Definition: Vector.h:302
yarp::sig::VectorOf::VectorOf
VectorOf(size_t size)
Definition: Vector.h:132
yarp::sig::VectorOf::zero
void zero()
Zero the elements of the vector.
Definition: Vector.h:377
yarp::sig::VectorOf< unsigned char >::const_iterator
typename std::vector< unsigned char >::const_iterator const_iterator
Definition: Vector.h:128
yarp::sig::VectorOf::length
size_t length() const
Get the length of the vector.
Definition: Vector.h:363
YARP_sig_API
#define YARP_sig_API
Definition: api.h:19
yarp::sig::VectorBase::read
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition: Vector.cpp:54
BottleTagMap< double >
int BottleTagMap< double >()
Definition: Vector.h:95
yarp::sig::VectorOf::end
iterator end() noexcept
Returns an iterator to the end of the VectorOf.
Definition: Vector.h:490
yarp::sig::VectorBase
A Base class for a VectorOf<T>, provide default implementation for read/write methods.
Definition: Vector.h:55
yarp::sig::VectorOf::VectorOf
VectorOf(const VectorOf &r)
Definition: Vector.h:162
yarp::os::ConnectionReader
An interface for reading from a network connection.
Definition: ConnectionReader.h:40
yarp::sig::VectorOf::data
T * data()
Return a pointer to the first element of the vector.
Definition: Vector.h:239
BOTTLE_TAG_FLOAT64
#define BOTTLE_TAG_FLOAT64
Definition: Bottle.h:27
yarp::sig::VectorOf::toString
std::string toString(int precision=-1, int width=-1) const
Creates a string object containing a text representation of the object.
Definition: Vector.h:391
yarp::sig::VectorOf::operator==
bool operator==(const VectorOf< T > &r) const
True iff all elements of 'a' match all element of 'b'.
Definition: Vector.h:475
yarp::sig::Vector
VectorOf< double > Vector
Definition: Vector.h:33
yarp::sig::VectorOf::data
const T * data() const
Return a pointer to the first element of the vector, const version.
Definition: Vector.h:247
YARP_DEPRECATED_MSG
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2883
yarp::sig::VectorOf::VectorOf
VectorOf()=default
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::sig::VectorBase::getElementSize
virtual size_t getElementSize() const =0
yarp::sig::VectorOf::reserve
void reserve(size_t size)
reserve, increase the capacity of the vector to a value that's greater or equal to size.
Definition: Vector.h:275
yarp::sig::VectorOf::VectorOf
VectorOf(VectorOf< T > &&other) noexcept
Move constructor.
Definition: Vector.h:181
yarp::sig::VectorOf::operator[]
const T & operator[](size_t i) const
Single element access, no range check, const version.
Definition: Vector.h:330
yarp::sig::VectorOf::VectorOf
VectorOf(size_t s, const T &def)
Build a vector and initialize it with def.
Definition: Vector.h:147
yarp::sig::VectorOf::operator[]
T & operator[](size_t i)
Single element access, no range check.
Definition: Vector.h:320
yarp::sig::VectorOf::size
size_t size() const
Definition: Vector.h:355
BottleTagMap< int >
int BottleTagMap< int >()
Definition: Vector.h:100
yarp::sig::VectorOf::push_back
void push_back(T &&elem)
Move a new element in the vector: size is changed.
Definition: Vector.h:291
yarp::sig::VectorBase::getBottleTag
virtual int getBottleTag() const =0
yAssert
#define yAssert(x)
Definition: Log.h:297
yarp::sig::VectorOf::begin
iterator begin() noexcept
Returns an iterator to the beginning of the VectorOf.
Definition: Vector.h:483
yarp::sig::VectorOf::getFirst
const T * getFirst() const
Definition: Vector.h:222
yarp::sig::VectorOf::VectorOf
VectorOf(std::initializer_list< T > values)
Initializer list constructor.
Definition: Vector.h:139
yarp::sig::VectorOf::getElementSize
size_t getElementSize() const override
Definition: Vector.h:197
yarp::sig::VectorOf::push_back
void push_back(const T &elem)
Push a new element in the vector: size is changed.
Definition: Vector.h:282
yarp::sig::VectorOf::getType
yarp::os::Type getType() const override
Definition: Vector.h:525
yarp::sig::VectorOf::subVector
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...
Definition: Vector.h:433
yarp::sig::VectorBase::getMemoryBlock
virtual const char * getMemoryBlock() const =0
yarp::sig::VectorOf::operator=
const VectorOf< T > & operator=(const VectorOf< T > &r)
Copy operator;.
Definition: Vector.h:168
yarp::sig::VectorBase::write
bool write(yarp::os::ConnectionWriter &connection) const override
Write vector to a connection.
Definition: Vector.cpp:77
yarp::sig::VectorBase::resize
virtual void resize(size_t size)=0
yarp::sig::VectorOf::setSubvector
bool setSubvector(int position, const VectorOf< T > &v)
Set a portion of this vector with the values of the specified vector.
Definition: Vector.h:454