serializable.h 2.15 KB
Newer Older
pryanikov's avatar
pryanikov committed
1
2
3
4
5
6
#ifndef REPLICATOR_SERIALIZABLE_H
#define REPLICATOR_SERIALIZABLE_H

#include <string>
#include <map>
#include <sstream>
7
#include <memory>
pryanikov's avatar
pryanikov committed
8
9
10
11
12
#include <boost/any.hpp>

namespace replicator {

class SerializableValue {
pryanikov's avatar
pryanikov committed
13
14
	private:
		boost::any value;
pryanikov's avatar
pryanikov committed
15

pryanikov's avatar
pryanikov committed
16
17
	public:
		template<typename T>
pryanikov's avatar
pryanikov committed
18
		inline SerializableValue& operator= (const T& value_) {
pryanikov's avatar
pryanikov committed
19
20
			value = boost::any(value_);
			return *this;
pryanikov's avatar
pryanikov committed
21
		}
pryanikov's avatar
pryanikov committed
22
23
24
25
26
		template<typename T>
		inline SerializableValue& operator= (T&& value_) {
			value = boost::any(std::forward<T>(value_));
			return *this;
		}
pryanikov's avatar
pryanikov committed
27

pryanikov's avatar
pryanikov committed
28
29
30
		inline SerializableValue& operator= (const boost::any& value_) {
			value = value_;
			return *this;
pryanikov's avatar
pryanikov committed
31
		}
pryanikov's avatar
pryanikov committed
32
33
34
35
		inline SerializableValue& operator= (boost::any&& value_) {
			value = std::forward<boost::any>(value_);
			return *this;
		}
pryanikov's avatar
pryanikov committed
36

pryanikov's avatar
pryanikov committed
37
38
		template<typename T> inline bool is() const {
			return value.type() == typeid(T);
pryanikov's avatar
pryanikov committed
39
40
		}

pryanikov's avatar
pryanikov committed
41
42
43
		template<typename T> inline T as() const {
			return boost::any_cast<T>(value);
		}
pryanikov's avatar
pryanikov committed
44

pryanikov's avatar
pryanikov committed
45
46
		std::string to_string() const {
			std::ostringstream s;
pryanikov's avatar
pryanikov committed
47

pryanikov's avatar
pryanikov committed
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
			if (is<std::string()>()) {
				s << as<std::string>();
			} else if (is<char>()) {
				s << as<char>();
			} else if (is<unsigned char>()) {
				s << as<unsigned char>();
			} else if (is<short>()) {
				s << as<short>();
			} else if (is<unsigned short>()) {
				s << as<unsigned short>();
			} else if (is<int>()) {
				s << as<int>();
			} else if (is<unsigned int>()) {
				s << as<unsigned int>();
			} else if (is<long>()) {
				s << as<long>();
			} else if (is<unsigned long>()) {
				s << as<unsigned long>();
			} else if (is<long long>()) {
				s << as<long long>();
			} else if (is<unsigned long long>()) {
				s << as<unsigned long long>();
			} else if (is<float>()) {
				s << as<float>();
			} else if (is<double>()) {
				s << as<double>();
			}
			return s.str();
		}
pryanikov's avatar
pryanikov committed
77
78
79
};


pryanikov's avatar
pryanikov committed
80
struct SerializableBinlogEvent
pryanikov's avatar
pryanikov committed
81
82
83
{
	std::string binlog_name;
	unsigned long binlog_pos;
pryanikov's avatar
pryanikov committed
84
	// unsigned long seconds_behind_master;
pryanikov's avatar
pryanikov committed
85
	// unsigned long unix_timestamp;
pryanikov's avatar
pryanikov committed
86
87
88
	std::string database;
	std::string table;
	std::string event;
pryanikov's avatar
pryanikov committed
89
	std::map<unsigned, SerializableValue> row;
pryanikov's avatar
pryanikov committed
90
91
};

92
93
typedef std::unique_ptr<SerializableBinlogEvent> SerializableBinlogEventPtr;

pryanikov's avatar
pryanikov committed
94
95
96
} // replicator

#endif // REPLICATOR_SERIALIZABLE_H