forked from highperformancecoder/minsky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschemaHelper.h
120 lines (102 loc) · 3.42 KB
/
schemaHelper.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
/*
@copyright Steve Keen 2012
@author Russell Standish
This file is part of Minsky.
Minsky is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Minsky 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with Minsky. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SCHEMA_HELPER
#define SCHEMA_HELPER
#include "operation.h"
#include "variable.h"
#include "godleyTable.h"
#include "godleyIcon.h"
#include "ravelWrap.h"
#include "variableValue.h"
#include <xml_unpack_base.h>
template <class T>
ecolab::array<T> toArray(const std::vector<T>& v)
{
ecolab::array<T> a(v.size());
for (std::size_t i=0; i<v.size(); ++i) a[i]=v[i];
return a;
}
template <class T>
std::vector<T> toVector(const ecolab::array<T>& a)
{
std::vector<T> v(a.size());
for (std::size_t i=0; i<v.size(); ++i) v[i]=a[i];
return v;
}
template <class T>
std::vector<double> toDoubleVector(const ecolab::array<T>& a)
{
std::vector<double> v(a.size());
for (std::size_t i=0; i<v.size(); ++i) v[i]=a[i];
return v;
}
namespace minsky
{
/**
A bridge pattern to allow schemas to update private members of
various classes, whilst retaining desired
encapsulation. SchemaHelper is priveleged to allow access to
private parts of the class to be initialised, but should only be
used by schema classes.
*/
struct SchemaHelper
{
static void setPrivates
(minsky::GodleyTable& g, const vector<vector<string> >& data,
const vector<GodleyTable::AssetClass>& assetClass)
{
g.data=data;
g.m_assetClass=assetClass;
}
static void setPrivates
(minsky::GodleyIcon& g, const vector<vector<string> >& data,
const vector<GodleyTable::AssetClass>& assetClass)
{
setPrivates(g.table, data, assetClass);
}
static void setVariableDisplay(minsky::GodleyIcon& g, bool variableDisplay)
{
g.m_variableDisplay=variableDisplay;
}
static void setStockAndFlow(minsky::GodleyIcon& g,
const minsky::GodleyIcon::Variables& flowVars,
const minsky::GodleyIcon::Variables& stockVars)
{
g.m_flowVars=flowVars;
g.m_stockVars=stockVars;
}
static void initHandleState(minsky::Ravel& r, const ravel::RavelState& s)
{r.initState=s;}
};
template <class PreviousSchema, class CurrentSchema>
void loadSchema(CurrentSchema& currentSchema,
classdesc::xml_unpack_t& data, const std::string& rootElement)
{
xml_unpack(data, rootElement, currentSchema);
if (currentSchema.schemaVersion < currentSchema.version)
{
PreviousSchema prevSchema(data);
currentSchema=prevSchema;
}
else if (currentSchema.schemaVersion > currentSchema.version)
throw error("Minsky schema version %d not supported",currentSchema.schemaVersion);
}
/// decode ascii-encoded representation to binary data
classdesc::pack_t decode(const classdesc::CDATA&);
/// encode binary data to ascii-encoded
classdesc::CDATA encode(const classdesc::pack_t&);
}
#endif