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 
5 Copyright (C) 2009 Ruediger Berndt
6 Copyright (C) 2010 Thomas Moor
7 
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12 
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
21 
22 
23 #include "cfl_elementary.h"
24 
25 namespace faudes{
26 
27 
28 
29 /*
30 ********************************************************************
31 ********************************************************************
32 ********************************************************************
33 
34 Implementation 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 
59 const Integer* Integer::Cast(const Type* pOther) const{
60  return dynamic_cast<const Integer*>(pOther);
61 }
62 
63 void Integer::CValue(Int val){
64  mCInteger = val;
65 }
66 
68  return(mCInteger);
69 }
70 
72  return &mCInteger;
73 }
74 
75 void 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);
85  rTw.WriteFloat(mCInteger);
86  rTw.WriteEnd(label);
87 }
88 
89 void 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
102 long int IntegerSum(const Integer& arg1, const Integer& arg2) {
103  return arg1+arg2;
104 }
105 
106 // integer sum, uniform rti api
107 long 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 
120 Implementation of class String
121 
122 ********************************************************************
123 ********************************************************************
124 ********************************************************************
125 */
126 
127 
128 // constructor
130  CValue("");
131 }
132 
133 // constructor
134 String::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
149 const String* String::Cast(const Type* pOther) const{
150  return dynamic_cast<const String*>(pOther);
151 }
152 
153 // c value
154 std::string String::CValue() const{
155  return mCString;
156 }
157 
158 // c value
159 void String::CValue(std::string s){
160  mCString = s;
161 }
162 
163 // c ref
164 std::string* String::CReference() {
165  return &mCString;
166 }
167 
168 // token io
169 void 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);
183  rTw.WriteString(mCString);
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
193 void 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 
209 Implementation of class Boolean
210 
211 ********************************************************************
212 ********************************************************************
213 ********************************************************************
214 */
215 
216 
217 // constructor
219  CValue(true);
220 }
221 
222 // constructor
223 Boolean::Boolean(bool val) {
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
238 const Boolean* Boolean::Cast(const Type* pOther) const{
239  return dynamic_cast<const Boolean*>(pOther);
240 }
241 
242 // cvaliu
243 void Boolean::CValue(bool val){
244  mCBool = val;
245 }
246 
247 // cvalue
248 bool Boolean::CValue() const{
249  return(mCBool);
250 }
251 
252 // c ref
254  return &mCBool;
255 }
256 
257 // token io
258 void 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
275 void 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)
Debug: optional on function and type definition.
Runtime interface, elementary types.
Elementary type.
bool mCBool
Variable to store current value.
bool * CReference(void)
Get reference.
virtual const Boolean * Cast(const Type *pOther) const
Cast the other object to Boolean.
void DoRead(TokenReader &rTr, const std::string &rLabel="", const Type *pContext=0)
Read data from TokenReader.
bool CValue(void) const
Get value.
void DoWrite(TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=0) const
Write data to Tokenwriter.
virtual Boolean * Copy(void) const
Construct copy on heap.
virtual Boolean * New(void) const
Construct on heap.
Boolean(void)
Constructor.
Faudes exception class.
Elementary type.
Integer(void)
Constructor.
void DoRead(TokenReader &rTr, const std::string &rLabel="", const Type *pContext=0)
Read data from TokenReader.
Int mCInteger
Variable to store current value.
virtual Integer * New(void) const
Construct on heap.
void DoWrite(TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=0) const
Reimplementation of faudes::Type::DoWrite() Write data to Tokenwriter.
virtual const Integer * Cast(const Type *pOther) const
Cast the other object to Integer.
Int CValue(void) const
Get value.
Int * CReference(void)
Get reference.
virtual Integer * Copy(void) const
Construct copy on heap.
Elementary type.
String(void)
Constructor.
void DoRead(TokenReader &rTr, const std::string &rLabel="", const Type *pContext=0)
Read data from TokenReader.
std::string mCString
Variable to store current value.
std::string CValue(void) const
Get value.
std::string * CReference(void)
Get reference.
void DoWrite(TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=0) const
Write data to Tokenwriter.
virtual String * New(void) const
Construct on heap.
virtual const String * Cast(const Type *pOther) const
Cast the other object to String.
virtual String * Copy(void) const
Construct copy on heap.
Vector template.
virtual const T & At(const Position &pos) const
Access element.
A TokenReader reads sequential tokens from a file or string.
std::string FileLine(void) const
Return "filename:line".
void ReadVerbatim(const std::string &rLabel, std::string &rText)
Read verbatim text.
void ReadEnd(const std::string &rLabel)
Close the current section by matching the previous ReadBegin().
double ReadFloat(void)
Read float token.
std::string ReadString(void)
Read string token.
void ReadBegin(const std::string &rLabel)
Open a section by specified label.
std::string FileName(void) const
Access the filename.
A TokenWriter writes sequential tokens to a file, a string or stdout.
std::string FileName(void) const
Get the filename.
void WriteFloat(const double &val)
Write float.
void Write(Token &rToken)
Write next token.
void WriteString(const std::string &rString)
Write string.
void WriteEnd(const std::string &rLabel)
Write end label.
void WriteVerbatim(Token &rBeginTag, const std::string &rText)
Write verbatim text section.
Tokens model atomic data for stream IO.
Definition: cfl_token.h:53
void SetBegin(const std::string &rName)
Initialize as Begin token.
Definition: cfl_token.cpp:91
void InsAttributeString(const std::string &name, const std::string &value)
Insert named attribute with string value.
Definition: cfl_token.cpp:309
Base class of all libFAUDES objects that participate in the run-time interface.
Definition: cfl_types.h:239
Idx Size(void) const
Get size of vector.
libFAUDES resides within the namespace faudes.
long int IntegerSum(const Integer &arg1, const Integer &arg2)
long int Int
Type definition for integer type (let target system decide, minimum 32bit)

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen