3
3
#include < algorithm>
4
4
#include < iostream>
5
5
#include < sstream>
6
+ #include < iomanip>
6
7
using namespace dataobject ;
7
8
using namespace std ;
8
9
10
+ namespace {
11
+ std::string doubleToStringWithPrecision (double value, int precision)
12
+ {
13
+ std::ostringstream out;
14
+ out << std::fixed << std::setprecision (precision) << value;
15
+ return out.str ();
16
+ };
17
+ }
18
+
9
19
// / Default dataobject is null
10
20
DataObject::DataObject () {}
11
21
@@ -20,13 +30,16 @@ DataObject::DataObject(DataType _type)
20
30
m_value = string ();
21
31
else if (_type == DataType::Integer)
22
32
m_value = 0 ;
33
+ else if (_type == DataType::Double)
34
+ m_value = (double )0.0 ;
23
35
else if (_type == DataType::Bool)
24
36
m_value = false ;
25
37
}
26
38
27
39
// / Define dataobject of string
28
40
DataObject::DataObject (std::string&& _str) : m_value(std::move(_str)) {}
29
- DataObject::DataObject (std::string const & _str) : m_value(_str) {}
41
+ DataObject::DataObject (std::string const & _str) : m_value(_str)
42
+ {}
30
43
31
44
// / Define dataobject[_key] = string
32
45
DataObject::DataObject (std::string&& _key, std::string&& _str)
@@ -38,14 +51,47 @@ DataObject::DataObject(std::string const& _key, std::string const& _str)
38
51
{}
39
52
40
53
DataObject::DataObject (std::string&& _key, int _val)
41
- : m_strKey(std::move(_key)), m_value(_val)
42
- {}
54
+ : m_strKey(std::move(_key))
55
+ {
56
+ setInt (_val);
57
+ }
43
58
44
59
// / Define dataobject of int
45
- DataObject::DataObject (int _int) : m_value(_int) {}
60
+ DataObject::DataObject (int _int)
61
+ {
62
+ setInt (_int);
63
+ }
64
+
65
+ DataObject::DataObject (DataType type, int _int)
66
+ {
67
+ _assert (type == DataType::Integer, " DataObject(DataType type, int _int) is not initialized as Integer" );
68
+ setInt (_int);
69
+ }
70
+
71
+ DataObject::DataObject (DataType type, double _double)
72
+ {
73
+ _assert (type == DataType::Double, " DataObject(DataType type, double _double) is not initialized as Double" );
74
+ setDouble (_double);
75
+ }
46
76
47
77
// / Define dataobject of bool
48
- DataObject::DataObject (DataType type, bool _bool) : m_value(_bool) { _assert (type == DataType::Bool); }
78
+ DataObject::DataObject (DataType type, SafeBool _bool)
79
+ {
80
+ string stype;
81
+ switch (type)
82
+ {
83
+ case DataType::Integer: stype = " INTEGER" ; break ;
84
+ case DataType::Array: stype = " Array" ; break ;
85
+ case DataType::Bool: stype = " Bool" ; break ;
86
+ case DataType::Double: stype = " Double" ; break ;
87
+ case DataType::NotInitialized: stype = " NotInit" ; break ;
88
+ case DataType::Null: stype = " Null" ; break ;
89
+ case DataType::String: stype = " String" ; break ;
90
+ case DataType::Object: stype = " Object" ; break ;
91
+ }
92
+ _assert (type == DataType::Bool, " DataObject(DataType type, bool _bool) is not initialized as Bool, requested " + stype);
93
+ setBool (_bool);
94
+ }
49
95
50
96
// / Get dataobject type
51
97
DataType DataObject::type () const
@@ -167,6 +213,7 @@ std::string const DataObject::asStringAnyway() const
167
213
{
168
214
case DataType::String: return asString ();
169
215
case DataType::Integer: return to_string (asInt ());
216
+ case DataType::Double: return doubleToStringWithPrecision (asDouble (), 4 );
170
217
case DataType::Bool: return asBool () ? " true" : " false" ;
171
218
case DataType::Object: return " Object" ;
172
219
case DataType::Array: return " Array" ;
@@ -184,6 +231,14 @@ int DataObject::asInt() const
184
231
return std::get<int >(m_value);
185
232
}
186
233
234
+ // / Get int value
235
+ double DataObject::asDouble () const
236
+ {
237
+ static const string c_errorAssert = " m_type == DataType::Double (DataObject::asDouble())" ;
238
+ _assert (type () == DataType::Double, c_errorAssert);
239
+ return std::get<double >(m_value);
240
+ }
241
+
187
242
// / Get bool value
188
243
bool DataObject::asBool () const
189
244
{
@@ -236,6 +291,9 @@ void DataObject::replace(DataObject const& _value)
236
291
case DataType::Integer:
237
292
m_value = _value.asInt ();
238
293
break ;
294
+ case DataType::Double:
295
+ m_value = _value.asDouble ();
296
+ break ;
239
297
case DataType::Bool:
240
298
m_value = _value.asBool ();
241
299
break ;
@@ -548,6 +606,17 @@ std::string DataObject::asJson(int level, bool pretty, bool nokey) const
548
606
}
549
607
out << " \" " << buffer << " \" " ;
550
608
break ;
609
+ case DataType::Double:
610
+ printLevel ();
611
+ if (!m_strKey.empty () && !nokey)
612
+ {
613
+ if (pretty)
614
+ out << " \" " << m_strKey << " \" : " ;
615
+ else
616
+ out << " \" " << m_strKey << " \" :" ;
617
+ }
618
+ out << doubleToStringWithPrecision (std::get<double >(m_value), 4 );
619
+ break ;
551
620
case DataType::Integer:
552
621
printLevel ();
553
622
if (!m_strKey.empty () && !nokey)
@@ -557,7 +626,7 @@ std::string DataObject::asJson(int level, bool pretty, bool nokey) const
557
626
else
558
627
out << " \" " << m_strKey << " \" :" ;
559
628
}
560
- out << std::get<int >(m_value);;
629
+ out << std::get<int >(m_value);
561
630
break ;
562
631
case DataType::Bool:
563
632
printLevel ();
@@ -588,6 +657,8 @@ std::string DataObject::dataTypeAsString(DataType _type)
588
657
return " string" ;
589
658
case Integer:
590
659
return " int" ;
660
+ case Double:
661
+ return " double" ;
591
662
case Array:
592
663
return " array" ;
593
664
case Bool:
@@ -699,11 +770,18 @@ void DataObject::setInt(int _value)
699
770
m_value = _value;
700
771
}
701
772
773
+ void DataObject::setDouble (double _value)
774
+ {
775
+ static const string c_assert = " In DataObject=(double) DataObject must be double or NotInitialized!" ;
776
+ _assert (type () == DataType::Double || type () == DataType::NotInitialized, c_assert);
777
+ m_value = _value;
778
+ }
779
+
702
780
void DataObject::setBool (bool _value)
703
781
{
704
782
static const string c_assert = " In DataObject:setBool(bool) DataObject must be bool or NotInitialized!" ;
705
783
_assert (type () == DataType::Bool || type () == DataType::NotInitialized, c_assert);
706
- m_value = _value;
784
+ m_value = ( bool ) _value;
707
785
}
708
786
709
787
void DataObject::copyFrom (DataObject const & _other)
@@ -716,6 +794,7 @@ void DataObject::copyFrom(DataObject const& _other)
716
794
{
717
795
case String: m_value = _other.asString (); break ;
718
796
case Integer: m_value = _other.asInt (); break ;
797
+ case Double: m_value = _other.asDouble (); break ;
719
798
case Bool: m_value = _other.asBool (); break ;
720
799
case Array:
721
800
_initArray (DataType::Array);
@@ -747,6 +826,7 @@ spDataObject DataObject::copy() const
747
826
{
748
827
case String: (*c).setString (string (asString ())); break ;
749
828
case Integer: (*c).setInt (asInt ()); break ;
829
+ case Double: (*c).setDouble (asDouble ()); break ;
750
830
case Bool: (*c).setBool (asBool ()); break ;
751
831
case Array:
752
832
for (size_t i = 0 ; i < getSubObjects ().size (); i++)
@@ -810,6 +890,9 @@ bool DataObject::operator==(DataObject const& _value) const
810
890
case DataType::Integer:
811
891
equal = asInt () == _value.asInt ();
812
892
break ;
893
+ case DataType::Double:
894
+ equal = asStringAnyway () == _value.asStringAnyway ();
895
+ break ;
813
896
case DataType::String:
814
897
equal = asString () == _value.asString ();
815
898
break ;
@@ -843,12 +926,24 @@ bool DataObject::operator==(bool _value) const
843
926
return *this == tmp;
844
927
}
845
928
929
+ DataObject& DataObject::operator =(size_t _value)
930
+ {
931
+ setInt (_value);
932
+ return *this ;
933
+ }
934
+
846
935
DataObject& DataObject::operator =(int _value)
847
936
{
848
937
setInt (_value);
849
938
return *this ;
850
939
}
851
940
941
+ DataObject& DataObject::operator =(double _value)
942
+ {
943
+ setDouble (_value);
944
+ return *this ;
945
+ }
946
+
852
947
DataObject& DataObject::operator [](std::string const & _key)
853
948
{
854
949
static const string c_assert = " m_type == DataType::NotInitialized || m_type == DataType::Object (DataObject& operator[])" ;
0 commit comments