OMNeT++ API 6.1
Discrete Event Simulation Library
cvaluearray.h
1 //==========================================================================
2 // CVALUEARRAY.H - part of
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_CVALUEARRAY_H
17 #define __OMNETPP_CVALUEARRAY_H
18 
19 #include <string>
20 #include <vector>
21 #include "cvalue.h"
22 #include "cvaluecontainer.h"
23 
24 
25 namespace omnetpp {
26 
37 class SIM_API cValueArray : public cValueContainer
38 {
39  private:
40  std::vector<cValue> array;
41 
42  private:
43  void cannotCast(cObject *obj, const char *toClass) const;
44  void copy(const cValueArray& other);
45 
46  public:
49 
58  cValueArray(const cValueArray& other) : cValueContainer(other) {copy(other);}
59 
64  explicit cValueArray(const char *name=nullptr, int capacity=0) : cValueContainer(name), array(capacity) {}
65 
70  virtual ~cValueArray();
71 
78  cValueArray& operator=(const cValueArray& other);
80 
83 
93  virtual cValueArray *dup() const override {return new cValueArray(*this);}
94 
99  virtual std::string str() const override;
100 
105  virtual void forEachChild(cVisitor* v) override;
107 
110 
114  virtual int size() const {return array.size();}
115 
119  virtual void clear();
120 
125  virtual int getCapacity() const {return array.capacity();}
126 
131  virtual void setCapacity(int capacity) {array.reserve(capacity);}
132 
136  virtual void add(const cValue& value);
137 
142  virtual void insert(int k, const cValue& value);
143 
147  const virtual cValue& get(int k) const;
148 
152  void set(int k, const cValue& value);
153 
157  const cValue& operator[](int k) const {return get(k);}
158 
163  virtual void erase(int k);
164 
169  virtual cValue remove(int k);
170 
174  const std::vector<cValue>& getArray() const {return array;}
176 
179 
184  std::vector<bool> asBoolVector() const;
185 
190  std::vector<intval_t> asIntVector() const;
191 
196  std::vector<intval_t> asIntVectorInUnit(const char *targetUnit) const;
197 
202  std::vector<double> asDoubleVector() const;
203 
208  std::vector<double> asDoubleVectorInUnit(const char *targetUnit) const;
209 
214  std::vector<std::string> asStringVector() const;
215 
220  template<typename T> std::vector<T*> asObjectVector() const;
222 };
223 
224 
225 template<typename T>
226 std::vector<T*> cValueArray::asObjectVector() const
227 {
228  std::vector<T*> result;
229  result.resize(array.size());
230  for (int i = 0; i < array.size(); i++) {
231  cObject *obj = array[i].objectValue();
232  result[i] = dynamic_cast<T*>(obj);
233  if (result[i] == nullptr && obj != nullptr)
234  cannotCast(obj, opp_typename(typeid(T)));
235  }
236  return result;
237 }
238 
239 } // namespace omnetpp
240 
241 #endif
242 
243 
omnetpp::cValueContainer
Common base class for cValue-based containers.
Definition: cvaluecontainer.h:32
omnetpp::cObject
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition: cobject.h:92
omnetpp::cValue
A variant-like value class used during evaluating NED expressions.
Definition: cvalue.h:47
omnetpp::cValueArray::asObjectVector
std::vector< T * > asObjectVector() const
Definition: cvaluearray.h:226
omnetpp::cVisitor
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
omnetpp::cValueArray::getArray
const std::vector< cValue > & getArray() const
Definition: cvaluearray.h:174
omnetpp::cValueArray
Container class based on std::vector<cValue> objects.
Definition: cvaluearray.h:37
omnetpp::cValueArray::size
virtual int size() const
Definition: cvaluearray.h:114
omnetpp::cValueArray::getCapacity
virtual int getCapacity() const
Definition: cvaluearray.h:125
omnetpp::cValueArray::cValueArray
cValueArray(const char *name=nullptr, int capacity=0)
Definition: cvaluearray.h:64
omnetpp::cValueArray::cValueArray
cValueArray(const cValueArray &other)
Definition: cvaluearray.h:58
omnetpp::cValueArray::setCapacity
virtual void setCapacity(int capacity)
Definition: cvaluearray.h:131
omnetpp::opp_typename
const SIM_API char * opp_typename(const std::type_info &t)
Returns the name of a C++ type, correcting the quirks of various compilers.
omnetpp::cValueArray::dup
virtual cValueArray * dup() const override
Definition: cvaluearray.h:93
omnetpp::cValueArray::operator[]
const cValue & operator[](int k) const
Definition: cvaluearray.h:157