OpenASIP  2.0
DataObject.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file DataObject.cc
26  *
27  * Definition of DataObject class.
28  *
29  * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30  * @author Pekka Jääskeläinen 2006 (pjaaskel-no.spam-cs.tut.fi)
31  */
32 
33 #include <string>
34 using std::string;
35 
36 #include "DataObject.hh"
37 #include "Conversion.hh"
38 #include "Application.hh"
39 #include "StringTools.hh"
40 
41 /**
42  * Constructor.
43  *
44  * Creates an DataObject which represents an empty string.
45  */
47  type_(TYPE_STRING), intFresh_(false), stringFresh_(true),
48  doubleFresh_(false), floatFresh_(false), intValue_(0), stringValue_(""),
49  doubleValue_(0), floatValue_(0) {
50 }
51 
52 /**
53  * Constructor for integer data.
54  *
55  * @param value Value as integer.
56  */
58  setInteger(value);
59 }
60 
61 /**
62  * Constructor for integer data.
63  *
64  * @param value Value as integer.
65  */
67  setLong(value);
68 }
69 
70 /**
71  * Constructor for double data.
72  *
73  * @param value Value as double.
74  */
75 DataObject::DataObject(double value) {
76  setDouble(value);
77 }
78 
79 /**
80  * Constructor for string data.
81  *
82  * @param value Value as string.
83  */
84 DataObject::DataObject(const std::string value) {
85  setString(value);
86 }
87 
88 /**
89  * Destructor.
90  */
92 }
93 
94 /**
95  * Sets the integer value of DataObject.
96  *
97  * @param value The integer value.
98  */
99 void
101  type_ = TYPE_INT;
102  intFresh_ = true;
103  stringFresh_ = false;
104  doubleFresh_ = false;
105  floatFresh_ = false;
106  intValue_ = value;
107 }
108 
109 /**
110  * Sets the integer value of DataObject.
111  *
112  * @param value The integer value.
113  */
114 void
116  type_ = TYPE_INT;
117  intFresh_ = true;
118  stringFresh_ = false;
119  doubleFresh_ = false;
120  floatFresh_ = false;
121  intValue_ = value;
122 }
123 
124 /**
125  * Sets the string value of DataObject.
126  *
127  * @param value The string value.
128  */
129 void
130 DataObject::setString(std::string value) {
131  type_ = TYPE_STRING;
132  stringFresh_ = true;
133  intFresh_ = false;
134  doubleFresh_ = false;
135  floatFresh_ = false;
136  stringValue_ = value;
137 }
138 
139 /**
140  * Sets the double value of DataObject.
141  *
142  * @param value The double value of DataObject.
143  */
144 void
145 DataObject::setDouble(double value) {
146  type_ = TYPE_DOUBLE;
147  doubleFresh_ = true;
148  intFresh_ = false;
149  stringFresh_ = false;
150  floatFresh_ = false;
151  doubleValue_ = value;
152 }
153 
154 /**
155  * Sets the float value of DataObject.
156  *
157  * @param value The float value of DataObject.
158  */
159 void
160 DataObject::setFloat(float value) {
161  type_ = TYPE_FLOAT;
162  floatFresh_ = true;
163  intFresh_ = false;
164  stringFresh_ = false;
165  doubleFresh_ = false;
166  floatValue_ = value;
167 }
168 
169 /**
170  * Sets the bool value of DataObject.
171  *
172  * @param value The bool value of DataObject.
173  */
174 void
175 DataObject::setBool(bool value) {
176  setInteger(value);
177 }
178 
179 /**
180  * Sets the DataObject to null.
181  *
182  */
183 void
185  type_ = TYPE_NULL;
186  intFresh_ = true;
187  intValue_ = 0;
188  stringFresh_ = true;
189  stringValue_ = "";
190 }
191 
192 
193 /**
194  * Returns the integer value of DataObject.
195  *
196  * If integer value is not available, the conversion is done from the
197  * original value type. Note that a NULL DataObject is converted to 0.
198  *
199  * @return The integer value of DataObject.
200  * @exception NumberFormatException If conversion fails or if DataObject is
201  * not initialized.
202  */
203 int
205  if (intFresh_) {
206  return intValue_;
207  } else {
208  try {
209  switch (type_) {
210  case TYPE_STRING:
212  break;
213  case TYPE_DOUBLE:
215  break;
216  case TYPE_FLOAT:
218  break;
219  case TYPE_NOTYPE: {
220  string method = "DataObject::integerValue()";
221  string message = "DataObject not initialized.";
222  throw NumberFormatException(
223  __FILE__, __LINE__, method, message);
224  break;
225  }
226  case TYPE_NULL:
227  intValue_ = 0;
228  break;
229  default:
230  break;
231  }
232  } catch (const NumberFormatException&) {
233 
234  // it can be an unsigned int, let's try to convert to
235  // unsigned int first
236  switch (type_) {
237  case TYPE_STRING:
239  break;
240  case TYPE_DOUBLE:
242  break;
243  case TYPE_FLOAT:
245  break;
246  case TYPE_NOTYPE: {
247  string method = "DataObject::integerValue()";
248  string message = "DataObject not initialized.";
249  throw NumberFormatException(
250  __FILE__, __LINE__, method, message);
251  break;
252  }
253  default:
254  break;
255  }
256  }
257  intFresh_ = true;
258  return intValue_;
259  }
260 }
261 
262 /**
263  * Returns the (long) integer value of DataObject.
264  *
265  * If integer value is not available, the conversion is done from the
266  * original value type. Note that a NULL DataObject is converted to 0.
267  *
268  * @return The integer value of DataObject.
269  * @exception NumberFormatException If conversion fails or if DataObject is
270  * not initialized.
271  */
272 SLongWord
274 
275  if (intFresh_) {
276  return intValue_;
277  } else {
278  try {
279  switch (type_) {
280  case TYPE_STRING:
282  break;
283  case TYPE_DOUBLE:
285  break;
286  case TYPE_FLOAT:
288  break;
289  case TYPE_NOTYPE: {
290  string method = "DataObject::integerValue()";
291  string message = "DataObject not initialized.";
292  throw NumberFormatException(
293  __FILE__, __LINE__, method, message);
294  break;
295  }
296  case TYPE_NULL:
297  intValue_ = 0;
298  break;
299  default:
300  break;
301  }
302  } catch (const NumberFormatException&) {
303 
304  // it can be an unsigned int, let's try to convert to
305  // unsigned int first
306  switch (type_) {
307  case TYPE_STRING:
309  break;
310  case TYPE_DOUBLE:
312  break;
313  case TYPE_FLOAT:
315  break;
316  case TYPE_NOTYPE: {
317  string method = "DataObject::integerValue()";
318  string message = "DataObject not initialized.";
319  throw NumberFormatException(
320  __FILE__, __LINE__, method, message);
321  break;
322  }
323  default:
324  break;
325  }
326  }
327  intFresh_ = true;
328  return intValue_;
329  }
330 }
331 
332 
333 /**
334  * Returns the string value of DataObject.
335  *
336  * If string value is not available, the conversion is done from the original
337  * data type. Note that a NULL DataObject is converted to an empty string.
338  *
339  * @return The string value of DataObject.
340  * @exception NumberFormatException If conversion fails or if DataObject is
341  * not initialized.
342  */
343 string
345  if (stringFresh_) {
346  return stringValue_;
347  } else {
348  switch (type_) {
349  case TYPE_INT:
351  break;
352  case TYPE_DOUBLE:
354  break;
355  case TYPE_FLOAT:
357  break;
358  case TYPE_NOTYPE: {
359  string method = "DataObject::stringValue()";
360  string message = "DataObject not initialized.";
361  throw NumberFormatException(__FILE__, __LINE__, method, message);
362  break;
363  }
364  case TYPE_NULL:
365  stringValue_ = "";
366  break;
367  default:
368  break;
369  }
370  stringFresh_ = true;
371  return stringValue_;
372  }
373 }
374 
375 /**
376  * Returns the double value of DataObject.
377  *
378  * If double value is not available, the conversion is done from the original
379  * data type. Note that a NULL DataObject is converted to 0.0.
380  *
381  * @return The double value of DataObject.
382  * @exception NumberFormatException If conversion fails or if DataObject is
383  * not initialized.
384  */
385 double
387  if (doubleFresh_) {
388  return doubleValue_;
389  } else {
390  switch (type_) {
391  case TYPE_STRING:
393  break;
394  case TYPE_INT:
396  break;
397  case TYPE_FLOAT:
399  break;
400  case TYPE_NOTYPE: {
401  string method = "DataObject::doubleValue()";
402  string message = "DataObject not initialized.";
403  throw NumberFormatException(__FILE__, __LINE__, method, message);
404  break;
405  }
406  case TYPE_NULL:
407  doubleValue_ = 0.0;
408  break;
409  default:
410  break;
411  }
412  doubleFresh_ = true;
413  return doubleValue_;
414  }
415 }
416 
417 /**
418  * Returns the float value of DataObject.
419  *
420  * If float value is not available, the conversion is done from the original
421  * data type. Note that a NULL DataObject is converted to 0.0.
422  *
423  * @return The float value of DataObject.
424  * @exception NumberFormatException If conversion fails or if DataObject is
425  * not initialized.
426  */
427 float
429  if (floatFresh_) {
430  return floatValue_;
431  } else {
432  switch (type_) {
433  case TYPE_STRING:
435  break;
436  case TYPE_INT:
438  break;
439  case TYPE_DOUBLE:
441  break;
442  case TYPE_NOTYPE: {
443  string method = "DataObject::floatValue()";
444  string message = "DataObject not initialized.";
445  throw NumberFormatException(__FILE__, __LINE__, method, message);
446  break;
447  }
448  case TYPE_NULL:
449  floatValue_ = 0.0;
450  break;
451  default:
452  break;
453  }
454  floatFresh_ = true;
455  return floatValue_;
456  }
457 }
458 
459 /**
460  * Returns the bool value of DataObject.
461  *
462  * The conversion is done from the original data type. The NULL value is
463  * represented as "false".
464  *
465  * @return The bool value of DataObject.
466  * @exception NumberFormatException If conversion fails or if DataObject is
467  * not initialized.
468  */
469 bool
471  if (type_ == TYPE_STRING) {
472  std::string strValue = StringTools::stringToLower(stringValue());
473  if (strValue == "true") {
474  return true;
475  } else if (strValue == "false") {
476  return false;
477  }
478  } else if (type_ == TYPE_NULL) {
479  return false;
480  }
481 
482  return integerValue();
483 }
484 
485 /**
486  * Returns true in case the object represents a NULL value.
487  *
488  * @return True in case the object's value is NULL.
489  */
490 bool
492  return type_ == TYPE_NULL;
493 }
494 
495 /**
496  * Tests the inequality of two DataObjects.
497  *
498  * @param object The object this object is compared to.
499  * @return True if the two objects are inequal.
500  * @exception NumberFormatException If conversion fails.
501  */
502 bool
503 DataObject::operator!=(const DataObject& object) const {
504  if (object.type_ != type_)
505  return true;
506 
507  if (type_ == TYPE_NULL)
508  return false; // the type_ is the value as itself
509 
510  string value1 = stringValue();
511  string value2 = object.stringValue();
512  return value1 != value2;
513 }
514 
515 ///////////////////////////////////////////////////////////////////////////////
516 // NullDataObject
517 ///////////////////////////////////////////////////////////////////////////////
518 
519 #include <iostream>
520 
522 
523 /**
524  * Constructor.
525  */
527 }
528 
529 /**
530  * Destructor.
531  */
533 }
534 
535 
536 /**
537  * Returns an instance of NullDataObject (singleton)
538  *
539  * @return NullDataObject singleton instance.
540  */
543  if (instance_ == NULL) {
544  instance_ = new NullDataObject();
545  }
546  return *instance_;
547 }
548 
549 
550 /**
551  * All method implementations of NullDataObject write a message to error log
552  * and abort the program.
553  */
554 void
556  abortWithError("setInteger()");
557 }
558 
559 void
561  abortWithError("setLong()");
562 }
563 
564 void
566  abortWithError("setString()");
567 }
568 
569 void
571  abortWithError("setDouble()");
572 }
573 
574 void
576  abortWithError("setFloat()");
577 }
578 
579 void
581  abortWithError("setNull()");
582 }
583 
584 int
586  abortWithError("integerValue()");
587  return 0;
588 }
589 
590 SLongWord
592  abortWithError("longValue()");
593  return 0;
594 }
595 
596 string
598  abortWithError("stringValue()");
599  return "";
600 }
601 
602 double
604  abortWithError("doubleValue()");
605  return 0.0;
606 }
607 
608 float
610  abortWithError("floatValue()");
611  return 0.0;
612 }
613 
614 bool
616  abortWithError("isNull()");
617  return false;
618 }
NullDataObject::setString
virtual void setString(std::string value) override
Definition: DataObject.cc:565
DataObject::TYPE_STRING
@ TYPE_STRING
Definition: DataObject.hh:58
NullDataObject::setNull
virtual void setNull() override
Definition: DataObject.cc:580
NumberFormatException
Definition: Exception.hh:421
DataObject::setDouble
virtual void setDouble(double value)
Definition: DataObject.cc:145
DataObject::longValue
virtual SLongWord longValue() const
Definition: DataObject.cc:273
DataObject
Definition: DataObject.hh:50
DataObject::TYPE_INT
@ TYPE_INT
Definition: DataObject.hh:57
DataObject::stringValue
virtual std::string stringValue() const
Definition: DataObject.cc:344
DataObject::type_
OrigType type_
The type of value in which DataObject was last assigned.
Definition: DataObject.hh:104
DataObject::TYPE_NOTYPE
@ TYPE_NOTYPE
Definition: DataObject.hh:62
DataObject::~DataObject
virtual ~DataObject()
Definition: DataObject.cc:91
DataObject::setInteger
virtual void setInteger(int value)
Definition: DataObject.cc:115
DataObject::floatValue
virtual float floatValue() const
Definition: DataObject.cc:428
NullDataObject
Definition: DataObject.hh:128
DataObject::floatFresh_
bool floatFresh_
Flag indicating that the value of float is up-to-date.
Definition: DataObject.hh:112
NullDataObject::stringValue
virtual std::string stringValue() const
Definition: DataObject.cc:597
NullDataObject::floatValue
virtual float floatValue() const
Definition: DataObject.cc:609
Conversion::toString
static std::string toString(const T &source)
DataObject::intFresh_
bool intFresh_
Flag indicating that the value of integer is up-to-date.
Definition: DataObject.hh:106
DataObject::floatValue_
float floatValue_
Value as float.
Definition: DataObject.hh:120
DataObject::TYPE_DOUBLE
@ TYPE_DOUBLE
Definition: DataObject.hh:59
DataObject::doubleValue
virtual double doubleValue() const
Definition: DataObject.cc:386
StringTools.hh
Conversion::toDouble
static double toDouble(const T &source)
DataObject::integerValue
virtual int integerValue() const
Definition: DataObject.cc:204
abortWithError
#define abortWithError(message)
Definition: Application.hh:72
NullDataObject::setFloat
virtual void setFloat(float value) override
Definition: DataObject.cc:575
DataObject::setLong
virtual void setLong(SLongWord value)
Definition: DataObject.cc:100
Conversion.hh
DataObject::setFloat
virtual void setFloat(float value)
Definition: DataObject.cc:160
DataObject::intValue_
SLongWord intValue_
Value as integer.
Definition: DataObject.hh:114
Application.hh
DataObject::isNull
virtual bool isNull() const
Definition: DataObject.cc:491
NullDataObject::setLong
virtual void setLong(SLongWord value) override
Definition: DataObject.cc:560
DataObject.hh
DataObject::stringFresh_
bool stringFresh_
Flag indicating that the value of string is up-to-date.
Definition: DataObject.hh:108
DataObject::TYPE_FLOAT
@ TYPE_FLOAT
Definition: DataObject.hh:60
DataObject::setNull
virtual void setNull()
Definition: DataObject.cc:184
Conversion::toUnsignedInt
static unsigned int toUnsignedInt(const T &source)
DataObject::doubleFresh_
bool doubleFresh_
Flag indicating that the value of double is up-to-date.
Definition: DataObject.hh:110
DataObject::TYPE_NULL
@ TYPE_NULL
Definition: DataObject.hh:61
NullDataObject::NullDataObject
NullDataObject()
Definition: DataObject.cc:526
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
DataObject::operator!=
virtual bool operator!=(const DataObject &object) const
Definition: DataObject.cc:503
NullDataObject::setInteger
virtual void setInteger(int value) override
Definition: DataObject.cc:555
NullDataObject::integerValue
virtual int integerValue() const override
Definition: DataObject.cc:585
NullDataObject::longValue
virtual SLongWord longValue() const override
Definition: DataObject.cc:591
DataObject::stringValue_
std::string stringValue_
Value as string.
Definition: DataObject.hh:116
NullDataObject::instance_
static NullDataObject * instance_
Definition: DataObject.hh:156
Conversion::toLong
static SLongWord toLong(const T &source)
DataObject::setBool
virtual void setBool(bool value)
Definition: DataObject.cc:175
Conversion::toInt
static int toInt(const T &source)
NullDataObject::doubleValue
virtual double doubleValue() const
Definition: DataObject.cc:603
NullDataObject::~NullDataObject
virtual ~NullDataObject()
Definition: DataObject.cc:532
DataObject::boolValue
virtual bool boolValue() const
Definition: DataObject.cc:470
DataObject::setString
virtual void setString(std::string value)
Definition: DataObject.cc:130
NullDataObject::setDouble
virtual void setDouble(double value) override
Definition: DataObject.cc:570
NullDataObject::isNull
virtual bool isNull() const
Definition: DataObject.cc:615
DataObject::doubleValue_
double doubleValue_
Value as double.
Definition: DataObject.hh:118
Conversion::toUnsignedLong
static ULongWord toUnsignedLong(const T &source)
SLongWord
long SLongWord
Definition: BaseType.hh:52
NullDataObject::instance
static NullDataObject & instance()
Definition: DataObject.cc:542
StringTools::stringToLower
static std::string stringToLower(const std::string &source)
Definition: StringTools.cc:160
DataObject::DataObject
DataObject()
Definition: DataObject.cc:46
Conversion::toFloat
static float toFloat(const T &source)