LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
EncodingTraits.hpp
Go to the documentation of this file.
1 
7 #ifndef LLU_WSTP_ENCODINGTRAITS_HPP_
8 #define LLU_WSTP_ENCODINGTRAITS_HPP_
9 
10 #include <cstdint>
11 #include <string>
12 #include <type_traits>
13 #include <utility>
14 
15 namespace LLU::WS {
16 
21  enum class Encoding : std::uint8_t {
22  Undefined,
23  Native,
24  Byte,
25  UTF8,
26  UTF16,
27  UCS2,
28  UTF32
29  };
30 
37  template<Encoding E>
38  struct CharTypeStruct {
39  static_assert(E != Encoding::Undefined, "Trying to deduce character type for undefined encoding");
41  using type = char;
42  };
43 
48  template<>
49  struct CharTypeStruct<Encoding::Native> {
50  using type = char;
51  };
52  template<>
53  struct CharTypeStruct<Encoding::Byte> {
54  using type = unsigned char;
55  };
56  template<>
57  struct CharTypeStruct<Encoding::UTF8> {
58  using type = unsigned char;
59  };
60  template<>
61  struct CharTypeStruct<Encoding::UTF16> {
62  using type = unsigned short;
63  };
64  template<>
65  struct CharTypeStruct<Encoding::UCS2> {
66  using type = unsigned short;
67  };
68  template<>
69  struct CharTypeStruct<Encoding::UTF32> {
70  using type = unsigned int;
71  };
73 
80  template<Encoding E>
82 
91  template<Encoding E, typename T>
92  inline constexpr bool CharacterTypesCompatible = (sizeof(T) == sizeof(CharType<E>));
93 
100  template<Encoding E>
103  using type = std::basic_string<CharType<E>>;
104  };
105 
112  template<Encoding E>
114 
120  constexpr const char* getEncodingName(Encoding e) {
121  switch (e) {
122  case Encoding::Undefined: return "Undefined";
123  case Encoding::Native: return "Native";
124  case Encoding::Byte: return "Byte";
125  case Encoding::UTF8: return "UTF8";
126  case Encoding::UTF16: return "UTF16";
127  case Encoding::UCS2: return "UCS2";
128  case Encoding::UTF32: return "UTF32";
129  }
130  }
131 
149  template<Encoding E, typename T, bool = std::is_lvalue_reference<T>::value>
150  struct PutAs;
151 
156  template<Encoding E, typename T>
157  struct PutAs<E, T, true> {
158 
163  explicit PutAs(const T& o) : obj(o) {}
164 
166  const T& obj;
167  };
168 
172  template<Encoding E, typename T>
173  struct PutAs<E, T, false> {
174 
179  explicit PutAs(T&& o) : obj(std::move(o)) {}
180 
182  T obj;
183  };
185 
191  template<Encoding E, typename T>
192  PutAs<E, T> putAs(T&& obj) {
193  return PutAs<E, T>(std::forward<T>(obj));
194  }
195 
213  template<Encoding E, typename T>
214  struct GetAs {
215 
220  explicit GetAs(T& o) : obj(o) {}
221 
223  T& obj;
224  };
225 
231  template<Encoding E, typename T>
232  GetAs<E, T> getAs(T& obj) {
233  return GetAs<E, T>(obj);
234  }
235 } /* namespace LLU::WS */
236 
237 #endif /* LLU_WSTP_ENCODINGTRAITS_HPP_ */
LLU::WS::Encoding::Native
@ Native
Use WSGetString for reading and WSPutString for writing strings.
LLU::WS::Encoding::Undefined
@ Undefined
Undefined, can be used to denote that certain function is not supposed to deal with strings.
LLU::WS::CharType
typename CharTypeStruct< E >::type CharType
Specializations of CharTypeStruct, encoding E has assigned type T iff WSPutEString takes const T* as ...
Definition: EncodingTraits.hpp:81
LLU::WS::Encoding::Byte
@ Byte
Use WSGetByteString for reading and WSPutByteString for writing strings.
LLU::WS::getAs
GetAs< E, T > getAs(T &obj)
This is a helper function to facilitate constructing WS::GetAs wrapper.
Definition: EncodingTraits.hpp:232
LLU::WS::GetAs
Utility structure used to enforce receiving given value via WSStream with encoding E.
Definition: EncodingTraits.hpp:214
LLU::WS::Encoding
Encoding
Definition: EncodingTraits.hpp:21
LLU::WS::Encoding::UTF8
@ UTF8
Use WSGetUTF8String for reading and WSPutUTF8String for writing strings.
LLU::WS::CharTypeStruct::type
char type
static_assert will trigger compilation error, we add a dummy type to prevent further compiler errors
Definition: EncodingTraits.hpp:41
LLU::WS::GetAs::obj
T & obj
A reference to which later a resulting value from WSTP function will be assigned.
Definition: EncodingTraits.hpp:223
LLU::WS::StringTypeStruct::type
std::basic_string< CharType< E > > type
Alias for the basic_string specialization which naturally corresponds to encoding E.
Definition: EncodingTraits.hpp:103
LLU::WS::StringType
typename StringTypeStruct< E >::type StringType
Definition: EncodingTraits.hpp:113
LLU::WS::Encoding::UTF32
@ UTF32
Use WSGetUTF32String for reading and WSPutUTF32String for writing strings.
LLU::WS::Encoding::UTF16
@ UTF16
Use WSGetUTF16String for reading and WSPutUTF16String for writing strings.
LLU::WS::CharTypeStruct
Definition: EncodingTraits.hpp:38
LLU::WS::CharacterTypesCompatible
constexpr bool CharacterTypesCompatible
Check whether character type T is compatible with encoding E (i.e.
Definition: EncodingTraits.hpp:92
LLU::WS::GetAs::GetAs
GetAs(T &o)
Constructor that takes a reference to value of type T and stores it.
Definition: EncodingTraits.hpp:220
LLU::WS::getEncodingName
constexpr const char * getEncodingName(Encoding e)
Get the name of encoding.
Definition: EncodingTraits.hpp:120
LLU::WS
Contains definitions related to WSTP functionality in LLU.
Definition: EncodingTraits.hpp:15
LLU::WS::StringTypeStruct
Definition: EncodingTraits.hpp:101
LLU::WS::PutAs
Utility structure used to enforce sending given value with encoding E via WSStream.
Definition: EncodingTraits.hpp:150
LLU::WS::putAs
PutAs< E, T > putAs(T &&obj)
This is a helper function to facilitate constructing WS::PutAs wrapper.
Definition: EncodingTraits.hpp:192
LLU::WS::Encoding::UCS2
@ UCS2
Use WSGetUCS2String for reading and WSPutUCS2String for writing strings.