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