torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Value.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 #ifndef TORC_GENERIC_VALUE_HPP
17 #define TORC_GENERIC_VALUE_HPP
18 
19 //BOOST
20 #include <boost/variant.hpp>
21 
22 #ifdef GENOM_SERIALIZATION
23 #include <boost/serialization/access.hpp>
24 #endif //GENOM_SERIALIZATION
25 #include "torc/generic/Error.hpp"
26 
27 namespace torc {
28 namespace generic {
29 
30 /**
31  * Represents a Value in EDIF. Values are used in different regions in EDIF such as in properties,
32  * parameters etc.
33  */
34 class Value {
35 #ifdef GENOM_SERIALIZATION
36  friend class boost::serialization::access;
37 #endif
38 public:
39  /**
40  * @enum Type
41  *
42  * Defines the type of inSource stored in this Value object.
43  */
44  enum Type {
47  };
48  /**
49  * Boolean Type
50  */
51  typedef bool Boolean;
52 
53  /**
54  * 32 bit Signed Integer type.
55  */
56  typedef int32_t Integer;
57 
58  /**
59  * Defines a non-unicode standard string.
60  */
62 
63  /**
64  * Represents a decimal number. This is used to represent decimal floating point numbers in
65  * EDIF. This has a exponent and mantissa part.
66  */
67  class Number {
68 #ifdef GENOM_SERIALIZATION
69  friend class boost::serialization::access;
70 #endif
71 
72  private:
73  int32_t mMantissa;
74  int32_t mExponent;
75 
76  public:
77  Number();
78 
79  Number(int32_t inMantissa, int32_t inExponent);
80 
81  ~Number() throw ();
82 
83  Number(const Number& inSource);
84 
85  Number& operator=(const Number& inSource);
86 
87  bool operator <(const Number& inRhs) const;
88 
89  bool operator ==(const Number& inRhs) const;
90 
91  bool operator >(const Number& inRhs) const;
92 
93  bool operator >=(const Number& inRhs) const;
94 
95  bool operator <=(const Number& inRhs) const;
96 
97  bool operator !=(const Number& inRhs) const;
98 
99  inline const int32_t getMantissa() const;
100 
101  void setMantissa(const int32_t& inSource);
102 
103  inline const int32_t getExponent() const;
104 
105  void setExponent(const int32_t& inSource);
106 
107  double eval() const;
108 
109  private:
110 #ifdef GENOM_SERIALIZATION
111  template <class Archive> void serialize(Archive& ar, unsigned int) {
112  ar & mExponent;
113  ar & mMantissa;
114  }
115 #endif //GENOM_SERIALIZATION
116  };
117 
118  /**
119  * Represents an EDIF MiNoMax inSource. This is basically a ranged integer. As such it stores
120  * the minimum, nominal and maximum values.
121  */
122  class MiNoMax {
123 #ifdef GENOM_SERIALIZATION
124  friend class boost::serialization::access;
125 #endif
126  private:
132 
133  public:
134  MiNoMax();
135 
136  MiNoMax(Value::Number inMin, Value::Number inNominal, Value::Number inMax);
137 
138  ~MiNoMax() throw ();
139 
140  MiNoMax(const MiNoMax& inSource);
141 
142  MiNoMax& operator=(const MiNoMax& inSource);
143 
144  bool operator <(const MiNoMax& inRhs) const;
145 
146  bool operator ==(const MiNoMax& inRhs) const;
147 
148  bool operator !=(const MiNoMax& inRhs) const;
149 
150  bool operator >(const MiNoMax& inRhs) const;
151 
152  bool operator >=(const MiNoMax& inRhs) const;
153 
154  bool operator <=(const MiNoMax& inRhs) const;
155 
156  void setMax(const Value::Number& inSource);
157 
158  inline const Value::Number getMax() const;
159 
160  inline const bool getMinUndefined() const;
161 
162  inline const bool getMaxUndefined() const;
163 
164  inline const Value::Number getMin() const;
165 
166  void setMin(const Value::Number& inSource);
167 
168  inline const Value::Number getNominal() const;
169 
170  void setNominal(const Value::Number& inSource);
171 
172 #ifdef GENOM_SERIALIZATION
173  template <class Archive> void serialize(Archive& ar, unsigned int) {
174  ar & mMin;
175  ar & mMinUndefined;
176  ar & mNominal;
177  ar & mMax;
178  ar & mMaxUndefined;
179  }
180 #endif //GENOM_SERIALIZATION
181  };
182 
183  /**
184  * Represents a point in cartesian coordinates. This stores the X and Y coordinates.
185  */
186  class Point {
187 #ifdef GENOM_SERIALIZATION
188  friend class boost::serialization::access;
189 #endif
190 
191  private:
192  int32_t mX;
193  int32_t mY;
194 
195  public:
196  Point();
197 
198  Point(int32_t inX, int32_t inY);
199 
200  ~Point() throw ();
201 
202  Point(const Point& inSource);
203 
204  Point& operator=(const Point& inSource);
205 
206  bool operator <(const Point& inRhs) const;
207 
208  bool operator ==(const Point& inRhs) const;
209 
210  bool operator !=(const Point& inRhs) const;
211 
212  bool operator >(const Point& inRhs) const;
213 
214  bool operator >=(const Point& inRhs) const;
215 
216  bool operator <=(const Point& inRhs) const;
217 
218  inline const int32_t getX() const;
219 
220  void setX(const int32_t& inSource);
221 
222  inline const int32_t getY() const;
223 
224  void setY(const int32_t& inSource);
225 
226  private:
227 #ifdef GENOM_SERIALIZATION
228  template <class Archive> void serialize(Archive& ar, unsigned int) {
229  ar & mX;
230  ar & mY;
231  }
232 #endif //GENOM_SERIALIZATION
233  };
234 
235  Value();
236 
237  Value(Type type);
238 
239  template <typename _ValueType> Value(Type type, const _ValueType& inSource);
240 
241  ~Value() throw ();
242 
243  Value(const Value& inSource);
244 
245  Value& operator=(const Value& inSource);
246 
247  /**
248  * Get the inSource stored in this object.
249  *
250  * @note This needs to be called as: v.get<Value::Integer>();
251  *
252  * @return The inSource according to the specified type.
253  *
254  * @exception Error The stored an requested types are incompatible.
255  */
256  template <typename _ValueType> _ValueType get() const throw (Error);
257 
258  /**
259  * Set a inSource object.
260  *
261  * @note This needs to be called as: v.set<Value::Integer>(10);
262  *
263  * @param[in] inSource The inSource according to the specified type.
264  *
265  * @exception Error The requested type is wrong.
266  */
267  template <typename _ValueType> void set(const _ValueType& inSource) throw (Error);
268 
269  /**
270  * Get the type of inSource stored in this object.
271  *
272  * @return The type of object
273  */
274  inline const Type getType() const;
275 
276  /**
277  * Get the type of inSource stored in this object.
278  *
279  * @return The type of object
280  */
281  void setType(const Type& inSource);
282 
283  /**
284  * Get whether this object is defined with a inSource, or is undefined.
285  *
286  * @return True if initialized.
287  */
288  inline const bool getIsSet() const;
289 
290  /**
291  * Set whether this object is defined with a inSource, or is undefined.
292  *
293  * @param[in] inSource True if initialized.
294  */
295  void setIsSet(const bool& inSource);
296 
297 private:
298 #ifdef GENOM_SERIALIZATION
299  template <class Archive> void serialize(Archive& ar, unsigned int);
300 #endif //GENOM_SERIALIZATION
304  bool mIsSet;
305 };
306 
307 inline const Value::Number Value::MiNoMax::getMax() const {
308  return mMax;
309 }
310 
311 inline const bool Value::MiNoMax::getMinUndefined() const {
312  return mMinUndefined;
313 }
314 
315 inline const bool Value::MiNoMax::getMaxUndefined() const {
316  return mMaxUndefined;
317 }
318 
319 inline const Value::Number Value::MiNoMax::getMin() const {
320  return mMin;
321 }
322 
324  return mNominal;
325 }
326 
327 inline const int32_t Value::Number::getMantissa() const {
328  return mMantissa;
329 }
330 
331 inline const int32_t Value::Number::getExponent() const {
332  return mExponent;
333 }
334 
335 inline const int32_t Value::Point::getX() const {
336  return mX;
337 }
338 
339 inline const int32_t Value::Point::getY() const {
340  return mY;
341 }
342 
343 /**
344  * Get the type of inSource stored in this object.
345  *
346  * @return The type of object
347  */
348 inline const Value::Type Value::getType() const {
349  return mType;
350 }
351 
352 /**
353  * Get whether this object is defined with a inSource, or is undefined.
354  *
355  * @return True if initialized.
356  */
357 inline const bool Value::getIsSet() const {
358  return mIsSet;
359 }
360 
361 template <typename _ValueType> Value::Value(Value::Type type, const _ValueType& inSource) :
362  mType(type), mValue(inSource), mIsSet(true) {}
363 
364 /**
365  * Set a inSource object.
366  *
367  * @note This needs to be called as: v.set<Value::Integer>(10);
368  *
369  * @param[in] inSource The inSource according to the specified type.
370  *
371  * @exception Error The requested type is wrong.
372  */
373 template <typename _ValueType> void Value::set(const _ValueType& inSource) throw (Error) {
374  mValue = inSource;
375 }
376 
377 /**
378  * Get the inSource stored in this object.
379  *
380  * @note This needs to be called as: v.get<Value::Integer>();
381  *
382  * @return The inSource according to the specified type.
383  *
384  * @exception Error The stored an requested types are incompatible.
385  */
386 template <typename _ValueType> _ValueType Value::get() const throw (Error) {
387  if(!mIsSet) {
388  Error e(eMessageIdErrorValueNotSet, __FUNCTION__, __FILE__, __LINE__);
389  e.saveContextData("Value set", mIsSet);
390  throw e;
391  }
392  _ValueType v;
393  try {
394  v = boost::get < _ValueType > (mValue);
395  } catch(const boost::bad_get& bg) {
396  Error e(eMessageIdErrorTypeCast, __FUNCTION__, __FILE__, __LINE__);
397  e.saveContextData("Value", mValue);
398  e.saveContextData("Casted Type", std::string("_ValueType"));
399  throw e;
400  }
401  return v;
402 }
403 
404 } // namespace generic
405 } // namespace torc
406 
407 #endif // TORC_GENERIC_VALUE_HPP
const bool getIsSet() const
Definition: Value.hpp:357
void setMantissa(const int32_t &inSource)
Definition: Value.cpp:134
double eval() const
Definition: Value.cpp:142
const Value::Number getNominal() const
Definition: Value.hpp:323
const bool getMinUndefined() const
Definition: Value.hpp:311
void setX(const int32_t &inSource)
Definition: Value.cpp:186
void setNominal(const Value::Number &inSource)
Definition: Value.cpp:88
void setMax(const Value::Number &inSource)
Definition: Value.cpp:78
void setMin(const Value::Number &inSource)
Definition: Value.cpp:83
std::string String
Definition: Value.hpp:61
std::string string
const int32_t getY() const
Definition: Value.hpp:339
const Value::Number getMax() const
Definition: Value.hpp:307
const int32_t getX() const
Definition: Value.hpp:335
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
void set(const _ValueType &inSource)
Definition: Value.hpp:373
const Type getType() const
Definition: Value.hpp:348
boost::variant< Value::Boolean, Value::Integer, Value::Number, Value::MiNoMax, Value::Point, Value::String > mValue
Definition: Value.hpp:303
void saveContextData(const std::string &inName, const boost::any &inSource)
Definition: Error.cpp:79
const Value::Number getMin() const
Definition: Value.hpp:319
const int32_t getExponent() const
Definition: Value.hpp:331
void setType(const Type &inSource)
Definition: Value.cpp:217
void setExponent(const int32_t &inSource)
Definition: Value.cpp:138
const int32_t getMantissa() const
Definition: Value.hpp:327
void setY(const int32_t &inSource)
Definition: Value.cpp:190
const bool getMaxUndefined() const
Definition: Value.hpp:315
_ValueType get() const
Definition: Value.hpp:386
void setIsSet(const bool &inSource)
Definition: Value.cpp:226