serializable.h 1.87 KB
Newer Older
pryanikov's avatar
pryanikov committed
1
2
3
4
5
6
7
8
9
10
11
#ifndef REPLICATOR_SERIALIZABLE_H
#define REPLICATOR_SERIALIZABLE_H

#include <string>
#include <map>
#include <sstream>
#include <boost/any.hpp>

namespace replicator {

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

pryanikov's avatar
pryanikov committed
15
16
17
	public:
		SerializableValue () {}
		SerializableValue (const boost::any &value_) : value(value_) {}
pryanikov's avatar
pryanikov committed
18

pryanikov's avatar
pryanikov committed
19
20
21
22
		template<typename T>
		inline SerializableValue& operator= (T value_) {
			value = boost::any(value_);
			return *this;
pryanikov's avatar
pryanikov committed
23
24
		}

pryanikov's avatar
pryanikov committed
25
26
27
		inline SerializableValue& operator= (const boost::any& value_) {
			value = value_;
			return *this;
pryanikov's avatar
pryanikov committed
28
29
		}

pryanikov's avatar
pryanikov committed
30
31
		template<typename T> inline bool is() const {
			return value.type() == typeid(T);
pryanikov's avatar
pryanikov committed
32
33
		}

pryanikov's avatar
pryanikov committed
34
35
36
		template<typename T> inline T as() const {
			return boost::any_cast<T>(value);
		}
pryanikov's avatar
pryanikov committed
37

pryanikov's avatar
pryanikov committed
38
39
		std::string to_string() const {
			std::ostringstream s;
pryanikov's avatar
pryanikov committed
40

pryanikov's avatar
pryanikov committed
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
			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
70
71
72
};


pryanikov's avatar
pryanikov committed
73
struct SerializableBinlogEvent
pryanikov's avatar
pryanikov committed
74
75
76
77
78
79
80
81
{
	std::string binlog_name;
	unsigned long binlog_pos;
	unsigned long seconds_behind_master;
	unsigned long unix_timestamp;
	std::string database;
	std::string table;
	std::string event;
pryanikov's avatar
pryanikov committed
82
	std::map<unsigned, SerializableValue> row;
pryanikov's avatar
pryanikov committed
83
84
85
86
87
};

} // replicator

#endif // REPLICATOR_SERIALIZABLE_H