36 StateVariable::StateVariable(
const string& name,
string* ptrToString)
40 m_value.pstr = ptrToString;
44 StateVariable::StateVariable(
const string& name,
bool* ptrToVar)
48 m_value.pbool = ptrToVar;
52 StateVariable::StateVariable(
const string& name,
double* ptrToVar)
56 m_value.pdouble = ptrToVar;
60 StateVariable::StateVariable(
const string& name, uint8_t* ptrToVar)
64 m_value.puint8 = ptrToVar;
68 StateVariable::StateVariable(
const string& name, uint16_t* ptrToVar)
72 m_value.puint16 = ptrToVar;
76 StateVariable::StateVariable(
const string& name, uint32_t* ptrToVar)
80 m_value.puint32 = ptrToVar;
84 StateVariable::StateVariable(
const string& name, uint64_t* ptrToVar)
88 m_value.puint64 = ptrToVar;
92 StateVariable::StateVariable(
const string& name, int8_t* ptrToVar)
96 m_value.psint8 = ptrToVar;
100 StateVariable::StateVariable(
const string& name, int16_t* ptrToVar)
104 m_value.psint16 = ptrToVar;
108 StateVariable::StateVariable(
const string& name, int32_t* ptrToVar)
112 m_value.psint32 = ptrToVar;
116 StateVariable::StateVariable(
const string& name, int64_t* ptrToVar)
120 m_value.psint64 = ptrToVar;
128 m_value.phandler = ptrToHandler;
144 string StateVariable::GetTypeString()
const
179 if (m_type != otherVariable.m_type)
184 *m_value.pstr = *otherVariable.m_value.
pstr;
187 *m_value.pbool = *otherVariable.m_value.
pbool;
190 *m_value.pdouble = *otherVariable.m_value.
pdouble;
193 *m_value.puint8 = *otherVariable.m_value.
puint8;
196 *m_value.puint16 = *otherVariable.m_value.
puint16;
199 *m_value.puint32 = *otherVariable.m_value.
puint32;
202 *m_value.puint64 = *otherVariable.m_value.
puint64;
205 *m_value.psint8 = *otherVariable.m_value.
psint8;
208 *m_value.psint16 = *otherVariable.m_value.
psint16;
211 *m_value.psint32 = *otherVariable.m_value.
psint32;
214 *m_value.psint64 = *otherVariable.m_value.
psint64;
217 m_value.phandler->CopyValueFrom(otherVariable.m_value.
phandler);
235 return m_value.pstr == NULL?
"" : *m_value.pstr;
237 sstr << (*m_value.pbool?
"true" :
"false");
240 sstr << *m_value.pdouble;
243 sstr << (int) *m_value.puint8;
246 sstr.flags(std::ios::hex | std::ios::showbase);
247 sstr << *m_value.puint16;
250 sstr.flags(std::ios::hex | std::ios::showbase);
251 sstr << *m_value.puint32;
254 sstr.flags(std::ios::hex | std::ios::showbase);
255 sstr << *m_value.puint64;
258 sstr << (int) *m_value.psint8;
261 sstr << *m_value.psint16;
264 sstr << *m_value.psint32;
267 sstr << *m_value.psint64;
292 return (*m_value.pbool)? 1 : 0;
294 return (uint64_t) *m_value.pdouble;
296 return *m_value.puint8;
298 return *m_value.puint16;
300 return *m_value.puint32;
302 return *m_value.puint64;
304 return *m_value.psint8;
306 return *m_value.psint16;
308 return *m_value.psint32;
310 return *m_value.psint64;
316 std::cerr <<
"StateVariable::ToDouble(): Unimplemented type.\n";
317 throw std::exception();
333 return (*m_value.pbool)? 1.0 : 0.0;
335 return *m_value.pdouble;
337 return *m_value.puint8;
339 return *m_value.puint16;
341 return *m_value.puint32;
343 return *m_value.puint64;
345 return *m_value.psint8;
347 return *m_value.psint16;
349 return *m_value.psint32;
351 return *m_value.psint64;
357 std::cerr <<
"StateVariable::ToDouble(): Unimplemented type.\n";
358 throw std::exception();
374 m_value.phandler->Serialize(ss);
385 ss << context.
Tabs() << GetTypeString() <<
" " << m_name +
" ";
391 string StateVariable::EvaluateExpression(
const string& expression,
396 string result = expression;
399 while (result.size() > 0 && result[0] ==
' ')
400 result.erase((
size_t) 0);
401 while (result.size() > 0 && result[result.size() - 1] ==
' ')
402 result.erase(result.size()-1);
415 if (m_value.pstr == NULL)
419 bool success =
false;
420 string value = EvaluateExpression(expression, success);
431 *m_value.pstr = newStr;
440 *m_value.pbool =
true;
441 else if (value ==
"false")
442 *m_value.pbool =
false;
454 if (isnan(doubleTmp) || isinf(doubleTmp))
456 *m_value.pdouble = doubleTmp;
465 if (tmp == tmp64 && !error)
466 *m_value.puint8 = tmp;
476 uint16_t tmp = tmp64;
477 if (tmp == tmp64 && !error)
478 *m_value.puint16 = tmp;
488 uint32_t tmp = tmp64;
489 if (tmp == tmp64 && !error)
490 *m_value.puint32 = tmp;
501 *m_value.puint64 = tmp64;
512 if (tmp == tmp64 && !error)
513 *m_value.psint8 = tmp;
524 if (tmp == tmp64 && !error)
525 *m_value.psint16 = tmp;
536 if (tmp == tmp64 && !error)
537 *m_value.psint32 = tmp;
548 *m_value.psint64 = tmp64;
555 return m_value.phandler->Deserialize(value);
559 std::cerr <<
"StateVariable::SetValue: Unimplemented type.\n";
560 throw std::exception();
568 if (m_value.pstr == NULL)
577 *m_value.pstr = ss.str();
582 *m_value.pbool = value != 0;
586 *m_value.pdouble = value;
590 *m_value.puint8 = value;
594 *m_value.puint16 = value;
598 *m_value.puint32 = value;
602 *m_value.puint64 = value;
606 *m_value.psint8 = value;
610 *m_value.psint16 = value;
614 *m_value.psint32 = value;
618 *m_value.psint64 = value;
626 std::cerr <<
"StateVariable::SetValue: Unimplemented type.\n";
627 throw std::exception();
637 static void Test_StateVariable_String_Construct()
639 string myString =
"hi";
644 var.GetName(),
"hello");
648 var.ToString(),
"hi");
651 static void Test_StateVariable_String_SetValue()
653 string myString =
"hi";
658 var.SetValue(
"value2") ==
false);
660 var.SetValue(
"\"value2\"") ==
true);
665 var.ToString(),
"value2");
670 static void Test_StateVariable_String_CopyValueFrom()
672 string myString1 =
"hi";
673 string myString2 =
"something";
679 var1.ToString(),
"hi");
681 var1.CopyValueFrom(var2);
684 var1.GetName(),
"hello");
688 var1.ToString(),
"something");
690 myString1,
"something");
693 static void Test_StateVariable_String_Serialize()
695 string hi =
"value world";
701 var.Serialize(ss, dummyContext);
703 ss.str(),
"string hello \"value world\"\n");
706 static void Test_StateVariable_String_Serialize_WithEscapes()
708 string s =
"a\\b\tc\nd\re\bf\"g'h";
714 var.Serialize(ss, dummyContext);
720 static void Test_StateVariable_Bool_Construct()
727 var.GetName(),
"hello");
731 var.ToString(),
"true");
734 static void Test_StateVariable_Bool_SetValue()
741 var.SetValue(
"false") ==
true);
746 var.ToString(),
"false");
751 var.SetValue(
"true") ==
true);
754 var.ToString(),
"true");
759 var.SetValue(
"hello") ==
false);
762 var.ToString(),
"true");
765 static void Test_StateVariable_Bool_CopyValueFrom()
767 bool myBool1 =
false;
774 var1.CopyValueFrom(var2) ==
true);
777 var1.GetName(),
"hello");
781 var1.ToString(),
"true");
785 string myString =
"hm";
789 var1.CopyValueFrom(var3) ==
false);
792 static void Test_StateVariable_Bool_Serialize()
800 var.Serialize(ss, dummyContext);
802 ss.str(),
"bool hello true\n");
806 var.Serialize(ss2, dummyContext);
809 ss2.str(),
"bool hello false\n");
812 static void Test_StateVariable_Numeric_Construct()
814 double varDouble = -12.345;
815 uint8_t varUInt8 = 223;
816 uint16_t varUInt16 = 55000;
817 uint32_t varUInt32 = 3000000001UL;
818 uint64_t varUInt64 = ((uint64_t) 0xfedc0102 << 32) | 0x03040506;
819 int8_t varSInt8 = -120;
820 int16_t varSInt16 = -22000;
821 int32_t varSInt32 = -1000000001;
822 int64_t varSInt64 = ((uint64_t) 0xfedc0102 << 32) | 0x03040506;
851 "0xfedc010203040506");
856 "-82189585047354106");
859 static void Test_StateVariable_Numeric_SetValue()
861 double varDouble = -12.345;
862 uint8_t varUInt8 = 223;
863 uint16_t varUInt16 = 55000;
864 uint32_t varUInt32 = 3000000001UL;
865 uint64_t varUInt64 = ((uint64_t) 0xfedc0102 << 32) | 0x03040506;
866 int8_t varSInt8 = -120;
867 int16_t varSInt16 = -22000;
868 int32_t varSInt32 = -1000000001;
869 int64_t varSInt64 = ((uint64_t) 0xfedc0102 << 32) | 0x03040506;
882 vuint8.SetValue(
"hello") ==
false);
886 vdouble.SetValue(
"100") ==
true);
890 vdouble.SetValue(
"-210.42") ==
true);
892 varDouble == -210.42);
894 vdouble.SetValue(
"1e-100") ==
true);
896 varDouble == 1e-100);
900 vuint8.SetValue(
"100") ==
true);
904 vuint8.SetValue(
"0x2f") ==
true);
908 vuint8.SetValue(
"300") ==
false);
912 vuint8.SetValue(
"-110") ==
false);
918 vsint8.SetValue(
"100") ==
true);
922 vsint8.SetValue(
"200") ==
false);
926 vsint8.SetValue(
"-210") ==
false);
930 vsint8.SetValue(
"-110") ==
true);
932 varSInt8, (uint64_t) -110);
934 vsint8.SetValue(
"-0x1a") ==
true);
936 varSInt8, (uint64_t) -0x1a);
944 UNITTEST(Test_StateVariable_String_Construct);
945 UNITTEST(Test_StateVariable_String_SetValue);
946 UNITTEST(Test_StateVariable_String_CopyValueFrom);
947 UNITTEST(Test_StateVariable_String_Serialize);
948 UNITTEST(Test_StateVariable_String_Serialize_WithEscapes);
951 UNITTEST(Test_StateVariable_Bool_Construct);
952 UNITTEST(Test_StateVariable_Bool_SetValue);
953 UNITTEST(Test_StateVariable_Bool_CopyValueFrom);
954 UNITTEST(Test_StateVariable_Bool_Serialize);
957 UNITTEST(Test_StateVariable_Numeric_Construct);
958 UNITTEST(Test_StateVariable_Numeric_SetValue);