YARP
Yet Another Robot Platform
Vector.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 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) = default;
163  VectorOf<T> &operator=(const VectorOf<T>& r) = default;
164  VectorOf(VectorOf<T>&& other) noexcept = default;
165  VectorOf& operator=(VectorOf<T>&& other) noexcept = default;
166  ~VectorOf() override = default;
167 
168  size_t getElementSize() const override {
169  return sizeof(T);
170  }
171 
172  int getBottleTag() const override {
173  return BottleTagMap <T>();
174  }
175 
176  size_t getListSize() const override
177  {
178  return bytes.size();
179  }
180 
181  const char* getMemoryBlock() const override
182  {
183  return reinterpret_cast<const char*>(this->data());
184  }
185 
186  char* getMemoryBlock() override
187  {
188  return reinterpret_cast<char*>(this->data());
189  }
190 #ifndef YARP_NO_DEPRECATED // since YARP 3.2.0
191  YARP_DEPRECATED_MSG("Use either data() if you need the pointer to the first element,"
192  " or cbegin() if you need the iterator")
193  inline const T *getFirst() const
194  {
195  return this->data();
196  }
197 
198  YARP_DEPRECATED_MSG("Use either data() if you need the pointer to the first element,"
199  " or begin() if you need the iterator")
200  inline T *getFirst()
201  {
202  return this->data();
203  }
204 #endif // YARP_NO_DEPRECATED
205 
210  inline T *data()
211  { return bytes.empty() ? nullptr : &(bytes.at(0)); }
212 
218  inline const T *data() const
219  { return bytes.empty() ? nullptr : &(bytes.at(0)); }
220 
225  void resize(size_t size) override
226  {
227  bytes.resize(size);
228  }
229 
235  void resize(size_t size, const T&def)
236  {
237  this->resize(size);
238  std::fill(bytes.begin(), bytes.end(), def);
239  }
240 
246  void reserve(size_t size) {
247  bytes.reserve(size);
248  }
249 
253  inline void push_back (const T &elem)
254  {
255  bytes.push_back(elem);
256  }
257 
262  inline void push_back (T&& elem)
263  {
264  bytes.push_back(std::move(elem));
265  }
266 
272  template<typename... _Args>
273  inline T& emplace_back(_Args&&... args)
274  {
275  bytes.emplace_back(std::forward<_Args>(args)...);
276  }
277 
281  inline void pop_back()
282  {
283  bytes.pop_back();
284  }
285 
291  inline T &operator[](size_t i)
292  {
293  return bytes[i];
294  }
295 
301  inline const T &operator[](size_t i) const
302  {
303  return bytes[i];
304  }
305 
311  inline T &operator()(size_t i)
312  {
313  return this->data()[i];
314  }
315 
321  inline const T &operator()(size_t i) const
322  {
323  return this->data()[i];
324  }
325 
326  inline size_t size() const {
327  return bytes.size();
328  }
329 
334  inline size_t length() const
335  { return this->size();}
336 
341  inline size_t capacity() const {
342  return bytes.capacity();
343  }
344 
348  void zero()
349  {
350  std::fill(bytes.begin(), bytes.end(), 0);
351  }
352 
362  std::string toString(int precision=-1, int width=-1) const
363  {
364  std::string ret = "";
365  size_t c = 0;
366  const size_t buffSize = 256;
367  char tmp[buffSize];
368  std::string formatStr;
369  if (getBottleTag() == BOTTLE_TAG_FLOAT64) {
370  if (width<0) {
371  formatStr = "% .*lf\t";
372  for (c=0;c<length();c++) {
373  snprintf(tmp, buffSize, formatStr.c_str(), precision, (*this)[c]);
374  ret+=tmp;
375  }
376  }
377  else{
378  formatStr = "% *.*lf ";
379  for (c=0;c<length();c++){
380  snprintf(tmp, buffSize, formatStr.c_str(), width, precision, (*this)[c]);
381  ret+=tmp;
382  }
383  }
384  }
385  else {
386  formatStr = "%" + getFormatStr(getBottleTag()) + " ";
387  for (c=0;c<length();c++) {
388  snprintf(tmp, buffSize, formatStr.c_str(), (*this)[c]);
389  ret+=tmp;
390  }
391  }
392 
393  if (length()>=1)
394  return ret.substr(0, ret.length()-1);
395  return ret;
396  }
397 
404  VectorOf<T> subVector(unsigned int first, unsigned int last) const
405  {
407  if ((first<=last)&&((int)last<(int)this->size()))
408  {
409  ret.resize(last-first+1);
410  for (unsigned int k=first; k<=last; k++)
411  ret[k-first]=(*this)[k];
412  }
413  return ret;
414  }
415 
425  bool setSubvector(int position, const VectorOf<T> &v)
426  {
427  if (position+v.size() > this->size())
428  return false;
429  for (size_t i=0;i<v.size();i++)
430  (*this)[position+i] = v(i);
431  return true;
432  }
433 
437  const VectorOf<T> &operator=(T v)
438  {
439  std::fill(bytes.begin(), bytes.end(), v);
440  return *this;
441  }
442 
446  bool operator==(const VectorOf<T> &r) const
447  {
448  return bytes == r.bytes;
449  }
450 
454  iterator begin() noexcept {
455  return bytes.begin();
456  }
457 
461  iterator end() noexcept {
462  return bytes.end();
463  }
464 
468  const_iterator begin() const noexcept {
469  return bytes.begin();
470  }
471 
475  const_iterator end() const noexcept {
476  return bytes.end();
477  }
478 
482  const_iterator cbegin() const noexcept {
483  return bytes.cbegin();
484  }
485 
489  const_iterator cend() const noexcept {
490  return bytes.cend();
491  }
492  void clear() {
493  bytes.clear();
494  }
495 
496  yarp::os::Type getType() const override {
497  return yarp::os::Type::byName("yarp/vector");
498  }
499 };
500 
501 
502 #ifdef _MSC_VER
503 /*YARP_sig_EXTERN*/ template class YARP_sig_API yarp::sig::VectorOf<double>;
504 #endif
505 
506 #endif // YARP_SIG_VECTOR_H
#define BOTTLE_TAG_FLOAT64
Definition: Bottle.h:27
#define BOTTLE_TAG_INT32
Definition: Bottle.h:23
bool ret
#define yAssert(x)
Definition: Log.h:297
int BottleTagMap()
Definition: Vector.h:88
int BottleTagMap< int >()
Definition: Vector.h:100
int BottleTagMap< double >()
Definition: Vector.h:95
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:29
static Type byName(const char *name)
Definition: Type.cpp:174
A Base class for a VectorOf<T>, provide default implementation for read/write methods.
Definition: Vector.h:55
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:77
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition: Vector.cpp:54
virtual std::string getFormatStr(int tag) const
Definition: Vector.cpp:98
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:362
T & operator()(size_t i)
Single element access, no range check.
Definition: Vector.h:311
T & emplace_back(_Args &&... args)
Construct a new element in the vector: size is changed.
Definition: Vector.h:273
size_t getListSize() const override
Definition: Vector.h:176
VectorOf(std::initializer_list< T > values)
Initializer list constructor.
Definition: Vector.h:139
T & operator[](size_t i)
Single element access, no range check.
Definition: Vector.h:291
const T & operator()(size_t i) const
Single element access, no range check, const version.
Definition: Vector.h:321
void resize(size_t size) override
Resize the vector.
Definition: Vector.h:225
yarp::os::Type getType() const override
Definition: Vector.h:496
int getBottleTag() const override
Definition: Vector.h:172
void push_back(T &&elem)
Move a new element in the vector: size is changed.
Definition: Vector.h:262
const T * data() const
Return a pointer to the first element of the vector, const version.
Definition: Vector.h:218
size_t size() const
Definition: Vector.h:326
void push_back(const T &elem)
Push a new element in the vector: size is changed.
Definition: Vector.h:253
iterator begin() noexcept
Returns an iterator to the beginning of the VectorOf.
Definition: Vector.h:454
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:446
const_iterator cend() const noexcept
Returns a const iterator to the end of the VectorOf.
Definition: Vector.h:489
const T & operator[](size_t i) const
Single element access, no range check, const version.
Definition: Vector.h:301
const T * getFirst() const
Definition: Vector.h:193
~VectorOf() override=default
VectorOf(VectorOf< T > &&other) noexcept=default
T * data()
Return a pointer to the first element of the vector.
Definition: Vector.h:210
char * getMemoryBlock() override
Definition: Vector.h:186
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:404
void resize(size_t size, const T &def)
Resize the vector and initilize the element to a default value.
Definition: Vector.h:235
const_iterator begin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
Definition: Vector.h:468
const_iterator end() const noexcept
Returns a const iterator to the end of the VectorOf.
Definition: Vector.h:475
size_t capacity() const
capacity
Definition: Vector.h:341
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:482
VectorOf(size_t size)
Definition: Vector.h:132
const char * getMemoryBlock() const override
Definition: Vector.h:181
size_t length() const
Get the length of the vector.
Definition: Vector.h:334
VectorOf(size_t s, const T &def)
Build a vector and initialize it with def.
Definition: Vector.h:147
VectorOf(const VectorOf &r)=default
void pop_back()
Pop an element out of the vector: size is changed.
Definition: Vector.h:281
typename std::vector< T >::const_iterator const_iterator
Definition: Vector.h:128
const VectorOf< T > & operator=(T v)
Set all elements of the vector to a scalar.
Definition: Vector.h:437
iterator end() noexcept
Returns an iterator to the end of the VectorOf.
Definition: Vector.h:461
void zero()
Zero the elements of the vector.
Definition: Vector.h:348
VectorOf(size_t s, const T *p)
Builds a vector and initialize it with values from 'p'.
Definition: Vector.h:156
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:246
size_t getElementSize() const override
Definition: Vector.h:168
typename std::vector< T >::iterator iterator
Definition: Vector.h:127
bool setSubvector(int position, const VectorOf< T > &v)
Set a portion of this vector with the values of the specified vector.
Definition: Vector.h:425
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2883
VectorOf< double > Vector
Definition: Vector.h:33
The main, catch-all namespace for YARP.
Definition: environment.h:25
#define YARP_sig_API
Definition: api.h:19