YARP
Yet Another Robot Platform
Vector.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef YARP_SIG_VECTOR_H
8 #define YARP_SIG_VECTOR_H
9 
10 #include <cstring>
11 #include <cstddef> //defines size_t
12 #include <memory>
13 #include <string>
14 #include <vector>
15 
16 #include <yarp/os/Portable.h>
17 #include <yarp/os/ManagedBytes.h>
18 #include <yarp/os/Type.h>
19 
20 #include <yarp/sig/api.h>
21 #include <yarp/os/Log.h>
22 
26 namespace yarp {
27 namespace sig {
28 
29 class VectorBase;
30 template<class T> class VectorOf;
31 // Swig(3.0.12) crashes when generating
32 // ruby bindings without these guards.
33 // Bindings for Vector are generated
34 // anyways throught the %template directive
35 // in the interface file.
36 #ifndef SWIG
38 #endif
39 
40 } // namespace sig
41 } // namespace yarp
42 
43 
52 {
53 public:
54  virtual size_t getElementSize() const = 0;
55  virtual int getBottleTag() const = 0;
56 
57  virtual size_t getListSize() const = 0;
58  virtual const char *getMemoryBlock() const = 0;
59  virtual char *getMemoryBlock() = 0;
60  virtual void resize(size_t size) = 0;
61 
62  /*
63  * Read vector from a connection.
64  * return true iff a vector was read correctly
65  */
66  bool read(yarp::os::ConnectionReader& connection) override;
67 
72  bool write(yarp::os::ConnectionWriter& connection) const override;
73 
74 protected:
75  virtual std::string getFormatStr(int tag) const;
76 
77 };
78 
79 /*
80 * This is a simple function that maps a type into its corresponding BOTTLE tag.
81 * Used for bottle compatible serialization, called inside getBottleTag().
82 * Needs to be instantiated for each type T used in VectorOf<T>.
83 */
84 template<class T>
85 inline int BottleTagMap () {
86  /* make sure this is never called unspecified */
87  yAssert(0);
88  return 0;
89  }
90 
91 template<>
92 inline int BottleTagMap <double> () {
93  return BOTTLE_TAG_FLOAT64;
94  }
95 
96 template<>
97 inline int BottleTagMap <int> () {
98  return BOTTLE_TAG_INT32;
99  }
100 
117 template<class T>
119 {
120 private:
121  std::vector<T> bytes;
122 
123 public:
124  using iterator = typename std::vector<T>::iterator;
125  using const_iterator = typename std::vector<T>::const_iterator;
126 
127  VectorOf() = default;
128 
129  VectorOf(size_t size) : bytes(size) {
130  }
131 
136  VectorOf(std::initializer_list<T> values) : bytes(values) {
137  }
138 
144  VectorOf(size_t s, const T& def) : bytes(s, def) {
145  }
146 
153  VectorOf(size_t s, const T *p)
154  {
155  this->resize(s);
156  memcpy(this->data(), p, sizeof(T)*s);
157  }
158 
159  VectorOf(const VectorOf& r) = default;
160  VectorOf<T> &operator=(const VectorOf<T>& r) = default;
161  VectorOf(VectorOf<T>&& other) noexcept = default;
162  VectorOf& operator=(VectorOf<T>&& other) noexcept = default;
163  ~VectorOf() override = default;
164 
165  size_t getElementSize() const override {
166  return sizeof(T);
167  }
168 
169  int getBottleTag() const override {
170  return BottleTagMap <T>();
171  }
172 
173  size_t getListSize() const override
174  {
175  return bytes.size();
176  }
177 
178  const char* getMemoryBlock() const override
179  {
180  return reinterpret_cast<const char*>(this->data());
181  }
182 
183  char* getMemoryBlock() override
184  {
185  return reinterpret_cast<char*>(this->data());
186  }
187 #ifndef YARP_NO_DEPRECATED // since YARP 3.2.0
188  YARP_DEPRECATED_MSG("Use either data() if you need the pointer to the first element,"
189  " or cbegin() if you need the iterator")
190  inline const T *getFirst() const
191  {
192  return this->data();
193  }
194 
195  YARP_DEPRECATED_MSG("Use either data() if you need the pointer to the first element,"
196  " or begin() if you need the iterator")
197  inline T *getFirst()
198  {
199  return this->data();
200  }
201 #endif // YARP_NO_DEPRECATED
202 
207  inline T *data()
208  { return bytes.empty() ? nullptr : &(bytes.at(0)); }
209 
215  inline const T *data() const
216  { return bytes.empty() ? nullptr : &(bytes.at(0)); }
217 
222  void resize(size_t size) override
223  {
224  bytes.resize(size);
225  }
226 
232  void resize(size_t size, const T&def)
233  {
234  this->resize(size);
235  std::fill(bytes.begin(), bytes.end(), def);
236  }
237 
243  void reserve(size_t size) {
244  bytes.reserve(size);
245  }
246 
250  inline void push_back (const T &elem)
251  {
252  bytes.push_back(elem);
253  }
254 
259  inline void push_back (T&& elem)
260  {
261  bytes.push_back(std::move(elem));
262  }
263 
269  template<typename... _Args>
270  inline T& emplace_back(_Args&&... args)
271  {
272  bytes.emplace_back(std::forward<_Args>(args)...);
273  }
274 
278  inline void pop_back()
279  {
280  bytes.pop_back();
281  }
282 
288  inline T &operator[](size_t i)
289  {
290  return bytes[i];
291  }
292 
298  inline const T &operator[](size_t i) const
299  {
300  return bytes[i];
301  }
302 
308  inline T &operator()(size_t i)
309  {
310  return this->data()[i];
311  }
312 
318  inline const T &operator()(size_t i) const
319  {
320  return this->data()[i];
321  }
322 
323  inline size_t size() const {
324  return bytes.size();
325  }
326 
331  inline size_t length() const
332  { return this->size();}
333 
338  inline size_t capacity() const {
339  return bytes.capacity();
340  }
341 
345  void zero()
346  {
347  std::fill(bytes.begin(), bytes.end(), 0);
348  }
349 
359  std::string toString(int precision=-1, int width=-1) const
360  {
361  std::string ret = "";
362  size_t c = 0;
363  const size_t buffSize = 256;
364  char tmp[buffSize];
365  std::string formatStr;
366  if (getBottleTag() == BOTTLE_TAG_FLOAT64) {
367  if (width<0) {
368  formatStr = "% .*lf\t";
369  for (c=0;c<length();c++) {
370  snprintf(tmp, buffSize, formatStr.c_str(), precision, (*this)[c]);
371  ret+=tmp;
372  }
373  }
374  else{
375  formatStr = "% *.*lf ";
376  for (c=0;c<length();c++){
377  snprintf(tmp, buffSize, formatStr.c_str(), width, precision, (*this)[c]);
378  ret+=tmp;
379  }
380  }
381  }
382  else {
383  formatStr = "%" + getFormatStr(getBottleTag()) + " ";
384  for (c=0;c<length();c++) {
385  snprintf(tmp, buffSize, formatStr.c_str(), (*this)[c]);
386  ret+=tmp;
387  }
388  }
389 
390  if (length() >= 1) {
391  return ret.substr(0, ret.length() - 1);
392  }
393  return ret;
394  }
395 
402  VectorOf<T> subVector(unsigned int first, unsigned int last) const
403  {
405  if ((first<=last)&&((int)last<(int)this->size()))
406  {
407  ret.resize(last-first+1);
408  for (unsigned int k = first; k <= last; k++) {
409  ret[k - first] = (*this)[k];
410  }
411  }
412  return ret;
413  }
414 
424  bool setSubvector(int position, const VectorOf<T> &v)
425  {
426  if (position + v.size() > this->size()) {
427  return false;
428  }
429  for (size_t i = 0; i < v.size(); i++) {
430  (*this)[position + i] = v(i);
431  }
432  return true;
433  }
434 
438  const VectorOf<T> &operator=(T v)
439  {
440  std::fill(bytes.begin(), bytes.end(), v);
441  return *this;
442  }
443 
447  bool operator==(const VectorOf<T> &r) const
448  {
449  return bytes == r.bytes;
450  }
451 
455  iterator begin() noexcept {
456  return bytes.begin();
457  }
458 
462  iterator end() noexcept {
463  return bytes.end();
464  }
465 
469  const_iterator begin() const noexcept {
470  return bytes.begin();
471  }
472 
476  const_iterator end() const noexcept {
477  return bytes.end();
478  }
479 
483  const_iterator cbegin() const noexcept {
484  return bytes.cbegin();
485  }
486 
490  const_iterator cend() const noexcept {
491  return bytes.cend();
492  }
493  void clear() {
494  bytes.clear();
495  }
496 
497  yarp::os::Type getType() const override {
498  return yarp::os::Type::byName("yarp/vector");
499  }
500 };
501 
502 
503 #ifdef _MSC_VER
504 /*YARP_sig_EXTERN*/ template class YARP_sig_API yarp::sig::VectorOf<double>;
505 #endif
506 
507 #endif // YARP_SIG_VECTOR_H
#define BOTTLE_TAG_FLOAT64
Definition: Bottle.h:25
#define BOTTLE_TAG_INT32
Definition: Bottle.h:21
bool ret
#define yAssert(x)
Definition: Log.h:294
int BottleTagMap()
Definition: Vector.h:85
int BottleTagMap< int >()
Definition: Vector.h:97
int BottleTagMap< double >()
Definition: Vector.h:92
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.
Definition: Portable.h:26
static Type byName(const char *name)
Definition: Type.cpp:171
A Base class for a VectorOf<T>, provide default implementation for read/write methods.
Definition: Vector.h:52
virtual size_t getListSize() const =0
virtual size_t getElementSize() const =0
virtual char * getMemoryBlock()=0
virtual const char * getMemoryBlock() const =0
virtual void resize(size_t size)=0
bool write(yarp::os::ConnectionWriter &connection) const override
Write vector to a connection.
Definition: Vector.cpp:79
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition: Vector.cpp:51
virtual std::string getFormatStr(int tag) const
Definition: Vector.cpp:100
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.
Definition: Vector.h:359
T & operator()(size_t i)
Single element access, no range check.
Definition: Vector.h:308
T & emplace_back(_Args &&... args)
Construct a new element in the vector: size is changed.
Definition: Vector.h:270
size_t getListSize() const override
Definition: Vector.h:173
VectorOf(std::initializer_list< T > values)
Initializer list constructor.
Definition: Vector.h:136
T & operator[](size_t i)
Single element access, no range check.
Definition: Vector.h:288
const T & operator()(size_t i) const
Single element access, no range check, const version.
Definition: Vector.h:318
void resize(size_t size) override
Resize the vector.
Definition: Vector.h:222
yarp::os::Type getType() const override
Definition: Vector.h:497
int getBottleTag() const override
Definition: Vector.h:169
void push_back(T &&elem)
Move a new element in the vector: size is changed.
Definition: Vector.h:259
const T * data() const
Return a pointer to the first element of the vector, const version.
Definition: Vector.h:215
size_t size() const
Definition: Vector.h:323
void push_back(const T &elem)
Push a new element in the vector: size is changed.
Definition: Vector.h:250
iterator begin() noexcept
Returns an iterator to the beginning of the VectorOf.
Definition: Vector.h:455
VectorOf< T > & operator=(const VectorOf< T > &r)=default
bool operator==(const VectorOf< T > &r) const
True iff all elements of 'a' match all element of 'b'.
Definition: Vector.h:447
const_iterator cend() const noexcept
Returns a const iterator to the end of the VectorOf.
Definition: Vector.h:490
const T & operator[](size_t i) const
Single element access, no range check, const version.
Definition: Vector.h:298
const T * getFirst() const
Definition: Vector.h:190
~VectorOf() override=default
VectorOf(VectorOf< T > &&other) noexcept=default
T * data()
Return a pointer to the first element of the vector.
Definition: Vector.h:207
char * getMemoryBlock() override
Definition: Vector.h:183
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:402
void resize(size_t size, const T &def)
Resize the vector and initilize the element to a default value.
Definition: Vector.h:232
const_iterator begin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
Definition: Vector.h:469
const_iterator end() const noexcept
Returns a const iterator to the end of the VectorOf.
Definition: Vector.h:476
size_t capacity() const
capacity
Definition: Vector.h:338
VectorOf & operator=(VectorOf< T > &&other) noexcept=default
const_iterator cbegin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
Definition: Vector.h:483
VectorOf(size_t size)
Definition: Vector.h:129
const char * getMemoryBlock() const override
Definition: Vector.h:178
size_t length() const
Get the length of the vector.
Definition: Vector.h:331
VectorOf(size_t s, const T &def)
Build a vector and initialize it with def.
Definition: Vector.h:144
VectorOf(const VectorOf &r)=default
void pop_back()
Pop an element out of the vector: size is changed.
Definition: Vector.h:278
typename std::vector< T >::const_iterator const_iterator
Definition: Vector.h:125
const VectorOf< T > & operator=(T v)
Set all elements of the vector to a scalar.
Definition: Vector.h:438
iterator end() noexcept
Returns an iterator to the end of the VectorOf.
Definition: Vector.h:462
void zero()
Zero the elements of the vector.
Definition: Vector.h:345
VectorOf(size_t s, const T *p)
Builds a vector and initialize it with values from 'p'.
Definition: Vector.h:153
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:243
size_t getElementSize() const override
Definition: Vector.h:165
typename std::vector< T >::iterator iterator
Definition: Vector.h:124
bool setSubvector(int position, const VectorOf< T > &v)
Set a portion of this vector with the values of the specified vector.
Definition: Vector.h:424
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2885
VectorOf< double > Vector
Definition: Vector.h:30
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_sig_API
Definition: api.h:18