Leviathan  0.8.0.0
Leviathan game engine
Convert.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2017 Henri Hyyryläinen
3 #pragma once
4 #include "Define.h"
5 // ------------------------------------ //
6 #include "../Common/Types.h"
7 #include <sstream>
8 
9 namespace Leviathan{
10 
11 #define STRINGTOSOMETHINGTEMPLATEALTERNATIVE(StringyType, strstreamt, funcname, totype) \
12 static inline totype funcname(const StringyType &str){ totype tempval; \
13  strstreamt stream; stream.str(str.c_str()); stream >> tempval; return tempval;}
14 
16 class Convert{
17 public:
18 
19  DLLEXPORT static double DegreesToRadians(float degrees);
20  DLLEXPORT static double RadiansToDegrees(float radians);
21 
22  DLLEXPORT static int WstringFromBoolToInt(const std::wstring &i);
23  DLLEXPORT static int StringFromBoolToInt(const std::string &i);
24 
25  DLLEXPORT static int StringTypeNameCheck(const std::string &name);
26 
27  DLLEXPORT static bool IsStringBool(const std::string &val, bool* receiver);
28 
29  template<class T>
30  static inline T WstringTo(const std::wstring &str){
31  T tempval(0);
32  std::wstringstream stream;
33  std::locale globallocale("C");
34  stream.imbue(globallocale);
35 
36  stream.str(str.c_str());
37  stream >> tempval;
38  return tempval;
39  }
40 
41  template<class T>
42  static inline T StringTo(const std::string &str){
43  T tempval(0);
44  std::stringstream stream;
45  std::locale globallocale("C");
46  stream.imbue(globallocale);
47 
48  stream.str(str.c_str());
49  stream >> tempval;
50  return tempval;
51  }
52 
53 
54  // macro conversions //
55  STRINGTOSOMETHINGTEMPLATEALTERNATIVE(std::wstring, std::wstringstream, WstringToInt, int);
56  STRINGTOSOMETHINGTEMPLATEALTERNATIVE(std::wstring, std::wstringstream, WstringToFloat, float);
57 
58  // template functions //
59 
60  template<typename T>
61  static std::wstring ToWstring(const T& val){
62  std::wstringstream stream;
63  std::locale globallocale("C");
64  stream.imbue(globallocale);
65 
66  if(!(stream << val)){
67  return L"";
68  }
69  return stream.str();
70  }
71  template<typename T>
72  static std::string ToString(const T& val){
73  std::stringstream stream;
74  std::locale globallocale("C");
75  stream.imbue(globallocale);
76 
77  if(!(stream << val)){
78  return "";
79  }
80  return stream.str();
81  }
82 
83  template<class T>
84  static std::wstring ToHexadecimalWstring(const T& val){
85  std::wstringstream stream;
86  if(!(stream << std::hex << val)){
87  return L"";
88  }
89  return stream.str();
90  }
91 
92  template<class T>
93  static std::string ToHexadecimalString(const T& val){
94  std::stringstream stream;
95  if(!(stream << std::hex << val)){
96  return "";
97  }
98  return stream.str();
99  }
100 
102  static std::string HexDump(const uint8_t* memory, size_t size, uint32_t groupby = 4) {
103 
104  std::stringstream stream;
105 
106  for (size_t i = 0; i < size; ++i) {
107 
108  if (i > 0 && i % groupby == 0)
109  stream << " ";
110 
111  stream << "0x" << std::hex << static_cast<int>(memory[i]) << " ";
112  }
113 
114  return stream.str();
115  }
116 
117 
120  DLLEXPORT static std::wstring Utf8ToUtf16(const std::string &utf8str);
121 
124  DLLEXPORT static std::string Utf16ToUtf8(const std::wstring &utf16str);
125 
127  DLLEXPORT static std::string CodePointToUtf8(int32_t codepoint);
128 
129 };
130 
131 
132 }
133 
134 #ifdef LEAK_INTO_GLOBAL
135 using Leviathan::Convert;
136 #endif
static T StringTo(const std::string &str)
Definition: Convert.h:42
Holds common conversion functions.
Definition: Convert.h:16
static DLLEXPORT int StringFromBoolToInt(const std::string &i)
Definition: Convert.cpp:29
static std::wstring ToWstring(const T &val)
Definition: Convert.h:61
static DLLEXPORT std::string Utf16ToUtf8(const std::wstring &utf16str)
Encodes an UTF8 string from a wide string (wstring/utf16)
Definition: Convert.cpp:108
static DLLEXPORT std::wstring Utf8ToUtf16(const std::string &utf8str)
Decodes an UTF8 string to an UTF16 string (wide string/wstring)
Definition: Convert.cpp:76
static T WstringTo(const std::wstring &str)
Definition: Convert.h:30
static DLLEXPORT double RadiansToDegrees(float radians)
Definition: Convert.cpp:17
STRINGTOSOMETHINGTEMPLATEALTERNATIVE(std::wstring, std::wstringstream, WstringToInt, int)
unsigned char uint8_t
Definition: core.h:38
static DLLEXPORT int WstringFromBoolToInt(const std::wstring &i)
Definition: Convert.cpp:22
static std::string ToString(const T &val)
Definition: Convert.h:72
static std::string ToHexadecimalString(const T &val)
Definition: Convert.h:93
static DLLEXPORT std::string CodePointToUtf8(int32_t codepoint)
Encodes a Unicode code point as an UTF8 string.
Definition: Convert.cpp:137
static DLLEXPORT int StringTypeNameCheck(const std::string &name)
Definition: Convert.cpp:54
static std::wstring ToHexadecimalWstring(const T &val)
Definition: Convert.h:84
static std::string HexDump(const uint8_t *memory, size_t size, uint32_t groupby=4)
Converts memory at pointer into a hex dump.
Definition: Convert.h:102
#define DLLEXPORT
Definition: Include.h:115
static DLLEXPORT double DegreesToRadians(float degrees)
Definition: Convert.cpp:13
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
unsigned int uint32_t
Definition: core.h:40
static DLLEXPORT bool IsStringBool(const std::string &val, bool *receiver)
Definition: Convert.cpp:37