forked from cparse/cparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packToken.h
91 lines (77 loc) · 2.87 KB
/
packToken.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
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
#ifndef PACKTOKEN_H_
#define PACKTOKEN_H_
#include <string>
// Encapsulate TokenBase* into a friendlier interface
class packToken {
TokenBase* base;
public:
static const packToken& None();
typedef std::string (*strFunc_t)(const TokenBase*, uint32_t);
static strFunc_t& str_custom();
public:
packToken() : base(new TokenNone()) {}
packToken(const TokenBase& t) : base(t.clone()) {}
packToken(const packToken& t) : base(t.base->clone()) {}
packToken(packToken&& t) : base(t.base) { t.base = 0; }
packToken& operator=(const packToken& t);
template<class C>
packToken(C c, tokType type) : base(new Token<C>(c, type)) {}
packToken(int i) : base(new Token<int64_t>(i, INT)) {}
packToken(int64_t l) : base(new Token<int64_t>(l, INT)) {}
packToken(bool b) : base(new Token<uint8_t>(b, BOOL)) {}
packToken(size_t s) : base(new Token<int64_t>(s, INT)) {}
packToken(float f) : base(new Token<double>(f, REAL)) {}
packToken(double d) : base(new Token<double>(d, REAL)) {}
packToken(const char* s) : base(new Token<std::string>(s, STR)) {}
packToken(const std::string& s) : base(new Token<std::string>(s, STR)) {}
packToken(const TokenMap& map);
packToken(const TokenList& list);
~packToken() { delete base; }
TokenBase* operator->() const;
bool operator==(const packToken& t) const;
bool operator!=(const packToken& t) const;
packToken& operator[](const std::string& key);
packToken& operator[](const char* key);
const packToken& operator[](const std::string& key) const;
const packToken& operator[](const char* key) const;
TokenBase* token() { return base; }
const TokenBase* token() const { return base; }
bool asBool() const;
double asDouble() const;
int64_t asInt() const;
std::string& asString() const;
TokenMap& asMap() const;
TokenList& asList() const;
Tuple& asTuple() const;
STuple& asSTuple() const;
Function* asFunc() const;
// Specialize this template to your types, e.g.:
// MyType& m = packToken.as<MyType>();
template<typename T> T& as() const;
// The nest argument defines how many times
// it will recursively print nested structures:
std::string str(uint32_t nest = 3) const;
static std::string str(const TokenBase* t, uint32_t nest = 3);
public:
// This constructor makes sure the TokenBase*
// will be deleted when the packToken destructor is called.
//
// If you still plan to use your TokenBase* use instead:
//
// - packToken(token->clone())
//
explicit packToken(TokenBase* t) : base(t) {}
public:
// Used to recover the original pointer.
// The intance whose pointer was removed must be an rvalue.
TokenBase* release() && {
TokenBase* b = base;
// Setting base to 0 leaves the class in an invalid state,
// except for destruction.
base = 0;
return b;
}
};
// To allow cout to print it:
std::ostream& operator<<(std::ostream& os, const packToken& t);
#endif // PACKTOKEN_H_