Ocular Engine
StringUtils.hpp
1 
17 #pragma once
18 #ifndef __H__OCULAR_CORE_UTILS_STRING_OPERATIONS__H__
19 #define __H__OCULAR_CORE_UTILS_STRING_OPERATIONS__H__
20 
21 #include "VoidCast.hpp"
22 
23 #include <string>
24 #include <cstdint>
25 #include <sstream>
26 #include <iomanip>
27 #include <functional>
28 #include <unordered_map>
29 
30 //------------------------------------------------------------------------------------------
31 
36 namespace Ocular
37 {
42  namespace Utils
43  {
48  class String
49  {
50  public:
51 
52  String();
53  ~String();
54 
55  //------------------------------------------------------------------------------
56  // General String Operations
57  //------------------------------------------------------------------------------
58 
65  static std::string ToLower(std::string const& str);
66 
73  static std::string ToUpper(std::string const& str);
74 
82  static bool IsEqual(std::string const& strA, std::string const& strB, bool ignoreCase = false);
83 
91  static bool Contains(std::string const& source, std::string const& find, bool ignoreCase = false);
92 
99  static std::string FormatWindowsError(unsigned long error);
100 
112  static std::string FormatBytes(uint64_t bytes);
113 
122  static void Split(std::string const& str, char delim, std::vector<std::string>& tokens);
123 
128  template<typename T>
129  static typename std::enable_if<std::is_arithmetic<T>::value, std::string>::type FormatHex(T const x)
130  {
131  std::stringstream sstream;
132  sstream << "0x" << std::setfill('0') << std::setw(sizeof(T) * static_cast<T>(2)) << std::hex << x;
133 
134  return sstream.str();
135  }
136 
137  //------------------------------------------------------------------------------
138  // To/From Methods and Helpers
139  //------------------------------------------------------------------------------
140 
141  // The following methods can not be static like the rest. If we make the maps static, then
142  // their is a very strong likelihood that some other static initializer will call these methods,
143  // particularly the registers, before the String static initializers can act. This leads to
144  // a crash when performing a search in the uninitialized map and why this class was added
145  // to the main engine class as OcularString.
146 
160  template<typename T>
161  typename std::enable_if<!std::is_pointer<T>::value, std::string>::type toString(T const& data, bool isPointer = false) const
162  {
163  std::string result;
164 
165  const std::string tStr = TypeName<T>::name;
166  auto find = m_ToFunctions.find(tStr);
167 
168  if(find != m_ToFunctions.end())
169  {
170  result = find->second(void_cast<T>(data), isPointer);
171  }
172 
173  return result;
174  }
175 
189  template<typename T>
190  typename std::enable_if<!std::is_pointer<T>::value, bool>::type toString(T const& data, std::string& str, bool isPointer = false) const
191  {
192  bool result = false;
193 
194  const std::string tStr = TypeName<T>::name;
195  auto find = m_ToFunctions.find(tStr);
196 
197  if(find != m_ToFunctions.end())
198  {
199  str = find->second(void_cast<T>(data), isPointer);
200  result = true;
201  }
202 
203  return result;
204  }
205 
219  std::string toString(std::string const& type, void* data, bool isPointer = false) const
220  {
221  std::string result;
222 
223  if(data)
224  {
225  auto find = m_ToFunctions.find(type);
226 
227  if(find != m_ToFunctions.end())
228  {
229  result = find->second(data, isPointer);
230  }
231  }
232 
233  return result;
234  }
235 
250  bool toString(std::string const& type, void* data, std::string& str, bool isPointer = false) const
251  {
252  bool result = false;
253 
254  if(data)
255  {
256  auto find = m_ToFunctions.find(type);
257 
258  if(find != m_ToFunctions.end())
259  {
260  str = find->second(data, isPointer);
261  result = true;
262  }
263  }
264 
265  return result;
266  }
267 
280  template<typename T>
281  typename std::enable_if<!std::is_pointer<T>::value, T>::type fromString(std::string const& value) const
282  {
283  T result;
284 
285  const std::string tStr = TypeName<T>::name;
286  auto find = m_FromFunctions.find(tStr);
287 
288  if(find != m_FromFunctions.end())
289  {
290  find->second(value, void_cast<T*>(&result));
291  }
292 
293  return result;
294  }
295 
308  template<typename T>
309  typename std::enable_if<!std::is_pointer<T>::value, bool>::type fromString(std::string const& value, T& object) const
310  {
311  bool result = false;
312  auto find = m_FromFunctions.find(value);
313 
314  if(find != m_FromFunctions.end())
315  {
316  find->second(value, object);
317  result = true;
318  }
319 
320  return result;
321  }
322 
340  bool fromString(std::string const& type, std::string const& value, void* object)
341  {
342  bool result = false;
343 
344  if(object)
345  {
346  auto find = m_FromFunctions.find(type);
347 
348  if(find != m_FromFunctions.end())
349  {
350  find->second(value, object);
351  result = true;
352  }
353  }
354 
355  return result;
356  }
357 
363  template<typename T>
364  typename std::enable_if<!std::is_pointer<T>::value, void>::type registerToString(std::function<std::string(void*, bool)> func)
365  {
366  const std::string tStr = TypeName<T>::name;
367  auto find = m_ToFunctions.find(tStr);
368 
369  if(find == m_ToFunctions.end())
370  {
371  m_ToFunctions.insert({tStr, func});
372  }
373  }
374 
380  template<typename T>
381  typename std::enable_if<!std::is_pointer<T>::value, void>::type registerFromString(std::function<void(std::string const&, void*)> func)
382  {
383  const std::string tStr = TypeName<T>::name;
384  auto find = m_FromFunctions.find(tStr);
385 
386  if(find == m_FromFunctions.end())
387  {
388  m_FromFunctions.insert({tStr, func});
389  }
390  }
391 
392  protected:
393 
394  private:
395 
396  std::unordered_map<std::string, std::function<std::string(void*, bool)>> m_ToFunctions;
397  std::unordered_map<std::string, std::function<void(std::string const&, void*)>> m_FromFunctions;
398  };
399  }
403 }
408 //------------------------------------------------------------------------------------------
409 
410 #endif
static void Split(std::string const &str, char delim, std::vector< std::string > &tokens)
Definition: StringUtils.cpp:183
static std::string FormatWindowsError(unsigned long error)
Definition: StringUtils.cpp:113
std::string toString(std::string const &type, void *data, bool isPointer=false) const
Definition: StringUtils.hpp:219
std::enable_if<!std::is_pointer< T >::value, T >::type fromString(std::string const &value) const
Definition: StringUtils.hpp:281
std::enable_if<!std::is_pointer< T >::value, void >::type registerFromString(std::function< void(std::string const &, void *)> func)
Definition: StringUtils.hpp:381
static std::enable_if< std::is_arithmetic< T >::value, std::string >::type FormatHex(T const x)
Definition: StringUtils.hpp:129
static std::string ToLower(std::string const &str)
Definition: StringUtils.cpp:57
bool fromString(std::string const &type, std::string const &value, void *object)
Definition: StringUtils.hpp:340
std::enable_if<!std::is_pointer< T >::value, bool >::type fromString(std::string const &value, T &object) const
Definition: StringUtils.hpp:309
static std::string ToUpper(std::string const &str)
Definition: StringUtils.cpp:66
Note: Once this library is made dynamic, this will no longer be needed.
Definition: Common.hpp:70
static bool Contains(std::string const &source, std::string const &find, bool ignoreCase=false)
Definition: StringUtils.cpp:94
Utility structure to convert a type T to a string representation.
Definition: Types.hpp:66
bool toString(std::string const &type, void *data, std::string &str, bool isPointer=false) const
Definition: StringUtils.hpp:250
std::enable_if<!std::is_pointer< T >::value, void >::type registerToString(std::function< std::string(void *, bool)> func)
Definition: StringUtils.hpp:364
std::enable_if<!std::is_pointer< T >::value, bool >::type toString(T const &data, std::string &str, bool isPointer=false) const
Definition: StringUtils.hpp:190
static bool IsEqual(std::string const &strA, std::string const &strB, bool ignoreCase=false)
Definition: StringUtils.cpp:75
static std::string FormatBytes(uint64_t bytes)
Definition: StringUtils.cpp:149
Collection of helper string-related utilities.
Definition: StringUtils.hpp:48
std::enable_if<!std::is_pointer< T >::value, std::string >::type toString(T const &data, bool isPointer=false) const
Definition: StringUtils.hpp:161