13#include <Common/assertion.h>
14#include <Common/Utility/string_utils.h>
23template<
typename FloatType>
28 return string_utils::parse_float<FloatType>(sdlFloatStr);
30 catch(
const std::exception& e)
32 throw SdlLoadError(
"on loading floating-point value -> " + std::string(e.what()));
36template<
typename IntType>
37inline IntType
load_int(std::string_view sdlIntStr)
41 return string_utils::parse_int<IntType>(sdlIntStr);
43 catch(
const std::exception& e)
45 throw SdlLoadError(
"on loading integer value -> " + std::string(e.what()));
49template<
typename NumberType>
52 if constexpr(std::is_floating_point_v<NumberType>)
58 static_assert(std::is_integral_v<NumberType>);
64template<
typename NumberType, std::
size_t EXTENT>
67 static const Tokenizer tokenizer({
' ',
'\t',
'\n',
'\r'}, {});
69 const auto N = out_numbers.size();
75 std::vector<std::string> tokens;
76 tokenizer.tokenize(std::string(sdlNumbersStr), tokens);
79 if(tokens.size() == N)
81 for(std::size_t ni = 0; ni < N; ++ni)
87 else if(tokens.size() == 1)
93 throw_formatted<SdlLoadError>(
94 "invalid number of tokens: {}", tokens.size());
99 throw_formatted<SdlLoadError>(
100 "on parsing {}-tuple -> {}", out_numbers.size(), e.whatStr());
114template<
typename Element>
125 throw SdlLoadError(
"on parsing Vector2 -> " + e.whatStr());
129template<
typename Element>
140 throw SdlLoadError(
"on parsing Vector3 -> " + e.whatStr());
144template<
typename Element>
155 throw SdlLoadError(
"on parsing Vector4 -> " + e.whatStr());
159template<
typename Element>
170 throw SdlLoadError(
"on parsing Quaternion -> " + e.whatStr());
174template<
typename NumberType>
177 static const Tokenizer tokenizer({
' ',
'\t',
'\n',
'\r'}, {});
182 std::vector<std::string> arrayTokens;
183 tokenizer.
tokenize(std::string(sdlNumberArrayStr), arrayTokens);
185 std::vector<NumberType> numberArray(arrayTokens.size());
186 for(std::size_t i = 0; i < numberArray.size(); ++i)
195 throw SdlLoadError(
"on parsing number array -> " + e.whatStr());
199template<
typename Element>
202 static const Tokenizer tokenizer({
' ',
'\t',
'\n',
'\r'}, {{
'"',
'"'}});
207 std::vector<std::string> vec3Tokens;
208 tokenizer.
tokenize(std::string(sdlVec3ArrayStr), vec3Tokens);
210 std::vector<math::TVector3<Element>> vec3Array(vec3Tokens.size());
211 for(std::size_t i = 0; i < vec3Array.size(); ++i)
220 throw SdlLoadError(
"on parsing Vector3 array -> " + e.whatStr());
224inline void save_real(
const real value, std::string& out_str)
234template<
typename FloatType>
235inline void save_float(
const FloatType value, std::string& out_str)
239 string_utils::stringify_number(value, out_str, 32);
241 catch(
const std::exception& e)
243 throw SdlSaveError(
"on saving floating-point value -> " + std::string(e.what()));
247template<
typename IntType>
248inline void save_int(
const IntType value, std::string& out_str)
252 string_utils::stringify_number(value, out_str, 32);
254 catch(
const std::exception& e)
256 throw SdlSaveError(
"on saving integer value -> " + std::string(e.what()));
260template<
typename NumberType>
261inline void save_number(
const NumberType value, std::string& out_str)
263 if constexpr(std::is_floating_point_v<NumberType>)
269 static_assert(std::is_integral_v<NumberType>);
275template<
typename NumberType, std::
size_t EXTENT>
278 std::string& out_str)
280 const auto N = numbers.size();
286 if(std::equal(numbers.begin() + 1, numbers.end(), numbers.begin()))
293 for(std::size_t ni = 1; ni < N; ++ni)
302 throw_formatted<SdlSaveError>(
303 "on saving {}-tuple -> {}", numbers.size(), e.whatStr());
307template<
typename Element>
318 throw SdlSaveError(
"on saving Vector2 -> " + e.whatStr());
322template<
typename Element>
333 throw SdlSaveError(
"on saving Vector3 -> " + e.whatStr());
337template<
typename Element>
348 throw SdlSaveError(
"on saving Vector4 -> " + e.whatStr());
352template<
typename Element>
363 throw SdlSaveError(
"on saving Quaternion -> " + e.whatStr());
367template<
typename NumberType>
378 throw SdlSaveError(
"on saving number array -> " + e.whatStr());
382template<
typename Element>
394 for(std::size_t vi = 1; vi < values.size(); ++vi)
404 throw SdlSaveError(
"on saving Vector3 array -> " + e.whatStr());
421template<std::
integral IntType>
463template<std::
floating_po
int FloatType>
477template<CNumber NumberType>
480 if constexpr(std::is_floating_point_v<NumberType>)
486 static_assert(std::is_integral_v<NumberType>);
548template<
typename DstType,
typename SrcType>
549inline DstType*
cast_to(SrcType*
const srcResource)
553 static_assert(std::is_base_of_v<ISdlResource, SrcType>,
554 "Source resource must derive from ISdlResource.");
556 static_assert(std::is_base_of_v<ISdlResource, DstType>,
557 "Casted-to type must derive from ISdlResource.");
559 if(srcResource ==
nullptr)
564 DstType*
const dstResource =
dynamic_cast<DstType*
>(srcResource);
565 if(dstResource ==
nullptr)
567 throw_formatted<SdlException>(
568 "type cast error: source resource cannot be casted to the specified type (resource ID: {})",
569 srcResource->getId());
General exception thrown on error related to SDL.
Definition sdl_exceptions.h:13
Error on the SDL input process.
Definition sdl_exceptions.h:22
Error on the SDL output process.
Definition sdl_exceptions.h:30
Definition Tokenizer.h:13
void tokenize(const std::string &source, std::vector< std::string > &out_results) const
Definition Tokenizer.cpp:25
TSpanView< T, N > toView() const
Definition TArithmeticArrayBase.ipp:869
TSpan< T, N > toSpan()
Definition TArithmeticArrayBase.ipp:862
Represents a quaternion.
Definition TQuaternion.h:17
Represents a 2-D vector.
Definition TVector2.h:19
Represents a 3-D vector.
Definition TVector3.h:17
Represents a 4-D vector.
Definition TVector4.h:15
TSpanView< T, N > toView() const
Definition TArithmeticArrayBase.ipp:869
TSpan< T, N > toSpan()
Definition TArithmeticArrayBase.ipp:862
Check if SDL category information can be obtained statically. The result is true if the static member...
Definition sdl_traits.h:20
Definition sdl_traits.h:13
Definition sdl_spectrum_io.cpp:17
void save_vector3(const math::TVector3< Element > &value, std::string &out_str)
Definition sdl_helpers.ipp:323
integer load_integer(std::string_view sdlIntegerStr)
Definition sdl_helpers.ipp:109
IntType load_int(std::string_view sdlIntStr)
Returns a integer number by processing its SDL representation. Supports ph::integer and all signed an...
Definition sdl_helpers.ipp:37
void save_number_array(TSpanView< NumberType > values, std::string &out_str)
Definition sdl_helpers.ipp:368
void save_int(IntType value, std::string &out_str)
Converts a integer number to its SDL representation. Supports ph::real, float, double,...
Definition sdl_helpers.ipp:248
void load_numbers(std::string_view sdlNumbersStr, TSpan< NumberType, EXTENT > out_numbers)
Definition sdl_helpers.ipp:65
NumberType load_number(std::string_view sdlNumberStr)
Returns a number by processing its SDL representation. Accepts all types supported by load_float() an...
Definition sdl_helpers.ipp:50
DstType * cast_to(SrcType *srcResource)
Cast between SDL resource types. Cast the input SDL resource instance of SrcType to an instance of Ds...
Definition sdl_helpers.ipp:549
void save_integer(integer value, std::string &out_str)
Definition sdl_helpers.ipp:229
std::vector< math::TVector3< Element > > load_vector3_array(std::string_view sdlVec3ArrayStr)
Definition sdl_helpers.ipp:200
void save_vector4(const math::TVector4< Element > &value, std::string &out_str)
Definition sdl_helpers.ipp:338
void save_quaternion(const math::TQuaternion< Element > &value, std::string &out_str)
Definition sdl_helpers.ipp:353
constexpr ESdlDataType int_type_of()
Definition sdl_helpers.ipp:422
void save_number(NumberType value, std::string &out_str)
Converts a number to its SDL representation. Accepts all types supported by save_float() and save_int...
Definition sdl_helpers.ipp:261
constexpr ESdlTypeCategory category_of()
Statically gets the SDL category of T.
Definition sdl_helpers.ipp:409
void save_numbers(TSpanView< NumberType, EXTENT > numbers, std::string &out_str)
Definition sdl_helpers.ipp:276
void save_float(FloatType value, std::string &out_str)
Converts a floating-point number to its SDL representation. Supports ph::real, float,...
Definition sdl_helpers.ipp:235
constexpr ESdlDataType resource_type_of()
Definition sdl_helpers.ipp:492
FloatType load_float(std::string_view sdlFloatStr)
Returns a floating-point number by processing its SDL representation. Supports ph::real,...
Definition sdl_helpers.ipp:24
void save_real(real value, std::string &out_str)
Definition sdl_helpers.ipp:224
math::TQuaternion< Element > load_quaternion(std::string_view sdlQuatStr)
Definition sdl_helpers.ipp:160
math::TVector4< Element > load_vector4(std::string_view sdlVec4Str)
Definition sdl_helpers.ipp:145
math::TVector2< Element > load_vector2(std::string_view sdlVec2Str)
Definition sdl_helpers.ipp:115
std::vector< NumberType > load_number_array(std::string_view sdlNumberArrayStr)
Definition sdl_helpers.ipp:175
void save_vector3_array(TSpanView< math::TVector3< Element > > values, std::string &out_str)
Definition sdl_helpers.ipp:383
constexpr ESdlDataType number_type_of()
Definition sdl_helpers.ipp:478
void save_vector2(const math::TVector2< Element > &value, std::string &out_str)
Definition sdl_helpers.ipp:308
constexpr ESdlDataType float_type_of()
Definition sdl_helpers.ipp:464
math::TVector3< Element > load_vector3(std::string_view sdlVec3Str)
Definition sdl_helpers.ipp:130
real load_real(std::string_view sdlRealStr)
Definition sdl_helpers.ipp:104
std::span< const T, EXTENT > TSpanView
Same as TSpan, except that the objects are const-qualified. Note that for pointer types,...
Definition TSpan.h:19
std::span< T, EXTENT > TSpan
A contiguous sequence of objects of type T. Effectively the same as std::span.
Definition TSpan.h:12
ESdlDataType
Common data types used by SDL. Each listed data type has some form of one to one mapping to a specifi...
Definition ESdlDataType.h:13
ESdlTypeCategory
Definition ESdlTypeCategory.h:15
Low-level helpers for SDL. Helpers are in an additional sdl namespace.