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