/***************************************************************************************** * * * OpenSpace * * * * Copyright (c) 2014-2017 * * * * 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. * ****************************************************************************************/ #include #include #include namespace std { std::string to_string(std::string value); } // namespace std namespace openspace::documentation { template TestResult TemplateVerifier::operator()(const ghoul::Dictionary& dict, const std::string& key) const { if (dict.hasKeyAndValue(key)) { return { true, {}, {} }; } else { if (dict.hasKey(key)) { return { false, { { key, TestResult::Offense::Reason::WrongType } }, {} }; } else { return { false, { { key, TestResult::Offense::Reason::MissingKey } }, {} }; } } } template std::string TemplateVerifier::documentation() const { return "Value of type '" + type() + "'"; } template std::string Vector2Verifier::type() const { return std::string("Vector2<") + typeid(T).name() + ">"; } template std::string Vector3Verifier::type() const { return std::string("Vector3<") + typeid(T).name() + ">"; } template std::string Vector4Verifier::type() const { return std::string("Vector4<") + typeid(T).name() + ">"; } template std::string Matrix2x2Verifier::type() const { return std::string("Matrix2x2<") + typeid(T).name() + ">"; } template std::string Matrix2x3Verifier::type() const { return std::string("Matrix2x3<") + typeid(T).name() + ">"; } template std::string Matrix2x4Verifier::type() const { return std::string("Matrix2x4<") + typeid(T).name() + ">"; } template std::string Matrix3x2Verifier::type() const { return std::string("Matrix3x2<") + typeid(T).name() + ">"; } template std::string Matrix3x3Verifier::type() const { return std::string("Matrix3x3<") + typeid(T).name() + ">"; } template std::string Matrix3x4Verifier::type() const { return std::string("Matrix3x4<") + typeid(T).name() + ">"; } template std::string Matrix4x2Verifier::type() const { return std::string("Matrix4x2<") + typeid(T).name() + ">"; } template std::string Matrix4x3Verifier::type() const { return std::string("Matrix4x3<") + typeid(T).name() + ">"; } template std::string Matrix4x4Verifier::type() const { return std::string("Matrix4x4<") + typeid(T).name() + ">"; } template OperatorVerifier::OperatorVerifier(typename T::Type val) : value(std::move(val)) {} template TestResult OperatorVerifier::operator()(const ghoul::Dictionary& dict, const std::string& key) const { TestResult res = T::operator()(dict, key); if (res.success) { if (Operator()(dict.value(key), value)) { return { true, {}, {} }; } else { return { false, { { key, TestResult::Offense::Reason::Verification } }, {} }; } } else { return res; } } template std::string LessVerifier::documentation() const { return "Less than: " + std::to_string(value); } template std::string LessEqualVerifier::documentation() const { return "Less or equal to: " + std::to_string(value); } template std::string GreaterVerifier::documentation() const { return "Greater than: " + std::to_string(value); } template std::string GreaterEqualVerifier::documentation() const { return "Greater or equal to: " + std::to_string(value); } template std::string EqualVerifier::documentation() const { return "Equal to: " + std::to_string(value); } template std::string UnequalVerifier::documentation() const { return "Unequal to: " + std::to_string(value); } template InListVerifier::InListVerifier(std::vector vals) : values(std::move(vals)) {} template TestResult InListVerifier::operator()(const ghoul::Dictionary& dict, const std::string& key) const { TestResult res = T::operator()(dict, key); if (res.success) { typename T::Type value = dict.value(key); auto it = std::find(values.begin(), values.end(), value); if (it != values.end()) { return { true, {}, {} }; } else { return { false, { { key, TestResult::Offense::Reason::Verification } }, {} }; } } else { return res; } } template std::string InListVerifier::documentation() const { std::string result = "In list { "; std::stringstream s; std::copy( values.begin(), values.end(), std::ostream_iterator(s, ",") ); std::string joined = s.str(); // We need to remove a trailing ',' at the end of the string result += joined.substr(0, joined.size() - 1); result += " }"; return result; } template NotInListVerifier::NotInListVerifier(std::vector vals) : values(std::move(vals)) {} template TestResult NotInListVerifier::operator()(const ghoul::Dictionary& dict, const std::string& key) const { TestResult res = T::operator()(dict, key); if (res.success) { typename T::Type value = dict.value(key); auto it = std::find(values.begin(), values.end(), value); if (it == values.end()) { return { true, {}, {} }; } else { return { false, { { key, TestResult::Offense::Reason::Verification } }, {} }; } } else { return res; } } template std::string NotInListVerifier::documentation() const { std::string result = "Not in list { "; std::stringstream s; std::copy( values.begin(), values.end(), std::ostream_iterator(s, ",") ); std::string joined = s.str(); // We need to remove a trailing ',' at the end of the string result += joined.substr(0, joined.size() - 1); result += " }"; return result; } template InRangeVerifier::InRangeVerifier(typename T::Type l, typename T::Type u) : lower(std::move(l)) , upper(std::move(u)) { ghoul_assert(lower <= upper, "lower must be smaller or equal to upper"); } template TestResult InRangeVerifier::operator()(const ghoul::Dictionary& dict, const std::string& key) const { TestResult res = T::operator()(dict, key); if (res.success) { typename T::Type val = dict.value(key); if (val >= lower && val <= upper) { return { true, {}, {} }; } else { return { false, { { key, TestResult::Offense::Reason::Verification } }, {} }; } } else { return res; } } template std::string InRangeVerifier::documentation() const { return "In range: ( " + std::to_string(lower) + "," + std::to_string(upper) + " )"; } template NotInRangeVerifier::NotInRangeVerifier(typename T::Type l, typename T::Type u) : lower(std::move(l)) , upper(std::move(u)) { ghoul_assert(lower <= upper, "lower must be smaller or equal to upper"); } template TestResult NotInRangeVerifier::operator()(const ghoul::Dictionary& dict, const std::string& key) const { TestResult res = T::operator()(dict, key); if (res.success) { typename T::Type val = dict.value(key); if (val >= lower && val <= upper) { return { false, { { key, TestResult::Offense::Reason::Verification } }, {} }; } else { return { true, {}, {} }; } } else { return res; } } template std::string NotInRangeVerifier::documentation() const { return "Not in range: ( " + std::to_string(lower) + "," + std::to_string(upper) + " )"; } template AnnotationVerifier::AnnotationVerifier(std::string a) : annotation(std::move(a)) { ghoul_assert(!annotation.empty(), "Annotation must not be empty"); } template std::string AnnotationVerifier::documentation() const { return annotation; } template TestResult DeprecatedVerifier::operator()(const ghoul::Dictionary& dict, const std::string& key) const { TestResult res = T::operator()(dict, key); res.warnings.push_back(TestResult::Warning{ key, TestResult::Warning::Reason::Deprecated }); return res; } template std::string DeprecatedVerifier::documentation() const { return T::documentation() + " (deprecated)"; } } // namespace openspace::documentation