forked from Conedy/Conedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobals.h
141 lines (115 loc) · 3.15 KB
/
globals.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef globals_h
#define globals_h globals_h
#include <map>
#include <iostream>
#include <string>
#include <sstream>
#include "baseType.h"
#include "command.h"
#include "params.h"
using namespace std;
template <typename T>
struct typeInteger
{
// enum { result = 666; }
};
template<> struct typeInteger<int> { enum { result = 0 }; };
template<> struct typeInteger<double> { enum { result = 1 }; };
template<> struct typeInteger<string> { enum { result = 2 }; };
#ifndef PYTHON
template<> struct typeInteger<bool> { enum { result = 3 }; };
#else
template<> struct typeInteger<bool> { enum { result = 0 }; };
#endif
template<> struct typeInteger<float> { enum { result = 4 }; };
template<> struct typeInteger<long double> { enum { result = 5 }; };
class globals
{
private:
string static typeString(int type)
{
switch ( type )
{
case 0:
return "int";
case 1:
return "double";
case 2:
return "string";
case 3:
return "bool";
case 4:
return "float";
case 5:
return "long double";
default:
stringstream fehler;
fehler << "You are using a non-predefined type (" << type << "). To predefine this type, modify globals.h." << endl;
throw fehler.str().c_str();
}
}
static map <string, void*> value;
static map <string, int> type;
public:
template <typename T> static void registerGlobal(string name, T v)
{
type[name] = typeInteger<T>::result;
value[name] = new T (v);
#ifndef PYTHON
command::declare(name, (T*)value[name]);
#endif
}
template <typename T> static T getGlobal(string name)
{
int typeInt = typeInteger <T>::result;
if (type.count(name) > 0)
{
if (type[name] == typeInt)
return * ((T*) value[name]) ;
stringstream fehler;
fehler << "Type mismatch: You try to get " << name << " as " << typeString(typeInt) << ". However, " << name << " is of type " << typeString(type[name]) << endl;
throw fehler.str().c_str();
}
else
{
stringstream fehler;
fehler << "Unknown string in getGlobal: " << name << endl;
throw fehler.str().c_str();
}
}
template <typename T> static T* getPointerToGlobal(string name)
{
int typeInt = typeInteger <T>::result;
if (type.count(name) > 0)
{
if (type[name] == typeInt)
return ((T*) value[name]) ;
stringstream fehler;
fehler << "Type mismatch: You try to get " << name << " as " << typeString(typeInt) << ". However, " << name << " is of type " << typeString(type[name]) << endl;
throw fehler.str().c_str();
}
else
{
stringstream fehler;
fehler << "Unknown string in getPointerToGlobal: " << name << endl;
throw fehler.str().c_str();
}
}
template <typename T> static void setGlobal(string name, T d)
{
if (type.count(name) > 0)
{
if (type [name] == typeInteger <T>::result)
* ((T*)value[name]) = d;
else
{
stringstream fehler;
fehler << "Type mismatch: " << name << " is of type " << typeString(type[name]) << ". But you try to set it as a " << typeString(typeInteger<T>::result) << endl;;
throw fehler.str().c_str();
}
}
else
conedy::params<T>::setStandard (name, d);
}
};
#endif