forked from edrosten/gvars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GVars2.h.historic
147 lines (87 loc) · 3.84 KB
/
GVars2.h.historic
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
/*
This file shows how GVars2 used to work. Now this functionality is provided by GVars3
via the GVars2.h compatibility wrapper.
This file is part of the GVars3 Library.
Copyright (C) 2005 The Authors
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __GVARS_2_H
#define __GVARS_2_H
#include <string>
#include <map>
#include <numerics.h>
using namespace std;
template <class T> class gvar2
{
friend class GVars2;
public:
gvar2() {datapointer_ptr=NULL;};
inline T& operator*() {return **datapointer_ptr;};
void SafeSet(T tNewVal); // This swaps pointer, which is assumed to be atomic.
// Use this if setting vects, matrices or strings in threaded environments.
private:
T **datapointer_ptr;
static const T tDefaultVal;
static const int nTypeNum;
};
typedef gvar2< Vector<> > gvar2_Vector;
typedef gvar2< Matrix<> > gvar2_Matrix;
typedef gvar2<double> gvar2_double;
typedef gvar2<std::string> gvar2_string;
typedef gvar2<int> gvar2_int;
enum GVar2Type {gvar2_int_Type,
gvar2_double_Type,
gvar2_string_Type,
gvar2_Vector_Type,
gvar2_Matrix_Type};
struct GVar2PointerElement
{
void* datapointer;
string* psName;
GVar2Type nType;
};
class GVars2
{
public:
GVars2();
void Register(gvar2_string& gv2, string sName, string sDefault, bool bSilent =false );
template <class T> void Register(gvar2<T>& gv2, string sName, string sDefault, bool bSilent = false );
template <class T> void Register(gvar2<T>& gv2, string sName, T tDefault=DefaultForType<T>(), bool bSilent = false );
void SetVar(string sVar, string sValue);
void SetVar(string s);
int GetInt(string sName, int nDefault=0, bool bSilent = false);
double GetDouble(string sName, double dDefault=0.0, bool bSilent = false);
string GetString(string sName, string sDefault="", bool bSilent = false);
Vector<> GetVector(string sName, Vector<> vDefault=(Vector<1>)((double[]){0}), bool bSilent = false);
Matrix<> GetMatrix(string sName, Matrix<> mDefault=(Matrix<1>)((double[]){0}), bool bSilent = false);
int GetInt(string sName, string sDefault, bool bSilent = false);
double GetDouble(string sName, string sDefault, bool bSilent = false);
Vector<> GetVector(string sName, string sDefault, bool bSilent = false);
Matrix<> GetMatrix(string sName, string sDefault, bool bSilent = false);
void Load(string sFileName);
void PrintVarList(ostream& os=cout);
void PrintVar(string s, ostream& os, bool bEndl = true);
string StringValue(const string &sVarName, bool bNoQuotes=false);
char* ReadlineCommandGenerator(const char *szText, int nState);
private:
template <class T> void ReallyRegister(gvar2<T>& gv2, string sName, T tDefault, bool bSilent=false);
template <class T> void SetRegisteredVar(string sVar, GVar2PointerElement *pgvpe, string sValue);
template <class T> static string TypeName();
template <class T> static GVar2Type WhatType();
template <class T> static T DefaultForType();
// template <class T> T* ParseAndAllocate(string s); // Now in GStringUtil.h
map<string,string> mUnmatchedTags;
map<string,GVar2PointerElement*> mRegisteredTags;
};
#endif