Leviathan  0.8.0.0
Leviathan game engine
Leviathan::Convert Class Reference

Holds common conversion functions. More...

#include <Convert.h>

Public Member Functions

 STRINGTOSOMETHINGTEMPLATEALTERNATIVE (std::wstring, std::wstringstream, WstringToInt, int)
 
 STRINGTOSOMETHINGTEMPLATEALTERNATIVE (std::wstring, std::wstringstream, WstringToFloat, float)
 

Static Public Member Functions

static DLLEXPORT double DegreesToRadians (float degrees)
 
static DLLEXPORT double RadiansToDegrees (float radians)
 
static DLLEXPORT int WstringFromBoolToInt (const std::wstring &i)
 
static DLLEXPORT int StringFromBoolToInt (const std::string &i)
 
static DLLEXPORT int StringTypeNameCheck (const std::string &name)
 
static DLLEXPORT bool IsStringBool (const std::string &val, bool *receiver)
 
template<class T >
static T WstringTo (const std::wstring &str)
 
template<class T >
static T StringTo (const std::string &str)
 
template<typename T >
static std::wstring ToWstring (const T &val)
 
template<typename T >
static std::string ToString (const T &val)
 
template<class T >
static std::wstring ToHexadecimalWstring (const T &val)
 
template<class T >
static std::string ToHexadecimalString (const T &val)
 
static std::string HexDump (const uint8_t *memory, size_t size, uint32_t groupby=4)
 Converts memory at pointer into a hex dump. More...
 
static DLLEXPORT std::wstring Utf8ToUtf16 (const std::string &utf8str)
 Decodes an UTF8 string to an UTF16 string (wide string/wstring) More...
 
static DLLEXPORT std::string Utf16ToUtf8 (const std::wstring &utf16str)
 Encodes an UTF8 string from a wide string (wstring/utf16) More...
 
static DLLEXPORT std::string CodePointToUtf8 (int32_t codepoint)
 Encodes a Unicode code point as an UTF8 string. More...
 

Detailed Description

Holds common conversion functions.

Definition at line 16 of file Convert.h.

Member Function Documentation

◆ CodePointToUtf8()

DLLEXPORT std::string Leviathan::Convert::CodePointToUtf8 ( int32_t  codepoint)
static

Encodes a Unicode code point as an UTF8 string.

Definition at line 137 of file Convert.cpp.

137  {
138 
139  std::string result;
140  utf8::utf32to8(&codepoint, (&codepoint) + 1, std::back_inserter(result));
141 
142  return result;
143 }
octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result)
Definition: checked.h:249

◆ DegreesToRadians()

double Convert::DegreesToRadians ( float  degrees)
static

Definition at line 13 of file Convert.cpp.

13  {
14  return (degrees*(PI/180.f));
15 
16 }
constexpr float PI
Definition: Define.h:59

◆ HexDump()

static std::string Leviathan::Convert::HexDump ( const uint8_t *  memory,
size_t  size,
uint32_t  groupby = 4 
)
inlinestatic

Converts memory at pointer into a hex dump.

Definition at line 102 of file Convert.h.

102  {
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  }

◆ IsStringBool()

DLLEXPORT bool Convert::IsStringBool ( const std::string &  val,
bool *  receiver 
)
static

Definition at line 37 of file Convert.cpp.

37  {
38 
39  if(StringOperations::CompareInsensitive<string>(val, "true") || val == "1"){
40 
41  *receiver = true;
42  return true;
43  }
44 
45  if(StringOperations::CompareInsensitive<string>(val, "false") || val == "0"){
46 
47  *receiver = false;
48  return true;
49  }
50 
51  return false;
52 }

◆ RadiansToDegrees()

double Convert::RadiansToDegrees ( float  radians)
static

Definition at line 17 of file Convert.cpp.

17  {
18  return radians*(180.f/PI);
19 }
constexpr float PI
Definition: Define.h:59

◆ StringFromBoolToInt()

int Convert::StringFromBoolToInt ( const std::string &  i)
static

Definition at line 29 of file Convert.cpp.

29  {
30  if(StringOperations::CompareInsensitive<string>(i, "true") || i == "1"){
31  return true;
32  } else {
33  return false;
34  }
35 }

◆ StringTo()

template<class T >
static T Leviathan::Convert::StringTo ( const std::string &  str)
inlinestatic

Definition at line 42 of file Convert.h.

42  {
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  }

◆ STRINGTOSOMETHINGTEMPLATEALTERNATIVE() [1/2]

Leviathan::Convert::STRINGTOSOMETHINGTEMPLATEALTERNATIVE ( std::wstring  ,
std::wstringstream  ,
WstringToInt  ,
int   
)

◆ STRINGTOSOMETHINGTEMPLATEALTERNATIVE() [2/2]

Leviathan::Convert::STRINGTOSOMETHINGTEMPLATEALTERNATIVE ( std::wstring  ,
std::wstringstream  ,
WstringToFloat  ,
float   
)

◆ StringTypeNameCheck()

DLLEXPORT int Convert::StringTypeNameCheck ( const std::string &  name)
static

Definition at line 54 of file Convert.cpp.

