Skip to content

Commit 9cd5d84

Browse files
committed
dataobject double
1 parent 1a5450e commit 9cd5d84

9 files changed

+182
-26
lines changed

CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ endif()
8484

8585
add_subdirectory(libdevcore)
8686
add_subdirectory(libdevcrypto)
87+
88+
set(DATA_YAML ON)
89+
add_compile_definitions(DATA_YAML="${DATA_YAML}")
8790
add_subdirectory(libdataobj)
91+
8892
if (${UNITTESTS})
8993
set(LIBSSZ ssz)
9094
add_subdirectory(libssz)

libdataobj/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ file(GLOB headers "*.h")
33

44
add_library(dataobj ${sources} ${headers})
55
target_include_directories(dataobj SYSTEM PRIVATE "../")
6-
target_link_libraries(dataobj PRIVATE yaml-cpp)
6+
if (DATA_YAML)
7+
target_link_libraries(dataobj PRIVATE yaml-cpp)
8+
endif()

libdataobj/ConvertYaml.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#ifdef DATA_YAML
12
#include "ConvertYaml.h"
23
#include <iostream>
34
using namespace std;
@@ -75,3 +76,4 @@ spDataObject ConvertYamlToData(YAML::Node const& _node, bool _sort)
7576
}
7677

7778
}//namespace
79+
#endif

libdataobj/ConvertYaml.h

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#ifdef DATA_YAML
23
#include "DataObject.h"
34
#include <yaml-cpp/yaml.h>
45

@@ -14,3 +15,4 @@ extern const std::string YML_INT_TAG;
1415
extern const std::string YML_BOOL_TAG;
1516
}
1617
}
18+
#endif

libdataobj/DataObject.cpp

+102-7
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@
33
#include <algorithm>
44
#include <iostream>
55
#include <sstream>
6+
#include <iomanip>
67
using namespace dataobject;
78
using namespace std;
89

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+
919
/// Default dataobject is null
1020
DataObject::DataObject() {}
1121

@@ -20,13 +30,16 @@ DataObject::DataObject(DataType _type)
2030
m_value = string();
2131
else if (_type == DataType::Integer)
2232
m_value = 0;
33+
else if (_type == DataType::Double)
34+
m_value = (double)0.0;
2335
else if (_type == DataType::Bool)
2436
m_value = false;
2537
}
2638

2739
/// Define dataobject of string
2840
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+
{}
3043

3144
/// Define dataobject[_key] = string
3245
DataObject::DataObject(std::string&& _key, std::string&& _str)
@@ -38,14 +51,47 @@ DataObject::DataObject(std::string const& _key, std::string const& _str)
3851
{}
3952

4053
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+
}
4358

4459
/// 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+
}
4676

4777
/// 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+
}
4995

5096
/// Get dataobject type
5197
DataType DataObject::type() const
@@ -167,6 +213,7 @@ std::string const DataObject::asStringAnyway() const
167213
{
168214
case DataType::String: return asString();
169215
case DataType::Integer: return to_string(asInt());
216+
case DataType::Double: return doubleToStringWithPrecision(asDouble(), 4);
170217
case DataType::Bool: return asBool() ? "true" : "false";
171218
case DataType::Object: return "Object";
172219
case DataType::Array: return "Array";
@@ -184,6 +231,14 @@ int DataObject::asInt() const
184231
return std::get<int>(m_value);
185232
}
186233

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+
187242
/// Get bool value
188243
bool DataObject::asBool() const
189244
{
@@ -236,6 +291,9 @@ void DataObject::replace(DataObject const& _value)
236291
case DataType::Integer:
237292
m_value = _value.asInt();
238293
break;
294+
case DataType::Double:
295+
m_value = _value.asDouble();
296+
break;
239297
case DataType::Bool:
240298
m_value = _value.asBool();
241299
break;
@@ -548,6 +606,17 @@ std::string DataObject::asJson(int level, bool pretty, bool nokey) const
548606
}
549607
out << "\"" << buffer << "\"";
550608
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;
551620
case DataType::Integer:
552621
printLevel();
553622
if (!m_strKey.empty() && !nokey)
@@ -557,7 +626,7 @@ std::string DataObject::asJson(int level, bool pretty, bool nokey) const
557626
else
558627
out << "\"" << m_strKey << "\":";
559628
}
560-
out << std::get<int>(m_value);;
629+
out << std::get<int>(m_value);
561630
break;
562631
case DataType::Bool:
563632
printLevel();
@@ -588,6 +657,8 @@ std::string DataObject::dataTypeAsString(DataType _type)
588657
return "string";
589658
case Integer:
590659
return "int";
660+
case Double:
661+
return "double";
591662
case Array:
592663
return "array";
593664
case Bool:
@@ -699,11 +770,18 @@ void DataObject::setInt(int _value)
699770
m_value = _value;
700771
}
701772

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+
702780
void DataObject::setBool(bool _value)
703781
{
704782
static const string c_assert = "In DataObject:setBool(bool) DataObject must be bool or NotInitialized!";
705783
_assert(type() == DataType::Bool || type() == DataType::NotInitialized, c_assert);
706-
m_value = _value;
784+
m_value = (bool)_value;
707785
}
708786

