OpenASIP 2.2
Loading...
Searching...
No Matches
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>
34using 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 */
69
70/**
71 * Constructor for double data.
72 *
73 * @param value Value as double.
74 */
76 setDouble(value);
77}
78
79/**
80 * Constructor for string data.
81 *
82 * @param value Value as string.
83 */
84DataObject::DataObject(const std::string value) {
85 setString(value);
86}
87
88/**
89 * Destructor.
90 */
93
94/**
95 * Sets the integer value of DataObject.
96 *
97 * @param value The integer value.
98 */
99void
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 */
114void
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 */
129void
130DataObject::setString(std::string value) {
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 */
144void
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 */
159void
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 */
174void
176 setInteger(value);
177}
178
179/**
180 * Sets the DataObject to null.
181 *
182 */
183void
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 */
203int
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.";
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.";
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 */
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.";
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.";
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 */
343string
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 */
385double
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 */
427float
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 */
469bool
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 */
490bool
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 */
502bool
503DataObject::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 */
528
529/**
530 * Destructor.
531 */
534
535
536/**
537 * Returns an instance of NullDataObject (singleton)
538 *
539 * @return NullDataObject singleton instance.
540 */
543 if (instance_ == NULL) {
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 */
554void
556 abortWithError("setInteger()");
557}
558
559void
563
564void
566 abortWithError("setString()");
567}
568
569void
571 abortWithError("setDouble()");
572}
573
574void
576 abortWithError("setFloat()");
577}
578
579void
581 abortWithError("setNull()");
582}
583
584int
586 abortWithError("integerValue()");
587 return 0;
588}
589
592 abortWithError("longValue()");
593 return 0;
594}
595
596string
598 abortWithError("stringValue()");
599 return "";
600}
601
602double
604 abortWithError("doubleValue()");
605 return 0.0;
606}
607
608float
610 abortWithError("floatValue()");
611 return 0.0;
612}
613
614bool
616 abortWithError("isNull()");
617 return false;
618}
#define abortWithError(message)
long SLongWord
Definition BaseType.hh:52
find Finds info of the inner loops in the false
static SLongWord toLong(const T &source)
static float toFloat(const T &source)
static double toDouble(const T &source)
static std::string toString(const T &source)
static int toInt(const T &source)
static ULongWord toUnsignedLong(const T &source)
static unsigned int toUnsignedInt(const T &source)
bool doubleFresh_
Flag indicating that the value of double is up-to-date.
OrigType type_
The type of value in which DataObject was last assigned.
double doubleValue_
Value as double.
virtual void setFloat(float value)
virtual void setLong(SLongWord value)
bool intFresh_
Flag indicating that the value of integer is up-to-date.
virtual void setNull()
bool stringFresh_
Flag indicating that the value of string is up-to-date.
virtual void setDouble(double value)
virtual float floatValue() const
virtual std::string stringValue() const
virtual void setBool(bool value)
virtual bool operator!=(const DataObject &object) const
float floatValue_
Value as float.
virtual bool isNull() const
SLongWord intValue_
Value as integer.
virtual double doubleValue() const
std::string stringValue_
Value as string.
virtual void setString(std::string value)
virtual bool boolValue() const
virtual int integerValue() const
bool floatFresh_
Flag indicating that the value of float is up-to-date.
virtual void setInteger(int value)
virtual SLongWord longValue() const
virtual ~DataObject()
Definition DataObject.cc:91
virtual void setString(std::string value) override
virtual double doubleValue() const
static NullDataObject * instance_
virtual void setLong(SLongWord value) override
virtual void setNull() override
static NullDataObject & instance()
virtual ~NullDataObject()
virtual float floatValue() const
virtual void setInteger(int value) override
virtual bool isNull() const
virtual void setFloat(float value) override
virtual void setDouble(double value) override
virtual std::string stringValue() const
virtual int integerValue() const override
virtual SLongWord longValue() const override
static std::string stringToLower(const std::string &source)