forked from hundlab/LongQt-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cellutils.h
132 lines (119 loc) · 3.65 KB
/
cellutils.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
/*
* general purpose functions used by mostly by protocols
* This is the place to add a new cell type
*/
#ifndef CELLUTILS_H
#define CELLUTILS_H
#include <map>
#include <list>
#include <utility>
#include <string>
#include <stdarg.h>
#include <QXmlStreamReader>
#include <memory>
#include <functional>
#include "cell.h"
using namespace std;
class Protocol;
/*
* This is equivalent to a simple static class which holds functions
* and variables which are not class specific
*/
namespace CellUtils {
//declare CellInitializer function type
typedef function<shared_ptr<Cell>(void)> CellInitializer;
/*
* cell map this is how new instances of cells are created
* if you are adding a new cell to longqt add it to
* cellutils.cpp and add it to protocolCellDefualts map
* declaration found below definition found in cellutils.cpp
*/
extern const map<string, CellInitializer> cellMap;
/*
* this map is used to setup default simulations in longqt add your new cell to
* this map to give it a meaningful default simulation. We typically pace to
* study-state ~500,000 ms and output values for the last 5,000 ms
*/
extern const map<string, list<pair<string,string>>> protocolCellDefaults;
//declare the ProtocolIntializer type
typedef function<shared_ptr<Protocol>(void)> ProtocolInitializer;
/*
* map of known protocols used to create new instances of protocols in
* longqt
*/
extern const map<string, ProtocolInitializer> protoMap;
/*
* Side provides a consistent system for numbering sides in 2D grids
*/
enum Side {
top = 0,
right = 1,
bottom = 2,
left = 3
};
void set_default_vals(Protocol& proto);
/*reads in until the next StartElement with name name
*returns:
* True if it is found
* False if eof or error
*/
inline bool readNext(QXmlStreamReader& xml, QString name) {
if(xml.name() == name && xml.tokenType() == QXmlStreamReader::StartElement) {
return true;
}
while(!xml.atEnd()) {
QXmlStreamReader::TokenType t = xml.readNext();
if(xml.name() == name && t == QXmlStreamReader::StartElement) {
return true;
} else if(xml.name() == name && t == QXmlStreamReader::EndElement) {
return false;
}
}
return false;
}
/*
* reads until xml tree is one level higher
* returns:
* True if it is found
* False if eof or error
*/
inline bool readUpLevel(QXmlStreamReader& xml) {
int depth = 1;
while(!xml.atEnd()) {
QXmlStreamReader::TokenType t = xml.readNext();
if(t == QXmlStreamReader::StartElement) {
++depth;
} else if(t == QXmlStreamReader::EndElement) {
--depth;
}
if(depth < 1) {
return true;
}
}
return false;
}
//trim whitespace from beginning and end of a string
inline string trim(string str)
{
string toFind = " \t\n\v\f\r";
str.erase(0, str.find_first_not_of(toFind));
str.erase(str.find_last_not_of(toFind)+1);
return str;
}
/* maps a bool to string
* b:true -> "true"
* b:false-> "false"
*/
inline string to_string(const bool& b) {
return b ? "true" : "false";
}
/* maps a string to a bool
* s:"true" -> true
* s:"false" -> false
*/
inline bool stob(const string& s) {
return (strcasecmp("true",trim(s).c_str()) == 0);
}
std::string strprintf(const char * format, ...);
}
#endif