54  {
55 
56  if(name == "int")
57  return DATABLOCK_TYPE_INT;
58  if(name == "float")
59  return DATABLOCK_TYPE_FLOAT;
60  if(name == "bool")
61  return DATABLOCK_TYPE_BOOL;
62  if(name == "wstring")
64  if(name == "string")
65  return DATABLOCK_TYPE_STRING;
66  if(name == "char")
67  return DATABLOCK_TYPE_CHAR;
68  if(name == "double")
69  return DATABLOCK_TYPE_DOUBLE;
70  if(name == "void" || name == "void*")
72 
73  return -1;
74 }
#define DATABLOCK_TYPE_INT
Definition: DataBlock.h:22
#define DATABLOCK_TYPE_VOIDPTR
Definition: DataBlock.h:29
#define DATABLOCK_TYPE_WSTRING
Definition: DataBlock.h:25
#define DATABLOCK_TYPE_FLOAT
Definition: DataBlock.h:23
#define DATABLOCK_TYPE_STRING
Definition: DataBlock.h:26
#define DATABLOCK_TYPE_DOUBLE
Definition: DataBlock.h:28
#define DATABLOCK_TYPE_BOOL
Definition: DataBlock.h:24
#define DATABLOCK_TYPE_CHAR
Definition: DataBlock.h:27

◆ ToHexadecimalString()

template<class T >
static std::string Leviathan::Convert::ToHexadecimalString ( const T &  val)
inlinestatic

Definition at line 93 of file Convert.h.

93  {
94  std::stringstream stream;
95  if(!(stream << std::hex << val)){
96  return "";
97  }
98  return stream.str();
99  }

◆ ToHexadecimalWstring()

template<class T >
static std::wstring Leviathan::Convert::ToHexadecimalWstring ( const T &  val)
inlinestatic

Definition at line 84 of file Convert.h.

84  {
85  std::wstringstream stream;
86  if(!(stream << std::hex << val)){
87  return L"";
88  }
89  return stream.str();
90  }

◆ ToString()

template<typename T >
static std::string Leviathan::Convert::ToString ( const T &  val)
inlinestatic

Definition at line 72 of file Convert.h.

72  {
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  }

◆ ToWstring()

template<typename T >
static std::wstring Leviathan::Convert::ToWstring ( const T &  val)
inlinestatic

Definition at line 61 of file Convert.h.

61  {
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  }

◆ Utf16ToUtf8()

DLLEXPORT std::string Leviathan::Convert::Utf16ToUtf8 ( const std::wstring &  utf16str)
static

Encodes an UTF8 string from a wide string (wstring/utf16)

Returns
The converted string or an empty string in case the input string is invalid/has invalid format

Definition at line 108 of file Convert.cpp.

108  {
109  // Store enough memory for the result //
110  string results;
111  results.reserve(utf16str.size());
112 
113  try {
114 
115  utf8::utf16to8(utf16str.begin(), utf16str.end(), back_inserter(results));
116 
117  } catch(utf8::invalid_code_point &e1){
118  #ifndef LEVIATHAN_UE_PLUGIN
119  Logger::Get()->Error("Convert: invalid UTF-8 code point: "+
121  #else
122  NOT_UNUSED(e1);
123  #endif //LEVIATHAN_UE_PLUGIN
124  return string();
125 
126  } catch(utf8::not_enough_room&){
127  #ifndef LEVIATHAN_UE_PLUGIN
128  Logger::Get()->Error("Convert: not enough memory for string conversion");
129  #endif //LEVIATHAN_UE_PLUGIN
130  return string();
131  }
132 
133  // The result is now done //
134  return results;
135 }
#define NOT_UNUSED(x)
Definition: Include.h:104
uint32_t code_point() const
Definition: checked.h:46
octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result)
Definition: checked.h:207
static std::string ToString(const T &val)
Definition: Convert.h:72
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
DLLEXPORT void Error(const std::string &data) override
Definition: Logger.cpp:177

◆ Utf8ToUtf16()

DLLEXPORT std::wstring Leviathan::Convert::Utf8ToUtf16 ( const std::string &  utf8str)
static

Decodes an UTF8 string to an UTF16 string (wide string/wstring)

Returns
The converted string or an empty string in case the input string is invalid/has invalid format

Definition at line 76 of file Convert.cpp.

76  {
77 
78  // Store enough memory for the result //
79  wstring results;
80  results.reserve(utf8str.size());
81 
82  try {
83 
84  utf8::utf8to16(utf8str.begin(), utf8str.end(), back_inserter(results));
85 
86  } catch(utf8::invalid_code_point &e1){
87 
88  #ifndef LEVIATHAN_UE_PLUGIN
89  Logger::Get()->Error("Convert: invalid UTF-8 code point: "+
91  #else
92  NOT_UNUSED(e1);
93  #endif //LEVIATHAN_UE_PLUGIN
94  return wstring();
95 
96  } catch(utf8::not_enough_room&){
97 
98  #ifndef LEVIATHAN_UE_PLUGIN
99  Logger::Get()->Error("Convert: not enough memory for string conversion");
100  #endif //LEVIATHAN_UE_PLUGIN
101  return wstring();
102  }
103 
104  // The result is now done //
105  return results;
106 }
#define NOT_UNUSED(x)
Definition: Include.h:104
uint32_t code_point() const
Definition: checked.h:46
static std::string ToString(const T &val)
Definition: Convert.h:72
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result)
Definition: checked.h:234
DLLEXPORT void Error(const std::string &data) override
Definition: Logger.cpp:177

◆ WstringFromBoolToInt()

int Convert::WstringFromBoolToInt ( const std::wstring &  i)
static

Definition at line 22 of file Convert.cpp.

22  {
23  if(StringOperations::CompareInsensitive<wstring>(i, L"true") || i == L"1"){
24  return true;
25  } else {
26  return false;
27  }
28 }

◆ WstringTo()

template<class T >
static T Leviathan::Convert::WstringTo ( const std::wstring &  str)
inlinestatic

Definition at line 30 of file Convert.h.

30  {
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  }

The documentation for this class was generated from the following files: