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