Manual for OpenFLUID 2.1.11

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  StringValue is a container for a std::string value, with methods for conversion
54  to other containers derived from Value.\n
55 
56 @see Value
57 
58  <I>Example : declaration</I>
59  @snippet misc/values.cpp string_decl
60 
61  <I>Example : getting the contained value</I>
62  @snippet misc/values.cpp string_get
63 
64  <I>Example : setting the contained value</I>
65  @snippet misc/values.cpp string_set
66 
67  @cond OpenFLUID:completion
68  {
69  "contexts" : ["ANYWARE"],
70  "menupath" : ["Compute code", "Types", "Values"],
71  "title" : "StringValue",
72  "text" : "openfluid::core::StringValue %%SEL_START%%Val%%SEL_END%%"
73  }
74  @endcond
75 */
77 {
78  private:
79 
80  std::string m_Value;
81 
82  static bool convertStringToDouble(const std::string& Str, double& Dbl);
83 
84  static std::vector<std::string> splitString(const std::string& StrToSplit,
85  const std::string& Separators,
86  bool ReturnsEmpty = false);
87 
88  std::vector<std::string> split(const std::string& Separators,
89  bool ReturnsEmpty = false) const;
90 
91 
92  public:
93 
94  /**
95  Default constructor
96  */
97  StringValue() : SimpleValue(), m_Value("")
98  { }
99 
100  /**
101  Copy constructor
102  */
104  m_Value(Val.m_Value)
105  { }
106 
107  /**
108  Constructor from char*
109  */
110  StringValue(const char* Val) : SimpleValue(), m_Value(std::string(Val))
111  {
112 
113  }
114 
115  /**
116  Constructor from std::string
117  */
118  StringValue(const std::string& Val) : SimpleValue(), m_Value(Val)
119  {
120 
121  }
122 
123  /**
124  Constructor from bool
125  */
126  StringValue(bool Val);
127 
128  /**
129  Constructor from int
130  */
131  StringValue(int Val);
132 
133  /**
134  Constructor from double
135  */
136  StringValue(double Val);
137 
138  StringValue& operator=(const Value& Other) override;
139 
140  StringValue& operator=(Value&& Other) override;
141 
142  StringValue& operator=(const StringValue& Other) = default;
143 
144  StringValue& operator=(StringValue&& Other) = default;
145 
146  virtual ~StringValue() = default;
147 
148 
149  /**
150  Cast operator
151  */
152  operator std::string() const
153  {
154  return m_Value;
155  }
156 
157  inline Type getType() const override
158  {
159  return Value::STRING;
160  }
161 
162  Value* clone() const override
163  {
164  return new StringValue(*this);
165  }
166 
167  bool convert(Value& Val) const override;
168 
169  /**
170  Returns the string value as std::string type
171  @return the string value
172  */
173  inline std::string get() const
174  {
175  return m_Value;
176  }
177 
178  /**
179  Returns a reference to the string value as std::string type
180  @return the string value
181  */
182  inline std::string& data()
183  {
184  return m_Value;
185  }
186 
187  /**
188  Returns a reference to the string value as std::string type
189  @return the string value
190  */
191  inline const std::string& data() const
192  {
193  return m_Value;
194  }
195 
196  /**
197  Sets the string value
198  @param[in] Val the string value
199  */
200  inline void set(const std::string& Val)
201  {
202  m_Value = Val;
203  }
204 
205  inline void clear()
206  {
207  m_Value.clear();
208  }
209 
210  void writeToStream(std::ostream& OutStm) const override;
211 
212  void writeQuotedToStream(std::ostream& OutStm) const override
213  {
214  OutStm << "\"" ; writeToStream(OutStm); OutStm << "\"" ;
215  }
216 
217  /**
218  Returns the size of the string
219  @return size of the string
220  */
221  inline unsigned long getSize() const
222  {
223  return m_Value.size();
224  }
225 
226  /**
227  Returns the size of the string
228  @return size of the string
229  */
230  unsigned long size() const
231  {
232  return m_Value.size();
233  }
234 
235 
236  /**
237  Replaces all occurences of FindStr by ReplaceStr
238  @param[in] FindStr the substring to find
239  @param[in] ReplaceStr the substring to replace the found substrings
240  @return the number of occurences
241  */
242  unsigned int replaceAll(const std::string& FindStr,const std::string& ReplaceStr);
243 
244 
245  /**
246  Try to find the the most adapted type for conversion
247  @return the most adapted type for conversion (if the value is empty)
248  */
249  Value::Type guessTypeConversion() const;
250 
251 
252  /**
253  Converts the contained string to a double value (if possible)
254  @return bool true if the conversion is correct, false otherwise
255  */
256  bool toDouble(double& Val) const;
257 
258  /**
259  Converts the contained string to a DoubleValue (if possible)
260  @param[out] Val the converted value
261  @return bool true if the conversion is correct, false otherwise
262  */
263  bool toDoubleValue(DoubleValue& Val) const;
264 
265  /**
266  Converts the contained string to a boolean value (if possible)
267  @param[out] Val the converted value
268  @return bool true if the conversion is correct, false otherwise
269  */
270  bool toBoolean(bool& Val) const;
271 
272  /**
273  Converts the contained string to a BooleanValue (if possible)
274  @param[out] Val the converted value
275  @return bool true if the conversion is correct, false otherwise
276  */
277  bool toBooleanValue(BooleanValue& Val) const;
278 
279  /**
280  Converts the contained string to an int value (if possible)
281  @param[out] Val the converted value
282  @return bool true if the conversion is correct, false otherwise
283  */
284  bool toInteger(int& Val) const;
285 
286  /**
287  Converts the contained string to a long value (if possible)
288  @param[out] Val the converted value
289  @return bool true if the conversion is correct, false otherwise
290  */
291  bool toInteger(long& Val) const;
292 
293  /**
294  Converts the contained string to an IntegerValue (if possible)
295  @param[out] Val the converted value
296  @return bool true if the conversion is correct, false otherwise
297  */
298  bool toIntegerValue(IntegerValue& Val) const;
299 
300  /**
301  Converts the contained string to a NullValue (if possible)
302  @param[out] Val the converted value
303  @return bool true if the conversion is correct, false otherwise
304  */
305  bool toNullValue(NullValue& Val) const;
306 
307  /**
308  Converts the contained string to a VectorValue value (if possible)
309  @param[out] Val the converted value
310  @return bool true if the conversion is correct, false otherwise
311  */
312  bool toVectorValue(VectorValue& Val) const;
313 
314  /**
315  Converts the contained string to a MatrixValue value (if possible)
316  @param[out] Val the converted value
317  @return bool true if the conversion is correct, false otherwise
318  */
319  bool toMatrixValue(MatrixValue& Val) const;
320 
321  /**
322  Converts the contained string to a MatrixValue value (if possible)
323  @param[in] RowLength the size of a row
324  @param[out] Val the converted value
325  @return bool true if the conversion is correct, false otherwise
326  */
327  bool toMatrixValue(const unsigned int& RowLength, MatrixValue& Val) const;
328 
329  /**
330  Converts the contained string to a MapValue value (if possible)
331  @param[out] Val the converted value
332  @return bool true if the conversion is correct, false otherwise
333  */
334  bool toMapValue(MapValue& Val) const;
335 
336  /**
337  Converts the contained string to a TreeValue value (if possible)
338  @param[out] Val the converted value
339  @return bool true if the conversion is correct, false otherwise
340  @throw FrameworkException when called
341  @warning This method is currently not implement and always throws a FrameworkException
342  */
343  bool toTreeValue(TreeValue& Val) const;
344 
345 };
346 
347 
348 } } // namespaces
349 
350 
351 #endif /* __OPENFLUID_CORE_STRINGVALUE_HPP__ */
openfluid::core::VectorValue
Definition: VectorValue.hpp:84
openfluid::core::MatrixValue
Definition: MatrixValue.hpp:84
openfluid::core::StringValue::set
void set(const std::string &Val)
Definition: StringValue.hpp:200
openfluid::core::Value
Definition: Value.hpp:62
OPENFLUID_API
#define OPENFLUID_API
Definition: dllexport.hpp:86
openfluid::core::StringValue::getType
Type getType() const override
Definition: StringValue.hpp:157
openfluid::core::StringValue::size
unsigned long size() const
Definition: StringValue.hpp:230
openfluid::core::StringValue::StringValue
StringValue(const StringValue &Val)
Definition: StringValue.hpp:103
openfluid::core::StringValue::StringValue
StringValue(const std::string &Val)
Definition: StringValue.hpp:118
openfluid::core::StringValue::get
std::string get() const
Definition: StringValue.hpp:173
openfluid::core::MapValue
Definition: MapValue.hpp:92
openfluid::core::StringValue::getSize
unsigned long getSize() const
Definition: StringValue.hpp:221
openfluid::core::StringValue::clear
void clear()
Definition: StringValue.hpp:205
SimpleValue.hpp
openfluid::core::Value::Type
Type
Definition: Value.hpp:66
openfluid::core::StringValue::clone
Value * clone() const override
Definition: StringValue.hpp:162
openfluid::core::IntegerValue
Definition: IntegerValue.hpp:79
openfluid::core::StringValue::writeQuotedToStream
void writeQuotedToStream(std::ostream &OutStm) const override
Definition: StringValue.hpp:212
openfluid::tools::splitString
std::vector< std::string > OPENFLUID_API splitString(const std::string &StrToSplit, const std::string &Separators, bool ReturnsEmpty=false)
openfluid::core::StringValue::StringValue
StringValue()
Definition: StringValue.hpp:97
openfluid::core::Value::STRING
@ STRING
Definition: Value.hpp:66
openfluid::core::DoubleValue
Definition: DoubleValue.hpp:80
openfluid::core::NullValue
Definition: NullValue.hpp:65
openfluid
Definition: ApplicationException.hpp:47
openfluid::core::StringValue::StringValue
StringValue(const char *Val)
Definition: StringValue.hpp:110
openfluid::core::StringValue::data
std::string & data()
Definition: StringValue.hpp:182
openfluid::core::StringValue::data
const std::string & data() const
Definition: StringValue.hpp:191
openfluid::core::BooleanValue
Definition: BooleanValue.hpp:80
openfluid::core::TreeValue
Definition: TreeValue.hpp:65
dllexport.hpp
openfluid::core::StringValue
Definition: StringValue.hpp:76
openfluid::core::SimpleValue
Definition: SimpleValue.hpp:51