-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasedataelement.h
More file actions
115 lines (89 loc) · 2.91 KB
/
basedataelement.h
File metadata and controls
115 lines (89 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#ifndef BASE_DATA_ELEMENT_H
#define BASE_DATA_ELEMENT_H
#include <boost/multiprecision/cpp_int.hpp>
#include "json.hpp"
using namespace boost::multiprecision;
namespace L16 __attribute__((visibility ("hidden"))) {
template <class T>
constexpr T make_mask(std::size_t pos, std::size_t len)
{
return ((static_cast<T>(1) << len) - 1) << pos;
}
class IDataElement
{
public:
IDataElement(): m_value(-1)
{}
virtual std::string name() const = 0;
virtual bool is_technical() const = 0;
unsigned long value() const { return m_value; }
virtual unsigned int position() const = 0;
bool is_valid() const { return m_validity; }
virtual bool operator() (uint1024_t const& data) = 0;
virtual void operator=(nlohmann::json const& data) = 0;
virtual uint1024_t mask() const = 0;
std::string identifier() const { return name(); }
nlohmann::json to_json() const;
enum Type {
Technical = 0,
Tactical = 1,
Auto = 2
};
//friend uint1024_t& operator<<(uint1024_t& data, const L16::IDataElement& elt);
protected:
unsigned long m_value;
bool m_validity;
};
template <unsigned int dfi, unsigned int dui, unsigned int pos, unsigned int length, unsigned long ns=-1, IDataElement::Type type=IDataElement::Tactical> class DataElement: public IDataElement
{
public:
DataElement()
{
m_value = ns;
}
bool operator() (uint1024_t const& data) override
{
m_value = static_cast <unsigned int>((data & mask()) >> pos);
m_validity = true;
return true;
}
void operator=(nlohmann::json const& data) override
{
if (type != IDataElement::Technical)
{
m_value = data.value(name(), (unsigned long)(-1));
m_validity = (m_value != (unsigned long)(-1));
}
}
uint1024_t const& operator >>(uint1024_t const & data)
{
m_value = static_cast <unsigned int>((data & mask()) >> pos);
m_validity = true;
return data;
}
uint1024_t mask() const
{
return (make_mask<uint1024_t>(pos, length));
}
unsigned int position() const override
{
return pos;
}
bool is_technical() const override { return type != IDataElement::Tactical; }
template <unsigned int, unsigned int>
friend uint1024_t const& operator>>(uint1024_t const& data, IDataElement& elt);
protected:
uint1024_t m_mask;
};
}
template <unsigned int pos, unsigned int length, unsigned long ns>
uint1024_t const& operator>>(uint1024_t const& data, L16::IDataElement& elt)
{
elt(data);
return data;
}
uint1024_t& operator<<(uint1024_t& data, const L16::IDataElement& elt);
std::ostream& operator<<(std::ostream& os, const L16::IDataElement& elt);
uint1024_t const& operator>>(uint1024_t const& data, L16::IDataElement& elt);
nlohmann::json const& operator>>(nlohmann::json const& data, L16::IDataElement& elt);
#endif // BASE_DATA_ELEMENT_H_16392080208