cfl_elementary.cpp
Go to the documentation of this file.
1/** @file cfl_elementary.cpp Runtime interface, elementary types */
2
3/* FAU Discrete Event Systems Library (libfaudes)
4
5Copyright (C) 2009 Ruediger Berndt
6Copyright (C) 2010 Thomas Moor
7
8This library is free software; you can redistribute it and/or
9modify it under the terms of the GNU Lesser General Public
10License as published by the Free Software Foundation; either
11version 2.1 of the License, or (at your option) any later version.
12
13This library is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16Lesser General Public License for more details.
17
18You should have received a copy of the GNU Lesser General Public
19License along with this library; if not, write to the Free Software
20Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
21
22
23#include "cfl_elementary.h"
24
25namespace faudes{
26
27// register
31
32
33/*
34********************************************************************
35********************************************************************
36********************************************************************
37
38Implementation of class Integer
39
40********************************************************************
41********************************************************************
42********************************************************************
43*/
44
45
46// public:
48 CValue(0);
49}
50
52 CValue(val);
53}
54
56 return(new Integer());
57}
58
60 return(new Integer(mCInteger));
61}
62
63const Integer* Integer::Cast(const Type* pOther) const{
64 return dynamic_cast<const Integer*>(pOther);
65}
66
68 mCInteger = val;
69}
70
72 return(mCInteger);
73}
74
76 return &mCInteger;
77}
78
79void Integer::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const{
80 (void) pContext;
81 std::string label=rLabel;
82 std::string ftype="Integer";
83 if(label=="") label=ftype;
84 Token btag;
85 btag.SetBegin(label);
86 if(ftype!=label) btag.InsAttributeString("ftype",ftype);
87 FD_DRTI("Integer::DoWrite(): file " << rTw.FileName() << " section " << label);
88 rTw.Write(btag);
90 rTw.WriteEnd(label);
91}
92
93void Integer::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
94 (void) pContext;
95 std::string label = rLabel;
96 if(label == "") label = "Integer";
97 FD_DRTI("Integer()::DoRead(): file " << rTr.FileName() << " section " << label);
98 rTr.ReadBegin(label);
99 mCInteger = (Int)rTr.ReadFloat();
100 rTr.ReadEnd(label);
101 FD_DRTI("Integer::DoRead(): done");
102}
103
104
105// integer sum, uniform rti api
106long int IntegerSum(const Integer& arg1, const Integer& arg2) {
107 return arg1+arg2;
108}
109
110// integer sum, uniform rti api
111long int IntegerSum(const IntegerVector& intvect) {
112 long int res=0;
113 for(unsigned int i=0; i< intvect.Size(); i++)
114 res+=intvect.At(i);
115 return res;
116}
117
118
119/*
120********************************************************************
121********************************************************************
122********************************************************************
123
124Implementation of class String
125
126********************************************************************
127********************************************************************
128********************************************************************
129*/
130
131
132// constructor
134 CValue("");
135}
136
137// constructor
138String::String(std::string val) {
139 CValue(val);
140}
141
142// factory constructor
144 return new String();
145}
146
147// factory constructor
149 return new String(mCString);
150}
151
152// cast
153const String* String::Cast(const Type* pOther) const{
154 return dynamic_cast<const String*>(pOther);
155}
156
157// c value
158std::string String::CValue() const{
159 return mCString;
160}
161
162// c value
163void String::CValue(std::string s){
164 mCString = s;
165}
166
167// c ref
168std::string* String::CReference() {
169 return &mCString;
170}
171
172// token io
173void String::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const{
174 (void) pContext;
175 // write begin tag
176 std::string label=rLabel;
177 std::string ftype="String";
178 if(label=="") label=ftype;
179 Token btag;
180 btag.SetBegin(label);
181 if(ftype!=label) btag.InsAttributeString("ftype",ftype);
182 FD_DRTI("String::DoWrite(): file " << rTw.FileName() << " section " << label);
183 // write as possibly quoted string token: no escape characters, no formating
184 std::string escstr="<>&\n\r\v";
185 if(mCString.find_first_of(escstr)==std::string::npos) {
186 rTw.Write(btag);
188 rTw.WriteEnd(label);
189 }
190 // nontrivial perhaps longe string: write section with CDATA elements
191 else {
192 rTw.WriteVerbatim(btag, mCString);
193 }
194}
195
196// token io
197void String::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
198 (void) pContext;
199 std::string label = rLabel;
200 if(label == "") label = "String";
201 FD_DRTI("String::DoRead(): file " << rTr.FileName() << " section " << label);
202 // note: ReadVerbatim will read both plain or verbatim string
203 rTr.ReadVerbatim(label, mCString);
204 FD_DRTI("String::DoRead(): done");
205}
206
207
208/*
209********************************************************************
210********************************************************************
211********************************************************************
212
213Implementation of class Boolean
214
215********************************************************************
216********************************************************************
217********************************************************************
218*/
219
220
221// constructor
223 CValue(true);
224}
225
226// constructor
228 CValue(val);
229}
230
231// factory constructor
233 return new Boolean();
234}
235
236// factory constructor
238 return new Boolean(mCBool);
239}
240
241// cast
242const Boolean* Boolean::Cast(const Type* pOther) const{
243 return dynamic_cast<const Boolean*>(pOther);
244}
245
246// cvaliu
247void Boolean::CValue(bool val){
248 mCBool = val;
249}
250
251// cvalue
252bool Boolean::CValue() const{
253 return(mCBool);
254}
255
256// c ref
258 return &mCBool;
259}
260
261// token io
262void Boolean::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const{
263 (void) pContext;
264 std::string label=rLabel;
265 std::string ftype="Boolean";
266 if(label=="") label=ftype;
267 Token btag;
268 btag.SetBegin(label);
269 if(ftype!=label) btag.InsAttributeString("ftype",ftype);
270 FD_DRTI("String::DoWrite(): file " << rTw.FileName() << " section " << label);
271 rTw.Write(btag);
272 if(mCBool) rTw.WriteString("true");
273 else rTw.WriteString("false");
274 rTw.WriteEnd(label);
275}
276
277
278// token io
279void Boolean::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
280 (void) pContext;
281 std::string label = rLabel;
282 if(label == "") label = "Boolean";
283 FD_DRTI("Boolean::DoRead(): file " << rTr.FileName() << " section " << label);
284 rTr.ReadBegin(label);
285 std::string value = rTr.ReadString();
286 std::transform(value.begin(), value.end(), value.begin(), tolower);
287 if(value=="true") mCBool=true;
288 else if(value=="false") mCBool=false;
289 else {
290 std::stringstream err;
291 err << "Expected true or false: " << rTr.FileLine();
292 throw Exception("Boolean::DoRead()", err.str(), 52);
293 }
294 rTr.ReadEnd(label);
295 FD_DRTI("Boolean::DoRead(): done");
296}
297
298
299
300} //namspace
301
#define FD_DRTI(message)
virtual Boolean * NewCpy(void) const
bool * CReference(void)
virtual const Boolean * Cast(const Type *pOther) const
void DoRead(TokenReader &rTr, const std::string &rLabel="", const Type *pContext=0)
bool CValue(void) const
void DoWrite(TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=0) const
virtual Boolean * New(void) const
void DoRead(TokenReader &rTr, const std::string &rLabel="", const Type *pContext=0)
virtual Integer * NewCpy(void) const
virtual Integer * New(void) const
void DoWrite(TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=0) const
virtual const Integer * Cast(const Type *pOther) const
Int CValue(void) const
Int * CReference(void)
virtual String * NewCpy(void) const
void DoRead(TokenReader &rTr, const std::string &rLabel="", const Type *pContext=0)
std::string mCString
std::string CValue(void) const
std::string * CReference(void)
void DoWrite(TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=0) const
virtual String * New(void) const
virtual const String * Cast(const Type *pOther) const
virtual const T & At(const Position &pos) const
std::string FileLine(void) const
void ReadVerbatim(const std::string &rLabel, std::string &rText)
void ReadEnd(const std::string &rLabel)
std::string ReadString(void)
void ReadBegin(const std::string &rLabel)
std::string FileName(void) const
std::string FileName(void) const
void WriteFloat(const double &val)
void Write(Token &rToken)
void WriteString(const std::string &rString)
void WriteEnd(const std::string &rLabel)
void WriteVerbatim(Token &rBeginTag, const std::string &rText)
void SetBegin(const std::string &rName)
Definition cfl_token.cpp:92
void InsAttributeString(const std::string &name, const std::string &value)
AutoRegisterType< Boolean > gRtiBoolean("Boolean")
long int IntegerSum(const Integer &arg1, const Integer &arg2)
AutoRegisterType< Integer > gRtiInteger("Integer")
AutoRegisterType< String > gRtiString("String")
long int Int

libFAUDES 2.34g --- 2026.03.30 --- c++ api documentaion by doxygen