SOIL C++
C++ Unified Device Interface
Figure.h
Go to the documentation of this file.
1#pragma once
2#include "constants.h"
3#include "Types.h"
4#include "Element.h"
5#include "Container.h"
6#include "Range.h"
7namespace SOIL
8{
18 template <typename T>
20 template<>
22 template<>
23 DLL HTTP::Json datatype<bool>(void);
24 template<>
25 DLL HTTP::Json datatype<std::string>(void);
26 template<>
28 template<>
29 DLL HTTP::Json datatype<int>(void);
30 template<>
31 DLL HTTP::Json datatype<SOIL::TIME>(void);
32 template<>
33 DLL HTTP::Json datatype<SOIL::ENUM>(void);
34
35
47 template <typename T, int x=-1, int y=-1>
48 class Figure : public Element
49 {
50 protected:
57
63 std::string unit;
64
71
78
85 virtual void read(void) = 0;
86
93 virtual void write(void) = 0;
94
95 public:
109 Figure(std::shared_ptr<Element> parent, std::string uuid, std::string name, std::string description, std::string unit, std::string ontology = "", Range<T> range = Range<T>(), TIME time = TIME());
110
117
129
130
138
148
156
164
176
177
186 HTTP::Json wjson(void) override;
187
199
209 virtual void update(const Container<T, x, y>& value, TIME time);
210 };
211
212
213}
214
215
216
217template<typename T, int x, int y>
218SOIL::Figure<T, x, y>::Figure(std::shared_ptr<Element> parent, std::string uuid, std::string name, std::string description, std::string unit, std::string ontology, Range<T> range, SOIL::TIME time) : SOIL::Element(parent, uuid, name, description, ontology), unit(unit), range(range), time(time)
219{
220}
221
222template<typename T, int x, int y>
224{
225}
226
227
228template<typename T, int x, int y>
230{
231
232 std::unique_lock<std::recursive_mutex> lock(mutex);
233 HTTP::Json json_root = SOIL::Element::wjson();
234 json_root[U("timestamp"] = (time.is_null() || time.rfc3339() == "")) ? HTTP::Json::null() : HTTP::Json::string(utility::conversions::to_string_t(time.rfc3339()));
235 json_root[U("range")] = range.wjson();
236 json_root[U("unit")] = (unit.length() == 0) ? HTTP::Json::null() : SOIL::to_json(unit);
237
238 HTTP::Json value_json = value.wjson();
239 json_root[U("value")] = value_json[U("value")];
240 json_root[U("dimension")] = value_json[U("dimension")];
241 json_root[U("datatype")] = datatype<T>();
242
243
244 return json_root;
245}
246
247template<typename T, int x, int y>
249{
250 return Container<T, x, y>(value);
251}
252
253template<typename T, int x, int y>
255{
256}
257
258template<typename T, int x, int y>
260{
261 if (!check_range(value))
262 {
263 throw std::range_error("Value is out of range specified for Figure " + uuid);
264 }
265 this->value = value;
266 return *this;
267}
268
269
270template<typename T, int x, int y>
272{
273 this->read();
274 return value;
275}
276
277template<typename T, int x, int y>
279{
280 return value.check_range(range);
281}
282
283template<typename T, int x, int y>
285{
286 this->range = range;
287}
288
289
290template<typename T, int x, int y>
292{
293 this->time = time;
294}
295
296
297template<typename T, int x, int y>
299{
300 if (!check_range(value))
301 {
302 throw std::range_error("Value is out of range specified for Figure " + uuid);
303 }
304 *this = value;
305}
306
307template<typename T, int x, int y>
309{
310 std::unique_lock<std::recursive_mutex> lock(mutex);
311 if (!check_range(value))
312 {
313 throw std::range_error("Value is out of range specified for Figure " + uuid);
314 }
315 *this = value;
316 this->time = time;
317}
318
319
320
321
322
HTTP::Json wjson(void)
WJSON representation.
Definition: Container.h:269
bool check_range(Range< T > range) const
Check range.
Definition: Container.h:293
SOIL Base Element.
Definition: Element.h:24
T * cast(void)
Get dynamically casted pointer.
Definition: Element.h:264
std::string uuid
Local UUID.
Definition: Element.h:53
virtual HTTP::Json wjson(void)
HTTP JSON.
Definition: Element.cpp:130
std::string description
Description.
Definition: Element.h:67
std::string ontology
Ontology identifier.
Definition: Element.h:76
std::string name
Name.
Definition: Element.h:60
std::shared_ptr< Element > parent
Parent Pointer.
Definition: Element.h:39
Intermediate class for Variable and Parameter that derives from Element.
Definition: Figure.h:49
~Figure()
Destructor.
Definition: Figure.h:223
void set_value(const Container< T, x, y > &value)
Set Value.
Definition: Figure.h:298
Figure< T, x, y > & operator=(const Container< T, x, y > &value)
Assignment operator.
Definition: Figure.h:259
std::string unit
Unit.
Definition: Figure.h:63
virtual void update(const Container< T, x, y > &value, TIME time)
Update.
Definition: Figure.h:308
Container< T, x, y > cast(T value)
Cast to container.
Definition: Figure.h:248
virtual void read(void)=0
Read callback.
Definition: Figure.h:254
Figure(std::shared_ptr< Element > parent, std::string uuid, std::string name, std::string description, std::string unit, std::string ontology="", Range< T > range=Range< T >(), TIME time=TIME())
Constructor.
Definition: Figure.h:218
TIME time
Data Timestamp.
Definition: Figure.h:56
Container< T, x, y > value
Value.
Definition: Figure.h:70
bool check_range(const Container< T, x, y > &value) const
Check range.
Definition: Figure.h:278
Range< T > range
Range.
Definition: Figure.h:77
void set_time(TIME time)
Set Time.
Definition: Figure.h:291
Container< T, x, y > & operator*(void)
Access Operator.
Definition: Figure.h:271
HTTP::Json wjson(void) override
HTTP JSON.
Definition: Figure.h:229
virtual void write(void)=0
Write callback.
void set_range(Range< T > range)
Set Range.
Definition: Figure.h:284
Range Helper Class.
Definition: Range.h:25
HTTP::Json wjson(void)
HTTP JSON.
Definition: Range.cpp:72
SOIL Time.
Definition: Time.h:13
bool is_null(void) const
Is Null?
Definition: Time.h:92
DLL std::string rfc3339(void) const
RFC3339 representation.
Definition: Time.cpp:6
web::json::value Json
HTTP JSON.
Definition: Types.h:39
Type definitions.
Definition: Container.h:7
DLL HTTP::Json datatype< bool >(void)
Definition: Figure.cpp:10
Time TIME
SOIL Time.
Definition: Types.h:54
DLL HTTP::Json datatype< int >(void)
Definition: Figure.cpp:31
HTTP::Json datatype(void)
HTTP JSON datatype.
DLL web::json::value to_json(const T &value)
Value to JSON.
DLL HTTP::Json datatype< double >(void)
Definition: Figure.cpp:4
DLL HTTP::Json datatype< int64_t >(void)
Definition: Figure.cpp:24