sisi4s
Loading...
Searching...
No Matches
Data.hpp
Go to the documentation of this file.
1#ifndef DATA_DEFINED
2#define DATA_DEFINED
3
4#include <util/Log.hpp>
5#include <math/Float.hpp>
6#include <math/Complex.hpp>
7#include <string>
8#include <map>
9#include <vector>
10#include <util/Tensor.hpp>
11// TODO: find out why Exception must be included after string,map and ctf
12#include <util/Exception.hpp>
13
14namespace sisi4s {
20template <typename F>
22#define _MAKE_TRAIT(_type, _name) \
23 template <> \
24 class TypeTraits<_type> { \
25 public: \
26 static std::string getName() { return _name; } \
27 };
28_MAKE_TRAIT(bool, "boolean")
29_MAKE_TRAIT(int64_t, "integer")
32#undef _MAKE_TRAIT
33
34class Data {
35public:
36 enum Stage {
38 TYPED = 1,
40 READY = 3,
41 UNUSED = 4,
42 LINGERING = 5
43 };
44 Data(std::string const &name_);
45 virtual ~Data() { dataMap[name] = nullptr; }
46 std::string getName() const { return name; }
47 std::string getTypeName() const { return typeName; }
48 Stage getStage() const { return stage; }
49
50 static Data *get(std::string const &name) {
51 auto iterator(dataMap.find(name));
52 return (iterator != dataMap.end()) ? iterator->second : nullptr;
53 }
54
55protected:
59 Data(std::string const &name_, std::string const &typeName_)
60 : name(name_)
61 , typeName(typeName_)
62 , stage(TYPED) {
63 Data *mentionedData(dataMap[name_]);
64 if (mentionedData) {
65 if (mentionedData->getStage() == MENTIONED) {
66 delete mentionedData;
67 } else {
68 LOG(1, "Data") << "overwriting existing data: " << name_ << std::endl;
69 delete mentionedData;
70 // throw new EXCEPTION("Trying to overwrite existing data");
71 }
72 }
73 dataMap[name_] = this;
74 }
75 std::string name, typeName;
77
78 static std::map<std::string, Data *> dataMap;
79 static int64_t nextAnynomousDataId;
80};
81
82class TypedData : public Data {
83protected:
87 TypedData(std::string const &typeName_)
88 : Data(nextName(), typeName_) {}
92 TypedData(std::string const &name_, std::string const &typeName_)
93 : Data(name_, typeName_) {}
94
95 static std::string nextName() {
96 std::stringstream sStream;
97 sStream << "Constant" << nextId++;
98 return sStream.str();
99 }
100
106 static int nextId;
107};
108
109class TextData : public TypedData {
110public:
111 TextData(std::string const &value_)
112 : TypedData("text")
113 , value(value_) {}
114 TextData(std::string const &name_, std::string const &value_)
115 : TypedData(name_, "text")
116 , value(value_) {}
117 std::string value;
118};
119
120class BooleanData : public TypedData {
121public:
122 BooleanData(bool const value_)
123 : TypedData("boolean")
124 , value(value_) {}
125 BooleanData(std::string const &name_, bool const value_)
126 : TypedData(name_, "boolean")
127 , value(value_) {}
128 bool value;
129};
130
131class NumericData : public TypedData {
132protected:
133 NumericData(std::string const &typeName_)
134 : TypedData(typeName_) {}
135 NumericData(std::string const &name_, std::string const &typeName_)
136 : TypedData(name_, typeName_) {}
137};
138
139class RealData : public NumericData {
140public:
141 RealData(real value_)
142 : NumericData("real")
143 , value(value_) {}
144 RealData(std::string const &name_, const real value_)
145 : NumericData(name_, "real")
146 , value(value_) {}
147 real value;
148};
149
150class IntegerData : public NumericData {
151public:
152 IntegerData(int64_t value_)
153 : NumericData("integer")
154 , value(value_) {}
155 IntegerData(std::string const &name_, int64_t const value_)
156 : NumericData(name_, "real")
157 , value(value_) {}
158 int64_t value;
159};
160
161template <typename F = double, typename C = std::vector<F>>
162struct ContainerData : public NumericData {
163 ContainerData(C *value_)
164 : NumericData("Container of " + TypeTraits<F>::getName())
165 , value(value_) {}
166 ContainerData(std::string const &name_, C *value_)
167 : NumericData(name_, "Container of " + TypeTraits<F>::getName())
168 , value(value_) {}
169 virtual ~ContainerData() {
170 if (value) delete value;
171 }
173};
174
175template <typename F = double, typename T = sisi4s::Tensor<F>>
176class TensorData : public NumericData {
177public:
178 TensorData(T *value_)
179 : NumericData("tensor of " + TypeTraits<F>::getName())
180 , value(value_) {}
181 TensorData(std::string const &name_, T *value_)
182 : NumericData(name_, "tensor of " + TypeTraits<F>::getName())
183 , value(value_) {}
184 virtual ~TensorData() {
185 if (value) delete value;
186 }
188};
189} // namespace sisi4s
190
191#endif
#define _MAKE_TRAIT(_type, _name)
Definition Data.hpp:22
#define LOG(...)
Definition Log.hpp:119
Definition Data.hpp:120
BooleanData(std::string const &name_, bool const value_)
Definition Data.hpp:125
bool value
Definition Data.hpp:128
BooleanData(bool const value_)
Definition Data.hpp:122
Definition Data.hpp:34
std::string getTypeName() const
Definition Data.hpp:47
std::string getName() const
Definition Data.hpp:46
Data(std::string const &name_, std::string const &typeName_)
protected constructor for typed data.
Definition Data.hpp:59
static Data * get(std::string const &name)
Definition Data.hpp:50
Stage getStage() const
Definition Data.hpp:48
static std::map< std::string, Data * > dataMap
Definition Data.hpp:78
std::string name
Definition Data.hpp:75
Stage stage
Definition Data.hpp:76
Stage
Definition Data.hpp:36
@ MENTIONED
Definition Data.hpp:37
@ LINGERING
Definition Data.hpp:42
@ UNUSED
Definition Data.hpp:41
@ TYPED
Definition Data.hpp:38
@ READY
Definition Data.hpp:40
@ ALLOCATED
Definition Data.hpp:39
std::string typeName
Definition Data.hpp:75
virtual ~Data()
Definition Data.hpp:45
static int64_t nextAnynomousDataId
Definition Data.hpp:79
Definition Data.hpp:150
IntegerData(int64_t value_)
Definition Data.hpp:152
int64_t value
Definition Data.hpp:158
IntegerData(std::string const &name_, int64_t const value_)
Definition Data.hpp:155
Definition Data.hpp:131
NumericData(std::string const &name_, std::string const &typeName_)
Definition Data.hpp:135
NumericData(std::string const &typeName_)
Definition Data.hpp:133
Definition Data.hpp:139
RealData(std::string const &name_, const real value_)
Definition Data.hpp:144
RealData(real value_)
Definition Data.hpp:141
real value
Definition Data.hpp:147
Definition Data.hpp:176
TensorData(T *value_)
Definition Data.hpp:178
virtual ~TensorData()
Definition Data.hpp:184
T * value
Definition Data.hpp:187
TensorData(std::string const &name_, T *value_)
Definition Data.hpp:181
Definition Data.hpp:109
TextData(std::string const &value_)
Definition Data.hpp:111
TextData(std::string const &name_, std::string const &value_)
Definition Data.hpp:114
std::string value
Definition Data.hpp:117
Definition Data.hpp:21
Definition Data.hpp:82
static std::string nextName()
Definition Data.hpp:95
static int nextId
next id number to be given anonymous constant data. They will be named "Constant0",...
Definition Data.hpp:106
TypedData(std::string const &typeName_)
Protected constructor for anonymous constant data.
Definition Data.hpp:87
TypedData(std::string const &name_, std::string const &typeName_)
Protected constructor for named data.
Definition Data.hpp:92
Definition Algorithm.hpp:10
Complex< Float64 > Complex64
Definition Complex.hpp:14
FloatTypes< 64 >::type Float64
Definition Float.hpp:30
Complex< real > complex
Definition Complex.hpp:17
Definition Data.hpp:162
ContainerData(C *value_)
Definition Data.hpp:163
virtual ~ContainerData()
Definition Data.hpp:169
ContainerData(std::string const &name_, C *value_)
Definition Data.hpp:166
C * value
Definition Data.hpp:172