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

libFAUDES 2.33k --- 2025.09.16 --- c++ api documentaion by doxygen