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
28// register
32
33
34/*
35********************************************************************
36********************************************************************
37********************************************************************
38
39Implementation of class Integer
40
41********************************************************************
42********************************************************************
43********************************************************************
44*/
45
46
47// public:
49 CValue(0);
50}
51
53 CValue(val);
54}
55
57 return(new Integer());
58}
59
61 return(new Integer(mCInteger));
62}
63
64const Integer* Integer::Cast(const Type* pOther) const{
65 return dynamic_cast<const Integer*>(pOther);
66}
67
69 mCInteger = val;
70}
71
73 return(mCInteger);
74}
75
77 return &mCInteger;
78}
79
80void Integer::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const{
81 (void) pContext;
82 std::string label=rLabel;
83 std::string ftype="Integer";
84 if(label=="") label=ftype;
85 Token btag;
86 btag.SetBegin(label);
87 if(ftype!=label) btag.InsAttributeString("ftype",ftype);
88 FD_DRTI("Integer::DoWrite(): file " << rTw.FileName() << " section " << label);
89 rTw.Write(btag);
91 rTw.WriteEnd(label);
92}
93
94void Integer::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
95 (void) pContext;
96 std::string label = rLabel;
97 if(label == "") label = "Integer";
98 FD_DRTI("Integer()::DoRead(): file " << rTr.FileName() << " section " << label);
99 rTr.ReadBegin(label);
100 mCInteger = (Int)rTr.ReadFloat();
101 rTr.ReadEnd(label);
102 FD_DRTI("Integer::DoRead(): done");
103}
104
105
106// integer sum, uniform rti api
107long int IntegerSum(const Integer& arg1, const Integer& arg2) {
108 return arg1+arg2;
109}
110
111// integer sum, uniform rti api
112long int IntegerSum(const IntegerVector& intvect) {
113 long int res=0;
114 for(unsigned int i=0; i< intvect.Size(); i++)
115 res+=intvect.At(i);
116 return res;
117}
118
119
120/*
121********************************************************************
122********************************************************************
123********************************************************************
124
125Implementation of class String
126
127********************************************************************
128********************************************************************
129********************************************************************
130*/
131
132
133// constructor
135 CValue("");
136}
137
138// constructor
139String::String(std::string val) {
140 CValue(val);
141}
142
143// factory constructor
145 return new String();
146}
147
148// factory constructor
150 return new String(mCString);
151}
152
153// cast
154const String* String::Cast(const Type* pOther) const{
155 return dynamic_cast<const String*>(pOther);
156}
157
158// c value
159std::string String::CValue() const{
160 return mCString;
161}
162
163// c value
164void String::CValue(std::string s){
165 mCString = s;
166}
167
168// c ref
169std::string* String::CReference() {
170 return &mCString;
171}
172
173// token io
174void String::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const{
175 (void) pContext;
176 // write begin tag
177 std::string label=rLabel;
178 std::string ftype="String";
179 if(label=="") label=ftype;
180 Token btag;
181 btag.SetBegin(label);
182 if(ftype!=label) btag.InsAttributeString("ftype",ftype);
183 FD_DRTI("String::DoWrite(): file " << rTw.FileName() << " section " << label);
184 // write as possibly quoted string token: no escape characters, no formating
185 std::string escstr="<>&\n\r\v";
186 if(mCString.find_first_of(escstr)==std::string::npos) {
187 rTw.Write(btag);
189 rTw.WriteEnd(label);
190 }
191 // nontrivial perhaps longe string: write section with CDATA elements
192 else {
193 rTw.WriteVerbatim(btag, mCString);
194 }
195}
196
197// token io
198void String::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
199 (void) pContext;
200 std::string label = rLabel;
201 if(label == "") label = "String";
202 FD_DRTI("String::DoRead(): file " << rTr.FileName() << " section " << label);
203 // note: ReadVerbatim will read both plain or verbatim string
204 rTr.ReadVerbatim(label, mCString);
205 FD_DRTI("String::DoRead(): done");
206}
207
208
209/*
210********************************************************************
211********************************************************************
212********************************************************************
213
214Implementation of class Boolean
215
216********************************************************************
217********************************************************************
218********************************************************************
219*/
220
221
222// constructor
224 CValue(true);
225}
226
227// constructor
229 CValue(val);
230}
231
232// factory constructor
234 return new Boolean();
235}
236
237// factory constructor
239 return new Boolean(mCBool);
240}
241
242// cast
243const Boolean* Boolean::Cast(const Type* pOther) const{
244 return dynamic_cast<const Boolean*>(pOther);
245}
246
247// cvaliu
248void Boolean::CValue(bool val){
249 mCBool = val;
250}
251
252// cvalue
253bool Boolean::CValue() const{
254 return(mCBool);
255}
256
257// c ref
259 return &mCBool;
260}
261
262// token io
263void Boolean::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const{
264 (void) pContext;
265 std::string label=rLabel;
266 std::string ftype="Boolean";
267 if(label=="") label=ftype;
268 Token btag;
269 btag.SetBegin(label);
270 if(ftype!=label) btag.InsAttributeString("ftype",ftype);
271 FD_DRTI("String::DoWrite(): file " << rTw.FileName() << " section " << label);
272 rTw.Write(btag);
273 if(mCBool) rTw.WriteString("true");
274 else rTw.WriteString("false");
275 rTw.WriteEnd(label);
276}
277
278
279// token io
280void Boolean::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
281 (void) pContext;
282 std::string label = rLabel;
283 if(label == "") label = "Boolean";
284 FD_DRTI("Boolean::DoRead(): file " << rTr.FileName() << " section " << label);
285 rTr.ReadBegin(label);
286 std::string value = rTr.ReadString();
287 std::transform(value.begin(), value.end(), value.begin(), tolower);
288 if(value=="true") mCBool=true;
289 else if(value=="false") mCBool=false;
290 else {
291 std::stringstream err;
292 err << "Expected true or false: " << rTr.FileLine();
293 throw Exception("Boolean::DoRead()", err.str(), 52);
294 }
295 rTr.ReadEnd(label);
296 FD_DRTI("Boolean::DoRead(): done");
297}
298
299
300
301} //namspace
302
#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.34d --- 2026.03.11 --- c++ api documentaion by doxygen