#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 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 std::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(const std::string& 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 std::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(const std::string& 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 std::string toString(const Magnum::Math::Vector& value, ConfigurationValueFlags flags) { std::string output; for(std::size_t i = 0; i != size; ++i) { if(!output.empty()) output += ' '; output += ConfigurationValue::toString(value[i], flags); } return output; } /** @brief Reads elements separated with whitespace */ static Magnum::Math::Vector fromString(const std::string& stringValue, ConfigurationValueFlags flags) { Magnum::Math::Vector result; std::size_t oldpos = 0, pos = std::string::npos, i = 0; do { pos = stringValue.find(' ', oldpos); std::string part = stringValue.substr(oldpos, pos-oldpos); if(!part.empty()) { result[i] = ConfigurationValue::fromString(part, flags); ++i; } oldpos = pos+1; } while(pos != std::string::npos && i != size); 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 std::string toString(const Magnum::Math::RectangularMatrix& value, ConfigurationValueFlags flags) { std::string output; for(std::size_t row = 0; row != rows; ++row) { for(std::size_t col = 0; col != cols; ++col) { if(!output.empty()) output += ' '; output += ConfigurationValue::toString(value[col][row], flags); } } return output; } /** @brief Reads elements separated with whitespace */ static Magnum::Math::RectangularMatrix fromString(const std::string& stringValue, ConfigurationValueFlags flags) { Magnum::Math::RectangularMatrix result; std::size_t oldpos = 0, pos = std::string::npos, i = 0; do { pos = stringValue.find(' ', oldpos); std::string part = stringValue.substr(oldpos, pos-oldpos); if(!part.empty()) { result[i%cols][i/cols] = ConfigurationValue::fromString(part, flags); ++i; } oldpos = pos+1; } while(pos != std::string::npos && i != cols*rows); 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 std::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(const std::string& 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 std::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(const std::string& 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 std::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(const std::string& 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 std::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(const std::string& 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 std::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(const std::string& 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 std::string toString(const Magnum::Math::Bezier& value, ConfigurationValueFlags flags) { std::string output; for(std::size_t o = 0; o != order + 1; ++o) { for(std::size_t i = 0; i != dimensions; ++i) { if(!output.empty()) output += ' '; output += ConfigurationValue::toString(value[o][i], flags); } } return output; } /** @brief Reads elements separated with whitespace */ static Magnum::Math::Bezier fromString(const std::string& stringValue, ConfigurationValueFlags flags) { Magnum::Math::Bezier result; std::size_t oldpos = 0, pos = std::string::npos, i = 0; do { pos = stringValue.find(' ', oldpos); std::string part = stringValue.substr(oldpos, pos-oldpos); if(!part.empty()) { result[i/dimensions][i%dimensions] = ConfigurationValue::fromString(part, flags); ++i; } oldpos = pos+1; } while(pos != std::string::npos); 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