df.h 3.13 KB
Newer Older
1
2
3
4
5
#pragma once

#include <atomic>
#include <cassert>
#include <cstddef>
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
6
7
#include <sstream>
#include <typeinfo>
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
8
#include <vector>
9
10

#include "common.h"
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
11
#include "serializable.h"
12

Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
13
14
15
16
17
18
19
20
21
enum ValueType {
	TYPE_UNDEFINED=0,
	TYPE_INT,
	TYPE_REAL,
	TYPE_STRING,
	TYPE_VALUE,
	TYPE_NAME
};

Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
22
class DF : public Serializable
23
{
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
24
protected:
25
26
27
	void *ptr_;
	size_t size_;
	std::atomic_int *refs_;
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
28
	ValueType type_;
29
30
31
32
33
34
public:
	~DF();

	DF();
	DF(const DF &);

35
36
	DF(int);
	DF(double);
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
37
38
39
40
41
42
43
	DF(const std::string &);
	DF(const void *, size_t);

	DF &operator=(int);
	DF &operator=(double);
	DF &operator=(const std::string &);
	DF &operator=(const DF &);
44
45

	DF operator+(const DF &) const;
46
	DF operator-(const DF &) const;
47
48
	DF operator*(const DF &) const;
	DF operator/(const DF &) const;
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
49
	DF operator%(const DF &) const;
50
51
52
	DF operator<(const DF &) const;
	DF operator<=(const DF &) const;
	DF operator==(const DF &) const;
53
	DF operator!=(const DF &) const;
54
55
	DF operator>=(const DF &) const;
	DF operator>(const DF &) const;
56

Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
57
58
59
60
	bool is_set() const;
	bool is_unset() const;

	ValueType get_type() const { return type_; }
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
61

Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
	int get_int() const;
	double get_real() const;
	std::string get_string() const;

	size_t get_size() const;
	const void *get_data() const;
	void *get_data();

	int cast_int() const;
	double cast_real() const;
	std::string cast_string() const;

	std::string to_string() const;

	virtual size_t get_serialization_size() const;
	virtual size_t serialize(void *buf, size_t buf_size) const;
	virtual size_t deserialize(const void *buf, size_t buf_size);
79

Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
80
81
82
83
84
85
86
87
88
89
#ifdef ADD_DEBUG_INFO
	std::string DBG_INFO;
	std::string NAME;
#endif
protected:
	void _unlink();
	void _link(const DF &);
	void _grab(void *ptr, size_t size);
	void _copy(const void *ptr, size_t size);
	void _create(size_t size);
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
90

Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
91
92
93
	// Compatibility members
public:
	template<class T>
94
	const T * getData() const
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
95
	{
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
96
97
		assert(size_%sizeof(T)==0);

98
		return static_cast<const T *>(get_data());
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
99
100
	}

101
102
	std::string getName() const;

Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
103
104
105
	size_t getSize() const;

	void *create(size_t size);
106

Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
107
108
	template<class T>
	T *create(size_t el_count)
109
	{
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
110
		return static_cast<T *>(create(el_count*sizeof(T)));
111
112
	}

Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
113
114
	template<class T>
	T *create(size_t el_count, const T &init_val)
115
	{
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
116
117
118
119
		T *res=static_cast<T *>(create(el_count*sizeof(T)));

		for (auto i=0u; i<el_count; i++) {
			res[i]=init_val;
120
121
		}

Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
122
		return res;
123
124
	}

Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
125
	template<class T>
126
	T *getData()
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
127
128
	{
		assert(size_%sizeof(T)==0);
129

130
		return static_cast<T *>(get_data());
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
131
	}
132

Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
133
134
	template<class T>
	void setValue(const T& val)
135
	{
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
136
137
138
139
		if (std::is_same<T, int>::value || std::is_same<T, double>::value
				|| std::is_same<T, std::string>::value) {
			*this=val;
		} else {
140
141
			ABORT("Unsupported type in setValue: "
				+std::string(typeid(T).name()))
142
		}
143
144
	}

Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
145
146
	template<class T>
	const T &getValue() const
147
	{
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
148
149
		if (std::is_same<T, int>::value) {
			if (type_!=TYPE_INT) {
150
				ABORT("not an int: "+to_string());
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
151
			}
152
			return *static_cast<const T *>(ptr_);
Sergey Kireev's avatar
Sergey Kireev committed
153
154
		} else if (std::is_same<T, double>::value) {
			if (type_!=TYPE_REAL) {
155
				ABORT("not a real: "+to_string());
Sergey Kireev's avatar
Sergey Kireev committed
156
			}
157
			return *static_cast<const T *>(ptr_);
Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
158
		} else {
159
			ABORT("getValue<"+std::string(typeid(T).name())+">() failed for "+to_string());
160
161
162
		}
	}

Vladislav Perepelkin's avatar
Vladislav Perepelkin committed
163
	void copy(const DF &);
164
};