DataHelpers.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 DataHelpers.hpp
35 
36  @author Jean-Christophe FABRE <jean-christophe.fabre@supagro.inra.fr>
37  */
38 
39 
40 #ifndef __OPENFLUID_TOOLS_DATAHELPERS_HPP__
41 #define __OPENFLUID_TOOLS_DATAHELPERS_HPP__
42 
43 
46 
47 #include <string>
48 #include <list>
49 #include <sstream>
50 #include <vector>
51 #include <set>
52 
53 #include <openfluid/dllexport.hpp>
55 
56 
57 namespace openfluid { namespace tools {
58 
59 
60 /**
61  Converts a string to another type
62  @param[in] StrToConvert the string to convert
63  @param[out] Converted the result of the conversion
64  @return true if the conversion is correct
65 */
66 template<typename T>
67 inline bool convertString(const std::string& StrToConvert, T* Converted)
68 {
69  std::istringstream iss(StrToConvert);
70  char c;
71  return ((iss >> (*Converted)) && !iss.get(c));
72 }
73 
74 
75 // =====================================================================
76 // =====================================================================
77 
78 
79 /**
80  Converts a string to another type
81  @param[in] StrToConvert the string to convert
82  @return true the string converted
83  @throws openfluid::base::FrameworkException Error in string conversion
84 */
85 template<typename T>
86 inline T convertString(const std::string& StrToConvert)
87 {
88  T Converted;
89 
90  if (convertString(StrToConvert,&Converted))
91  return Converted;
92 
93  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Error in string conversion");
94 }
95 
96 
97 // =====================================================================
98 // =====================================================================
99 
100 
101 /**
102  @deprecated Since version 2.1.0. Use openfluid::tools::convertString instead
103 */
104 template<typename T>
105 inline bool ConvertString(const std::string& StrToConvert, T* Converted) OPENFLUID_DEPRECATED;
106 template<typename T>
107 inline bool ConvertString(const std::string& StrToConvert, T* Converted)
108 {
109  return convertString(StrToConvert,Converted);
110 }
111 
112 
113 // =====================================================================
114 // =====================================================================
115 
116 
117 /**
118  Converts a value to a string
119  @param[in] ValueToConvert the value to convert
120  @param[out] StrConverted the result of the conversion
121  @return true if the conversion is correct
122 */
123 template<typename T>
124 inline bool convertValue(const T& ValueToConvert, std::string* StrConverted)
125 {
126  std::ostringstream oss;
127  bool IsOK = !(oss << ValueToConvert).fail();
128 
129  if (IsOK) (*StrConverted) = oss.str();
130 
131  return IsOK;
132 }
133 
134 
135 // =====================================================================
136 // =====================================================================
137 
138 
139 /**
140  Converts a value to a string
141  @param[in] ValueToConvert the value to convert
142  @return the converted value
143  @throws openfluid::base::FrameworkException Error in value conversion
144 */
145 template<typename T>
146 inline std::string convertValue(const T& ValueToConvert)
147 {
148  std::string Converted;
149 
150  if (convertValue(ValueToConvert,&Converted))
151  return Converted;
152 
153  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Error in value conversion");
154 }
155 
156 
157 // =====================================================================
158 // =====================================================================
159 
160 
161 /**
162  @deprecated Since version 2.1.0. Use openfluid::tools::convertValue instead
163 */
164 template<typename T>
165 inline bool ConvertValue(const T& ValueToConvert, std::string* StrConverted) OPENFLUID_DEPRECATED;
166 template<typename T>
167 inline bool ConvertValue(const T& ValueToConvert, std::string* StrConverted)
168 {
169  return convertValue(ValueToConvert,StrConverted);
170 }
171 
172 
173 // =====================================================================
174 // =====================================================================
175 
176 
177 /**
178  Function for tokenizing string into a vector of tokens
179  @param[in] StrToTokenize the string to tokenize
180  @param[out] Tokens the resulting tokens
181  @param[in] Delimiters the string delimiter
182 */
183 void OPENFLUID_API tokenizeString(const std::string& StrToTokenize,
184  std::vector<std::string>& Tokens,
185  const std::string& Delimiters);
186 
187 
188 /**
189  @deprecated Since version 2.1.0. Use openfluid::tools::tokenizeString instead
190 */
191 inline void TokenizeString(const std::string& StrToTokenize,
192  std::vector<std::string>& Tokens,
193  const std::string& Delimiters) OPENFLUID_DEPRECATED;
194 inline void TokenizeString(const std::string& StrToTokenize,
195  std::vector<std::string>& Tokens,
196  const std::string& Delimiters)
197 {
198  tokenizeString(StrToTokenize,Tokens,Delimiters);
199 }
200 
201 
202 /**
203  Splits the passed string into a std::string array, split using the given SepString
204  @param[in] StrToSplit the string to split
205  @param[in] Separators the string of separators used to split the string
206  @param[in] ReturnsEmpty if true, the empty strings are returned
207  @return a vector of strings
208 */
209 std::vector<std::string> OPENFLUID_API splitString(const std::string& StrToSplit,
210  const std::string& Separators,
211  bool ReturnsEmpty = false);
212 
213 /**
214  @deprecated Since version 2.1.0. Use openfluid::tools::splitString instead
215 */
216 inline std::vector<std::string> SplitString(const std::string& StrToSplit,
217  const std::string& Separators,
218  bool ReturnsEmpty = false) OPENFLUID_DEPRECATED;
219 inline std::vector<std::string> SplitString(const std::string& StrToSplit,
220  const std::string& Separators,
221  bool ReturnsEmpty)
222 {
223  return splitString(StrToSplit,Separators,ReturnsEmpty);
224 }
225 
226 
227 } } // namespaces
228 
229 
230 #endif /* __OPENFLUID_TOOLS_DATAHELPERS_HPP__ */
bool convertString(const std::string &StrToConvert, T *Converted)
Definition: DataHelpers.hpp:67
std::vector< std::string > SplitString(const std::string &StrToSplit, const std::string &Separators, bool ReturnsEmpty=false) OPENFLUID_DEPRECATED
Definition: DataHelpers.hpp:219
bool ConvertValue(const T &ValueToConvert, std::string *StrConverted) OPENFLUID_DEPRECATED
Definition: DataHelpers.hpp:167
bool ConvertString(const std::string &StrToConvert, T *Converted) OPENFLUID_DEPRECATED
Definition: DataHelpers.hpp:107
#define OPENFLUID_DEPRECATED
Definition: deprecation.hpp:54
bool convertValue(const T &ValueToConvert, std::string *StrConverted)
Definition: DataHelpers.hpp:124
std::vector< std::string > OPENFLUID_API splitString(const std::string &StrToSplit, const std::string &Separators, bool ReturnsEmpty=false)
Definition: FrameworkException.hpp:50
Definition: ApplicationException.hpp:47
void TokenizeString(const std::string &StrToTokenize, std::vector< std::string > &Tokens, const std::string &Delimiters) OPENFLUID_DEPRECATED
Definition: DataHelpers.hpp:194
#define OPENFLUID_API
Definition: dllexport.hpp:87
void OPENFLUID_API tokenizeString(const std::string &StrToTokenize, std::vector< std::string > &Tokens, const std::string &Delimiters)