709787
void DataObject::copyFrom(DataObject const& _other)
@@ -716,6 +794,7 @@ void DataObject::copyFrom(DataObject const& _other)
716794
{
717795
case String: m_value = _other.asString(); break;
718796
case Integer: m_value = _other.asInt(); break;
797+
case Double: m_value = _other.asDouble(); break;
719798
case Bool: m_value = _other.asBool(); break;
720799
case Array:
721800
_initArray(DataType::Array);
@@ -747,6 +826,7 @@ spDataObject DataObject::copy() const
747826
{
748827
case String: (*c).setString(string(asString())); break;
749828
case Integer: (*c).setInt(asInt()); break;
829+
case Double: (*c).setDouble(asDouble()); break;
750830
case Bool: (*c).setBool(asBool()); break;
751831
case Array:
752832
for (size_t i = 0; i < getSubObjects().size(); i++)
@@ -810,6 +890,9 @@ bool DataObject::operator==(DataObject const& _value) const
810890
case DataType::Integer:
811891
equal = asInt() == _value.asInt();
812892
break;
893+
case DataType::Double:
894+
equal = asStringAnyway() == _value.asStringAnyway();
895+
break;
813896
case DataType::String:
814897
equal = asString() == _value.asString();
815898
break;
@@ -843,12 +926,24 @@ bool DataObject::operator==(bool _value) const
843926
return *this == tmp;
844927
}
845928

929+
DataObject& DataObject::operator=(size_t _value)
930+
{
931+
setInt(_value);
932+
return *this;
933+
}
934+
846935
DataObject& DataObject::operator=(int _value)
847936
{
848937
setInt(_value);
849938
return *this;
850939
}
851940

941+
DataObject& DataObject::operator=(double _value)
942+
{
943+
setDouble(_value);
944+
return *this;
945+
}
946+
852947
DataObject& DataObject::operator[](std::string const& _key)
853948
{
854949
static const string c_assert = "m_type == DataType::NotInitialized || m_type == DataType::Object (DataObject& operator[])";

libdataobj/DataObject.h

+21-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ enum DataType
1616
Bool,
1717
String,
1818
Integer,
19+
Double,
1920
Object,
2021
Array,
2122
Null
@@ -25,6 +26,17 @@ class DataObjectK;
2526
class GCP_SPointerDataObject;
2627
typedef GCP_SPointerDataObject spDataObject;
2728

29+
class SafeBool
30+
{
31+
public:
32+
SafeBool(bool value) : m_value(value) {}
33+
SafeBool(int) = delete;
34+
operator bool() const { return m_value; }
35+
36+
private:
37+
bool m_value;
38+
};
39+
2840
/// DataObject
2941
/// A data sturcture to manage data from json, yml
3042
class DataObject : public GCP_SPointerBase
@@ -33,9 +45,11 @@ class DataObject : public GCP_SPointerBase
3345
DataObject();
3446
DataObject(DataObject const&) = delete;
3547
explicit DataObject(DataType _type);
36-
DataObject(DataType _type, bool _bool);
48+
explicit DataObject(DataType _type, SafeBool _bool);
49+
explicit DataObject(DataType _type, int _bool);
50+
explicit DataObject(DataType _type, double _bool);
3751

38-
// DataObject(str)
52+
explicit DataObject(double _int);
3953
explicit DataObject(int _int);
4054
explicit DataObject(std::string&& _str);
4155
explicit DataObject(std::string const& _str);
@@ -71,6 +85,7 @@ class DataObject : public GCP_SPointerBase
7185
std::string const asStringAnyway() const;
7286

7387
int asInt() const;
88+
double asDouble() const;
7489
bool asBool() const;
7590

7691
bool operator==(bool _value) const;
@@ -83,10 +98,13 @@ class DataObject : public GCP_SPointerBase
8398
void copyFrom(DataObject const& _other);
8499
DataObject& operator=(std::string&& _value);
85100
DataObject& operator=(std::string const& _value);
101+
DataObject& operator=(size_t _value);
86102
DataObject& operator=(int _value);
103+
DataObject& operator=(double _value);
87104

88105
void setString(std::string&& _value);
89106
void setInt(int _value);
107+
void setDouble(double _value);
90108
void setBool(bool _value);
91109
void replace(DataObject const& _value);
92110
void renameKey(std::string const& _currentKey, std::string&& _newKey);
@@ -136,7 +154,7 @@ class DataObject : public GCP_SPointerBase
136154
typedef std::pair<VecSpData, MapKeyToObject> DataObjecto;
137155
typedef std::tuple<VecSpData, MapKeyToObject> DataArray;
138156
struct DataNull {};
139-
typedef std::variant<std::monostate, bool, std::string, int, DataObjecto, DataArray, DataNull> DataVariant;
157+
typedef std::variant<std::monostate, bool, std::string, int, double, DataObjecto, DataArray, DataNull> DataVariant;
140158
DataVariant m_value;
141159
};
142160

0 commit comments

Comments
 (0)