Documentation for OpenFLUID 2.2.0
StringValue.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 
33 /**
34  @file StringValue.hpp
35 
36  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37  */
38 
39 
40 #ifndef __OPENFLUID_CORE_STRINGVALUE_HPP__
41 #define __OPENFLUID_CORE_STRINGVALUE_HPP__
42 
43 
44 #include <string>
45 #include <vector>
46 
48 #include <openfluid/dllexport.hpp>
49 
50 
51 namespace openfluid { namespace core {
52 
53 
54 bool OPENFLUID_API stringToBoolean(const std::string& Str);
55 // first defined here and not in tools because of circular import issue
56 
57 
58 /**
59  StringValue is a container for a std::string value, with methods for conversion
60  to other containers derived from Value.\n
61 
62 @see Value
63 
64  <I>Example : declaration</I>
65  @snippet misc/values.cpp string_decl
66 
67  <I>Example : getting the contained value</I>
68  @snippet misc/values.cpp string_get
69 
70  <I>Example : setting the contained value</I>
71  @snippet misc/values.cpp string_set
72 
73  @cond OpenFLUID:completion
74  {
75  "contexts" : ["ANYWARE"],
76  "menupath" : ["Types", "Values"],
77  "title" : "StringValue",
78  "text" : "openfluid::core::StringValue %%SEL_START%%Val%%SEL_END%%"
79  }
80  @endcond
81 */
83 {
84  private:
85 
86  std::string m_Value;
87 
88  static bool convertStringToDouble(const std::string& Str, double& Dbl);
89 
90  static std::vector<std::string> splitString(const std::string& StrToSplit,
91  const std::string& Separators,
92  bool ReturnsEmpty = false);
93 
94  std::vector<std::string> split(const std::string& Separators,
95  bool ReturnsEmpty = false) const;
96 
97 
98  public:
99 
100  /**
101  Default constructor
102  */
103  StringValue() : SimpleValue(), m_Value("")
104  { }
105 
106  /**
107  Copy constructor
108  */
110  m_Value(Val.m_Value)
111  { }
112 
113  /**
114  Constructor from char*
115  */
116  StringValue(const char* Val) : SimpleValue(), m_Value(std::string(Val))
117  {
118 
119  }
120 
121  /**
122  Constructor from std::string
123  */
124  StringValue(const std::string& Val) : SimpleValue(), m_Value(Val)
125  {
126 
127  }
128 
129  /**
130  Constructor from bool
131  */
132  StringValue(bool Val);
133 
134  /**
135  Constructor from int
136  */
137  StringValue(int Val);
138 
139  /**
140  Constructor from double
141  */
142  StringValue(double Val);
143 
144  StringValue& operator=(const Value& Other) override;
145 
146  StringValue& operator=(Value&& Other) override;
147 
148  StringValue& operator=(const StringValue& Other) = default;
149 
150  StringValue& operator=(StringValue&& Other) = default;
151 
152  virtual ~StringValue() = default;
153 
154 
155  /**
156  Cast operator
157  */
158  operator std::string() const
159  {
160  return m_Value;
161  }
162 
163  inline Type getType() const override
164  {
165  return Value::STRING;
166  }
167 
168  Value* clone() const override
169  {
170  return new StringValue(*this);
171  }
172 
173  bool convert(Value& Val) const override;
174 
175  /**
176  Returns the string value as std::string type
177  @return the string value
178  */
179  inline std::string get() const
180  {
181  return m_Value;
182  }
183 
184  /**
185  Returns a reference to the string value as std::string type
186  @return the string value
187  */
188  inline std::string& data()
189  {
190  return m_Value;
191  }
192 
193  /**
194  Returns a reference to the string value as std::string type
195  @return the string value
196  */
197  inline const std::string& data() const
198  {
199  return m_Value;
200  }
201 
202  /**
203  Sets the string value
204  @param[in] Val the string value
205  */
206  inline void set(const std::string& Val)
207  {
208  m_Value = Val;
209  }
210 
211  inline void clear()
212  {
213  m_Value.clear();
214  }
215 
216  void writeToStream(std::ostream& OutStm) const override;
217 
218  void writeQuotedToStream(std::ostream& OutStm) const override
219  {
220  OutStm << "\"" ; writeToStream(OutStm); OutStm << "\"" ;
221  }
222 
223  /**
224  Returns the size of the string
225  @return size of the string
226  */
227  inline unsigned long getSize() const
228  {
229  return m_Value.size();
230  }
231 
232  /**
233  Returns the size of the string
234  @return size of the string
235  */
236  unsigned long size() const
237  {
238  return m_Value.size();
239  }
240 
241 
242  /**
243  Replaces all occurences of FindStr by ReplaceStr
244  @param[in] FindStr the substring to find
245  @param[in] ReplaceStr the substring to replace the found substrings
246  @return the number of occurences
247  */
248  unsigned int replaceAll(const std::string& FindStr,const std::string& ReplaceStr);
249 
250 
251  /**
252  Try to find the the most adapted type for conversion
253  @return the most adapted type for conversion (if the value is empty)
254  */
256 
257 
258  /**
259  Converts the contained string to a double value (if possible)
260  @return bool true if the conversion is correct, false otherwise
261  */
262  bool toDouble(double& Val) const;
263 
264  /**
265  Converts the contained string to a DoubleValue (if possible)
266  @param[out] Val the converted value
267  @return bool true if the conversion is correct, false otherwise
268  */
269  bool toDoubleValue(DoubleValue& Val) const;
270 
271  /**
272  Converts the contained string to a boolean value (if possible)
273  @param[out] Val the converted value
274  @return bool true if the conversion is correct, false otherwise
275  */
276  bool toBoolean(bool& Val) const;
277 
278  /**
279  Converts the contained string to a BooleanValue (if possible)
280  @param[out] Val the converted value
281  @return bool true if the conversion is correct, false otherwise
282  */
283  bool toBooleanValue(BooleanValue& Val) const;
284 
285  /**
286  Converts the contained string to an int value (if possible)
287  @param[out] Val the converted value
288  @return bool true if the conversion is correct, false otherwise
289  */
290  bool toInteger(int& Val) const;
291 
292  /**
293  Converts the contained string to a long value (if possible)
294  @param[out] Val the converted value
295  @return bool true if the conversion is correct, false otherwise
296  */
297  bool toInteger(long& Val) const;
298 
299  /**
300  Converts the contained string to an IntegerValue (if possible)
301  @param[out] Val the converted value
302  @return bool true if the conversion is correct, false otherwise
303  */
304  bool toIntegerValue(IntegerValue& Val) const;
305 
306  /**
307  Converts the contained string to a NullValue (if possible)
308  @param[out] Val the converted value
309  @return bool true if the conversion is correct, false otherwise
310  */
311  bool toNullValue(NullValue& Val) const;
312 
313  /**
314  Converts the contained string to a VectorValue value (if possible)
315  @param[out] Val the converted value
316  @return bool true if the conversion is correct, false otherwise
317  */
318  bool toVectorValue(VectorValue& Val) const;
319 
320  /**
321  Converts the contained string to a MatrixValue value (if possible)
322  @param[out] Val the converted value
323  @return bool true if the conversion is correct, false otherwise
324  */
325  bool toMatrixValue(MatrixValue& Val) const;
326 
327  /**
328  Converts the contained string to a MatrixValue value (if possible)
329  @param[in] RowLength the size of a row
330  @param[out] Val the converted value
331  @return bool true if the conversion is correct, false otherwise
332  */
333  bool toMatrixValue(const unsigned int& RowLength, MatrixValue& Val) const;
334 
335  /**
336  Converts the contained string to a MapValue value (if possible)
337  @param[out] Val the converted value
338  @return bool true if the conversion is correct, false otherwise
339  */
340  bool toMapValue(MapValue& Val) const;
341 
342  /**
343  Converts the contained string to a TreeValue value (if possible)
344  @param[out] Val the converted value
345  @return bool true if the conversion is correct, false otherwise
346  @throw FrameworkException when called
347  @warning This method is currently not implement and always throws a FrameworkException
348  */
349  bool toTreeValue(TreeValue& Val) const;
350 
351 };
352 
353 
354 } } // namespaces
355 
356 
357 #endif /* __OPENFLUID_CORE_STRINGVALUE_HPP__ */
Definition: BooleanValue.hpp:81
Definition: DoubleValue.hpp:81
Definition: IntegerValue.hpp:80
Definition: MapValue.hpp:93
Definition: MatrixValue.hpp:85
Definition: NullValue.hpp:66
Definition: SimpleValue.hpp:52
Definition: StringValue.hpp:83
bool toMapValue(MapValue &Val) const
StringValue & operator=(const Value &Other) override
bool toInteger(int &Val) const
std::string & data()
Definition: StringValue.hpp:188
bool toBoolean(bool &Val) const
Value::Type guessTypeConversion() const
bool toMatrixValue(MatrixValue &Val) const
void writeQuotedToStream(std::ostream &OutStm) const override
Definition: StringValue.hpp:218
unsigned long size() const
Definition: StringValue.hpp:236
StringValue & operator=(const StringValue &Other)=default
std::string get() const
Definition: StringValue.hpp:179
bool toNullValue(NullValue &Val) const
bool toDoubleValue(DoubleValue &Val) const
StringValue(const char *Val)
Definition: StringValue.hpp:116
bool toMatrixValue(const unsigned int &RowLength, MatrixValue &Val) const
StringValue(const std::string &Val)
Definition: StringValue.hpp:124
StringValue(const StringValue &Val)
Definition: StringValue.hpp:109
StringValue()
Definition: StringValue.hpp:103
unsigned int replaceAll(const std::string &FindStr, const std::string &ReplaceStr)
bool toDouble(double &Val) const
virtual ~StringValue()=default
void clear()
Definition: StringValue.hpp:211
bool toInteger(long &Val) const
bool toIntegerValue(IntegerValue &Val) const
bool toVectorValue(VectorValue &Val) const
bool toBooleanValue(BooleanValue &Val) const
bool toTreeValue(TreeValue &Val) const
void set(const std::string &Val)
Definition: StringValue.hpp:206
unsigned long getSize() const
Definition: StringValue.hpp:227
Value * clone() const override
Definition: StringValue.hpp:168
void writeToStream(std::ostream &OutStm) const override
const std::string & data() const
Definition: StringValue.hpp:197
StringValue & operator=(Value &&Other) override
StringValue & operator=(StringValue &&Other)=default
bool convert(Value &Val) const override
Type getType() const override
Definition: StringValue.hpp:163
Definition: TreeValue.hpp:66
Definition: Value.hpp:63
Type
Definition: Value.hpp:66
@ STRING
Definition: Value.hpp:66
Definition: VectorValue.hpp:85
#define OPENFLUID_API
Definition: dllexport.hpp:86
bool OPENFLUID_API stringToBoolean(const std::string &Str)
std::vector< std::string > OPENFLUID_API split(const std::string &Str, const char Sep, bool KeepEmpty=false)
std::vector< std::string > OPENFLUID_API splitString(const std::string &StrToSplit, const std::string &Separators, bool ReturnsEmpty=false)
Definition: ApplicationException.hpp:47