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  virtual ~StringValue()
139  {
140 
141  }
142 
143  /**
144  Assignment operator
145  */
146  Value& operator=(const Value& Other);
147 
148  /**
149  Cast operator
150  */
151  operator std::string() const
152  {
153  return m_Value;
154  }
155 
156  inline Type getType() const
157  {
158  return Value::STRING;
159  }
160 
161  Value* clone() const
162  {
163  return new StringValue(*this);
164  }
165 
166  bool convert(Value& Val) const;
167 
168  /**
169  Returns the string value as std::string type
170  @return the string value
171  */
172  inline std::string get() const
173  {
174  return m_Value;
175  }
176 
177  /**
178  Returns a reference to the string value as std::string type
179  @return the string value
180  */
181  inline std::string& data()
182  {
183  return m_Value;
184  }
185 
186  /**
187  Returns a reference to the string value as std::string type
188  @return the string value
189  */
190  inline const std::string& data() const
191  {
192  return m_Value;
193  }
194 
195  /**
196  Sets the string value
197  @param[in] Val the string value
198  */
199  inline void set(const std::string& Val)
200  {
201  m_Value = Val;
202  }
203 
204  inline void clear()
205  { m_Value.clear(); }
206 
207  void writeToStream(std::ostream& OutStm) const;
208 
209  void writeQuotedToStream(std::ostream& OutStm) const
210  {
211  OutStm << "\"" ; writeToStream(OutStm); OutStm << "\"" ;
212  }
213 
214  /**
215  Returns the size of the string
216  @return size of the string
217  */
218  inline unsigned long getSize() const
219  {
220  return m_Value.size();
221  }
222 
223  /**
224  Returns the size of the string
225  @return size of the string
226  */
227  unsigned long size() const
228  {
229  return m_Value.size();
230  }
231 
232 
233  /**
234  Replaces all occurences of FindStr by ReplaceStr
235  @param[in] FindStr the substring to find
236  @param[in] ReplaceStr the substring to replace the found substrings
237  @return the number of occurences
238  */
239  unsigned int replaceAll(const std::string& FindStr,const std::string& ReplaceStr);
240 
241 
242  /**
243  Try to find the the most adapted type for conversion
244  @return the most adapted type for conversion (if the value is empty)
245  */
246  Value::Type guessTypeConversion() const;
247 
248 
249  /**
250  Converts the contained string to a double value (if possible)
251  @return bool true if the conversion is correct, false otherwise
252  */
253  bool toDouble(double& Val) const;
254 
255  /**
256  Converts the contained string to a DoubleValue (if possible)
257  @param[out] Val the converted value
258  @return bool true if the conversion is correct, false otherwise
259  */
260  bool toDoubleValue(DoubleValue& Val) const;
261 
262  /**
263  Converts the contained string to a boolean value (if possible)
264  @param[out] Val the converted value
265  @return bool true if the conversion is correct, false otherwise
266  */
267  bool toBoolean(bool& Val) const;
268 
269  /**
270  Converts the contained string to a BooleanValue (if possible)
271  @param[out] Val the converted value
272  @return bool true if the conversion is correct, false otherwise
273  */
274  bool toBooleanValue(BooleanValue& Val) const;
275 
276  /**
277  Converts the contained string to an int value (if possible)
278  @param[out] Val the converted value
279  @return bool true if the conversion is correct, false otherwise
280  */
281  bool toInteger(int& Val) const;
282 
283  /**
284  Converts the contained string to a long value (if possible)
285  @param[out] Val the converted value
286  @return bool true if the conversion is correct, false otherwise
287  */
288  bool toInteger(long& Val) const;
289 
290  /**
291  Converts the contained string to an IntegerValue (if possible)
292  @param[out] Val the converted value
293  @return bool true if the conversion is correct, false otherwise
294  */
295  bool toIntegerValue(IntegerValue& Val) const;
296 
297  /**
298  Converts the contained string to a NullValue (if possible)
299  @param[out] Val the converted value
300  @return bool true if the conversion is correct, false otherwise
301  */
302  bool toNullValue(NullValue& Val) const;
303 
304  /**
305  Converts the contained string to a VectorValue value (if possible)
306  @param[out] Val the converted value
307  @return bool true if the conversion is correct, false otherwise
308  */
309  bool toVectorValue(VectorValue& Val) const;
310 
311  /**
312  Converts the contained string to a MatrixValue value (if possible)
313  @param[out] Val the converted value
314  @return bool true if the conversion is correct, false otherwise
315  */
316  bool toMatrixValue(MatrixValue& Val) const;
317 
318  /**
319  Converts the contained string to a MatrixValue value (if possible)
320  @param[in] RowLength the size of a row
321  @param[out] Val the converted value
322  @return bool true if the conversion is correct, false otherwise
323  */
324  bool toMatrixValue(const unsigned int& RowLength, MatrixValue& Val) const;
325 
326  /**
327  Converts the contained string to a MapValue value (if possible)
328  @param[out] Val the converted value
329  @return bool true if the conversion is correct, false otherwise
330  */
331  bool toMapValue(MapValue& Val) const;
332 
333  /**
334  Converts the contained string to a TreeValue value (if possible)
335  @param[out] Val the converted value
336  @return bool true if the conversion is correct, false otherwise
337  @throw FrameworkException when called
338  @warning This method is currently not implement and always throws a FrameworkException
339  */
340  bool toTreeValue(TreeValue& Val) const;
341 
342 };
343 
344 
345 } } // namespaces
346 
347 
348 #endif /* __OPENFLUID_CORE_STRINGVALUE_HPP__ */
StringValue()
Definition: StringValue.hpp:97
std::vector< std::string > OPENFLUID_API splitString(const std::string &StrToSplit, const std::string &Separators, bool ReturnsEmpty=false)
Value * clone() const
Definition: StringValue.hpp:161
Definition: IntegerValue.hpp:79
StringValue(const char *Val)
Definition: StringValue.hpp:110
Definition: SimpleValue.hpp:51
Definition: Value.hpp:64
std::string & data()
Definition: StringValue.hpp:181
void writeQuotedToStream(std::ostream &OutStm) const
Definition: StringValue.hpp:209
Definition: StringValue.hpp:76
Definition: VectorValue.hpp:84
Type getType() const
Definition: StringValue.hpp:156
unsigned long size() const
Definition: StringValue.hpp:227
StringValue(const StringValue &Val)
Definition: StringValue.hpp:103
Definition: BooleanValue.hpp:80
Definition: Value.hpp:68
const std::string & data() const
Definition: StringValue.hpp:190
Definition: DoubleValue.hpp:80
Definition: NullValue.hpp:65
Definition: MapValue.hpp:92
Definition: ApplicationException.hpp:47
Type
Definition: Value.hpp:68
Definition: MatrixValue.hpp:84
StringValue(const std::string &Val)
Definition: StringValue.hpp:118
#define OPENFLUID_API
Definition: dllexport.hpp:86
virtual ~StringValue()
Definition: StringValue.hpp:138
void clear()
Definition: StringValue.hpp:204
Definition: TreeValue.hpp:65
unsigned long getSize() const
Definition: StringValue.hpp:218