#ifndef Magnum_Math_ConfigurationValue_h #define Magnum_Math_ConfigurationValue_h /* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022 Vladimír Vondruš Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** @file * @brief Configuration value parsers and writers for @ref Magnum::Math types * @m_since{2019,10} */ #include #include #include "Magnum/Math/Angle.h" #include "Magnum/Math/Bezier.h" #include "Magnum/Math/Color.h" #include "Magnum/Math/Complex.h" #include "Magnum/Math/DualComplex.h" #include "Magnum/Math/DualQuaternion.h" #include "Magnum/Math/Matrix.h" #include "Magnum/Math/Matrix3.h" #include "Magnum/Math/Matrix4.h" #include "Magnum/Math/Range.h" #include "Magnum/Math/RectangularMatrix.h" #include "Magnum/Math/Quaternion.h" #include "Magnum/Math/Vector.h" #include "Magnum/Math/Vector2.h" #include "Magnum/Math/Vector3.h" #include "Magnum/Math/Vector4.h" namespace Corrade { namespace Utility { /** @configurationvalue{Magnum::Math::Deg} */ template struct ConfigurationValue> { ConfigurationValue() = delete; /** @brief Writes degrees as a number */ static Containers::String toString(const Magnum::Math::Deg& value, ConfigurationValueFlags flags) { return ConfigurationValue::toString(T(value), flags); } /** @brief Reads degrees as a number */ static Magnum::Math::Deg fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) { return Magnum::Math::Deg(ConfigurationValue::fromString(stringValue, flags)); } }; /** @configurationvalue{Magnum::Math::Rad} */ template struct ConfigurationValue> { ConfigurationValue() = delete; /** @brief Writes degrees as a number */ static Containers::String toString(const Magnum::Math::Rad& value, ConfigurationValueFlags flags) { return ConfigurationValue::toString(T(value), flags); } /** @brief Reads degrees as a number */ static Magnum::Math::Rad fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) { return Magnum::Math::Rad(ConfigurationValue::fromString(stringValue, flags)); } }; /** @configurationvalue{Magnum::Math::Vector} */ template struct ConfigurationValue> { ConfigurationValue() = delete; /** @brief Writes elements separated with spaces */ static Containers::String toString(const Magnum::Math::Vector& value, ConfigurationValueFlags flags) { Containers::String output; for(std::size_t i = 0; i != size; ++i) { if(!output.isEmpty()) output = output + " "; output = output + ConfigurationValue::toString(value[i], flags); } return output; } /** @brief Reads elements separated with whitespace */ static Magnum::Math::Vector fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) { Magnum::Math::Vector result; Containers::StringView next, cursor = stringValue; std::size_t i = 0; do { next = cursor.find(" "); Containers::StringView part = cursor.exceptSuffix(next); if(!part.isEmpty()) { result[i] = ConfigurationValue::fromString(part, flags); ++i; } cursor = cursor.exceptPrefix(next); } while(cursor); return result; } }; /* Explicit instantiation for commonly used types */ #if !defined(DOXYGEN_GENERATING_OUTPUT) && !defined(__MINGW32__) extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; #endif /** @configurationvalue{Magnum::Math::Vector2} */ template struct ConfigurationValue>: public ConfigurationValue> {}; /** @configurationvalue{Magnum::Math::Vector3} */ template struct ConfigurationValue>: public ConfigurationValue> {}; /** @configurationvalue{Magnum::Math::Vector4} */ template struct ConfigurationValue>: public ConfigurationValue> {}; /** @configurationvalue{Magnum::Color3} */ template struct ConfigurationValue>: ConfigurationValue> {}; /** @configurationvalue{Magnum::Color4} */ template struct ConfigurationValue>: ConfigurationValue> {}; /* No explicit instantiation for Vector[234] / Color[34] needed, as Vector<2, T> / Vector<3, T> / Vector<4, T> is instantiated already */ /** @configurationvalue{Magnum::Math::RectangularMatrix} */ template struct ConfigurationValue> { ConfigurationValue() = delete; /** @brief Writes elements separated with spaces */ static Containers::String toString(const Magnum::Math::RectangularMatrix& value, ConfigurationValueFlags flags) { Containers::String output; for(std::size_t row = 0; row != rows; ++row) { for(std::size_t col = 0; col != cols; ++col) { if(!output.isEmpty()) output = output + " "; output = output + ConfigurationValue::toString(value[col][row], flags); } } return output; } /** @brief Reads elements separated with whitespace */ static Magnum::Math::RectangularMatrix fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) { Magnum::Math::RectangularMatrix result; Containers::StringView next, cursor = stringValue; std::size_t i = 0; do { next = cursor.find(" "); Containers::StringView part = cursor.exceptSuffix(next); if(!part.isEmpty()) { result[i%cols][i/cols] = ConfigurationValue::fromString(part, flags); ++i; } cursor = cursor.exceptPrefix(next); } while(cursor); return result; } }; /* Explicit instantiation for commonly used types */ #if !defined(DOXYGEN_GENERATING_OUTPUT) && !defined(__MINGW32__) /* Square matrices */ extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; /* Rectangular matrices */ extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; #endif /** @configurationvalue{Magnum::Math::Matrix} */ template struct ConfigurationValue>: public ConfigurationValue> {}; /** @configurationvalue{Magnum::Math::Matrix3} */ template struct ConfigurationValue>: public ConfigurationValue> {}; /** @configurationvalue{Magnum::Math::Matrix4} */ template struct ConfigurationValue>: public ConfigurationValue> {}; /* No explicit instantiation for Matrix needed, as RectangularMatrix is instantiated already */ /** @configurationvalue{Magnum::Math::Range} */ template struct ConfigurationValue> { ConfigurationValue() = delete; /** @brief Writes elements separated with spaces */ static Containers::String toString(const Magnum::Math::Range& value, const ConfigurationValueFlags flags) { return ConfigurationValue>::toString( reinterpret_cast&>(value), flags); } /** @brief Reads elements separated with whitespace */ static Magnum::Math::Range fromString(Containers::StringView stringValue, const ConfigurationValueFlags flags) { const auto vec = ConfigurationValue>::fromString(stringValue, flags); return *reinterpret_cast*>(vec.data()); } }; /** @configurationvalue{Magnum::Math::Range2D} */ template struct ConfigurationValue>: public ConfigurationValue> {}; /** @configurationvalue{Magnum::Math::Range3D} */ template struct ConfigurationValue>: public ConfigurationValue> {}; /* Explicit instantiation for commonly used types */ #if !defined(DOXYGEN_GENERATING_OUTPUT) && !defined(__MINGW32__) extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; #endif /** @configurationvalue{Magnum::Math::Complex} */ template struct ConfigurationValue> { ConfigurationValue() = delete; /** @brief Writes elements separated with spaces */ static Containers::String toString(const Magnum::Math::Complex& value, ConfigurationValueFlags flags) { return ConfigurationValue>::toString(reinterpret_cast&>(value), flags); } /** @brief Reads elements separated with whitespace */ static Magnum::Math::Complex fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) { const Magnum::Math::Vector<2, T> value = ConfigurationValue>::fromString(stringValue, flags); return reinterpret_cast&>(value); } }; /** @configurationvalue{Magnum::Math::DualComplex} */ template struct ConfigurationValue> { ConfigurationValue() = delete; /** @brief Writes elements separated with spaces */ static Containers::String toString(const Magnum::Math::DualComplex& value, ConfigurationValueFlags flags) { return ConfigurationValue>::toString(reinterpret_cast&>(value), flags); } /** @brief Reads elements separated with whitespace */ static Magnum::Math::DualComplex fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) { const Magnum::Math::Vector<4, T> value = ConfigurationValue>::fromString(stringValue, flags); return reinterpret_cast&>(value); } }; /* No explicit instantiation for DualComplex needed, as Vector<4, T> is instantiated already */ /** @configurationvalue{Magnum::Math::Quaternion} */ template struct ConfigurationValue> { ConfigurationValue() = delete; /** @brief Writes elements separated with spaces */ static Containers::String toString(const Magnum::Math::Quaternion& value, ConfigurationValueFlags flags) { return ConfigurationValue>::toString(reinterpret_cast&>(value), flags); } /** @brief Reads elements separated with whitespace */ static Magnum::Math::Quaternion fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) { const Magnum::Math::Vector<4, T> value = ConfigurationValue>::fromString(stringValue, flags); return reinterpret_cast&>(value); } }; /* No explicit instantiation needed for Quaternion, as Vector<4, T> is instantiated already */ /** @configurationvalue{Magnum::Math::DualQuaternion} */ template struct ConfigurationValue> { ConfigurationValue() = delete; /** @brief Writes elements separated with spaces */ static Containers::String toString(const Magnum::Math::DualQuaternion& value, ConfigurationValueFlags flags) { return ConfigurationValue>::toString(reinterpret_cast&>(value), flags); } /** @brief Reads elements separated with whitespace */ static Magnum::Math::DualQuaternion fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) { const Magnum::Math::Vector<8, T> value = ConfigurationValue>::fromString(stringValue, flags); return reinterpret_cast&>(value); } }; /* Explicit instantiation for commonly used types */ #if !defined(DOXYGEN_GENERATING_OUTPUT) && !defined(__MINGW32__) extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; #endif /** @configurationvalue{Magnum::Math::Bezier} */ template struct ConfigurationValue> { ConfigurationValue() = delete; /** @brief Writes elements separated with spaces */ static Containers::String toString(const Magnum::Math::Bezier& value, ConfigurationValueFlags flags) { Containers::String output; for(std::size_t o = 0; o != order + 1; ++o) { for(std::size_t i = 0; i != dimensions; ++i) { if(!output.isEmpty()) output = output + " "; output = output + ConfigurationValue::toString(value[o][i], flags); } } return output; } /** @brief Reads elements separated with whitespace */ static Magnum::Math::Bezier fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) { Magnum::Math::Bezier result; Containers::StringView next, cursor = stringValue; std::size_t i = 0; do { next = cursor.find(" "); Containers::StringView part = cursor.exceptSuffix(next); if(!part.isEmpty()) { result[i/dimensions][i%dimensions] = ConfigurationValue::fromString(part, flags); ++i; } cursor = cursor.exceptPrefix(next); } while(cursor); return result; } }; /* Explicit instantiation for commonly used types */ #if !defined(DOXYGEN_GENERATING_OUTPUT) && !defined(__MINGW32__) extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; #endif }} #endif