-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.h
84 lines (65 loc) · 2.3 KB
/
table.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
#ifndef __TABLE_H__
#define __TABLE_H__
#include <string>
#include <map>
#include "lexal.h"
class Table;
class Value
{
public:
enum Type {
Integer,
Double,
String,
Table
};
public:
virtual ~Value() { };
public:
virtual ::Table* asTable() const = 0;
virtual Type getType() const = 0;
virtual int asInt() const = 0;
virtual double asDouble() const = 0;
virtual std::wstring asString() const = 0;
virtual Value* clone() const = 0;
};
class Table
{
public:
typedef std::map<std::wstring, Value*> ValuesMap;
typedef ValuesMap::const_iterator Iterator;
private:
ValuesMap fields;
int lastArrayIndex;
public:
Table();
Table(const Table &table);
Table(const std::string &fileName);
Table(Lexal &lexal, int startLine, int startPos);
~Table();
public:
Table& operator = (const Table &table);
std::wstring toString() const;
std::wstring toString(bool printBraces, bool butify, int ident) const;
bool isArray() const;
void save(const std::wstring &fileName) const;
public:
Iterator begin() const { return fields.begin(); };
Iterator end() const { return fields.end(); };
bool hasKey(const std::wstring &key) const;
Value::Type getType(const std::wstring &key) const;
std::wstring getString(const std::wstring &key, const std::wstring &dflt = L"") const;
int getInt(const std::wstring &key, int dflt=0) const;
double getDouble(const std::wstring &key, double dflt=0) const;
Table* getTable(const std::wstring &key, Table *dflt=NULL) const;
void setString(const std::wstring &key, const std::wstring &value);
void setInt(const std::wstring &key, int value);
void setDouble(const std::wstring &key, double value);
void setTable(const std::wstring &key, Table *value);
private:
void parse(Lexal &lexal, bool needBracket, int startLine, int startPos);
void addArrayElement(Lexal &lexal, const Lexeme &lexeme);
void addValuePair(Lexal &lexal, const std::wstring &name);
void setValue(const std::wstring &key, Value *value);
};
#endif