-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStore.h
More file actions
130 lines (108 loc) · 4.58 KB
/
Store.h
File metadata and controls
130 lines (108 loc) · 4.58 KB
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
#ifndef STORE_H
#define STORE_H
#include <string>
#include <fstream>
#include <vector>
#include <map>
#include <iostream>
#include <sstream>
#include <typeinfo>
#include <cstring>
#include <cstdlib>
/**
* \class Store
*
* This class Is a dynamic data storeage class and can be used to store variables of any type listed by ASCII key. The storage of the varaible is in ASCII, so is inefficent for large numbers of entries.
*
* $Author: B.Richards $
* $Date: 2019/05/28 10:44:00 $
* Contact: b.richards@qmul.ac.uk
*/
class Store{
public:
Store(); ////< Sinple constructor
void Initialise(std::string filename); ///< Initialises Store by reading in entries from an ASCII text file, when each line is a variable and its value in key value pairs. @param filename The filepath and name to the input file.
void JsonParser(std::string input); ///< Converts a flat JSON formatted string to Store entries in the form of key value pairs. @param input The input flat JSON string.
void Print(); ///< Prints the contents of the Store.
void Delete(); ///< Deletes all entries in the Store.
bool Has(std::string key); ///<Returns bool based on if store contains entry given by sting @param string key to comapre.
const std::map<std::string,std::string>* GetMap();
bool Erase(std::string key); ///<Erase the element from the store, if it exists
/**
Templated getter function for tore content. Assignment is templated and via reference.
@param name The ASCII key that the variable in the Store is stored with.
@param out The variable to fill with the value.
@return Return value is true if varaible exists in the Store and correctly assigned to out and false if not.
*/
template<typename T> bool Get(std::string name,T &out){
if(m_variables.count(name)>0){
std::stringstream stream(m_variables[name]);
stream>>out;
if(stream.fail()) return false;
// the trouble here is that if we have a string "4cds" and stream it into a numeric,
// then stream.fail will be false, but only the leading numeric chars will be output.
// c++11's std::is_arithmetic(T) would be ideal here, but without c++11 we do it the long way...
if(typeid(float).name()==typeid(T).name()){
char* pEnd;
float f1 = strtof(stream.str().c_str(), &pEnd);
if(strcmp(pEnd,"")!=0) return false;
} else if(typeid(double).name()==typeid(T).name()){
char* pEnd;
double d1 = strtod(stream.str().c_str(), &pEnd);
if(strcmp(pEnd,"")!=0) return false;
} else if(typeid(T).name()==typeid(short int).name() ||
typeid(T).name()==typeid(int).name() ||
typeid(T).name()==typeid(long int).name() ||
// typeid(T).name()==typeid(long long int).name() ||
typeid(T).name()==typeid(unsigned char).name() ||
typeid(T).name()==typeid(unsigned short int).name() ||
typeid(T).name()==typeid(unsigned int).name() ||
typeid(T).name()==typeid(unsigned long int).name()){
// typeid(T).name()==typeid(unsigned long long int).name()){
char* pEnd;
long l1 = strtol(stream.str().c_str(), &pEnd,10);
if(strcmp(pEnd,"")!=0) return false;
}
// else the data type we're streaming into is probably a string
// TODO check whether the held value is numeric and warn if so..?
return true;
}
else return false;
}
/**
Templated setter function to assign vairables in the Store.
@param name The key to be used to store and reference the variable in the Store.
@param in the varaible to be stored.
*/
template<typename T> void Set(std::string name,T in){
std::stringstream stream;
stream<<in;
m_variables[name]=stream.str();
}
/**
Returns string pointer to Store element.
@param key The key of the string pointer to return.
@return a pointer to the string version of the value within the Store.
*/
std::string* operator[](std::string key){
return &m_variables[key];
}
/**
Allows streaming of a flat JASON formatted string of Store contents.
*/
template<typename T> void operator>>(T& obj){
std::stringstream stream;
stream<<"{";
bool first=true;
for (std::map<std::string,std::string>::iterator it=m_variables.begin(); it!=m_variables.end(); ++it){
if (!first) stream<<",";
stream<<"\""<<it->first<<"\":\""<< it->second<<"\"";
first=false;
}
stream<<"}";
obj=stream.str();
}
private:
std::map<std::string,std::string> m_variables;
};
#endif