-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverToBuoys.h
61 lines (53 loc) · 1.91 KB
/
serverToBuoys.h
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
//
// Created by tag on 16/12/16.
//
#pragma once
#include <string>
#include <vector>
#include <map>
class Params; // Forward declaration of Params
class ServerToBuoys
{
public:
ServerToBuoys();
~ServerToBuoys();
private:
int sock;
void sigchld_handler(int s);
void *get_in_addr(struct sockaddr *sa);
void sendParams(Params params);
};
/*!
* A class to hold any type supported by vibes properties system, an to provide JSON serialization
*/
class Value {
enum value_type_enum{vt_none, vt_integer, vt_string, vt_decimal, vt_array, vt_object} _type;
union {int _integer; double _decimal; const Params *_object;};
std::string _string;
std::vector<Value> _array;
public:
Value() : _type(vt_none) {}
Value(int i) : _integer(i), _type(vt_integer) {}
Value(const double &d) : _decimal(d), _type(vt_decimal) {}
Value(const std::string &s) : _string(s), _type(vt_string) {}
Value(const char *s) : _string(s), _type(vt_string) {}
Value(const std::vector<Value> &a) : _array(a), _type(vt_array) {}
template <typename T> Value(const std::vector<T> &v) : _array(v.begin(),v.end()), _type(vt_array) {}
/*explicit */Value(const Params &p) : _object(&p), _type(vt_object) {}
bool empty() {return (_type == vt_none);}
std::string toJSONString() const;
};
class Params
{
public:
Params() {}
template<typename T> Params(const std::string & name, const T &p) {_values[name] = p;}
Value & operator[](const std::string &key) {return _values[key];}
Value pop(const std::string &key, const Value &value_not_found = Value());
Params& operator& (const Params &p) { for(KeyValueMap::const_iterator it = p._values.begin(); it != p._values.end(); ++it) _values[it->first] = it->second; return *this;}
std::size_t size() const { return _values.size(); }
std::string toJSON() const;
private:
typedef std::map<std::string, Value> KeyValueMap;
KeyValueMap _values;
};