MQTTSuite
Loading...
Searching...
No Matches
anonymous_namespace{json-validator.cpp}::numeric< T > Class Template Reference
Inheritance diagram for anonymous_namespace{json-validator.cpp}::numeric< T >:
Collaboration diagram for anonymous_namespace{json-validator.cpp}::numeric< T >:

Public Member Functions

 numeric (const json &sch, root_schema *root, std::set< std::string > &kw)
 numeric (const json &sch, root_schema *root, std::set< std::string > &kw)
Public Member Functions inherited from anonymous_namespace{json-validator.cpp}::schema
virtual ~schema ()=default
 schema (root_schema *root)
virtual const jsondefault_value (const json::json_pointer &, const json &, error_handler &) const
void set_default_value (const json &v)
virtual ~schema ()=default
 schema (root_schema *root)
virtual const jsondefault_value (const json::json_pointer &, const json &, error_handler &) const
void set_default_value (const json &v)

Private Member Functions

bool violates_multiple_of (T x) const
void validate (const json::json_pointer &ptr, const json &instance, json_patch &, error_handler &e) const override
bool violates_multiple_of (T x) const
void validate (const json::json_pointer &ptr, const json &instance, json_patch &, error_handler &e) const override

Private Attributes

std::pair< bool, T > maximum_ {false, 0}
std::pair< bool, T > minimum_ {false, 0}
bool exclusiveMaximum_ = false
bool exclusiveMinimum_ = false
std::pair< bool, json::number_float_t > multipleOf_ {false, 0}

Additional Inherited Members

Static Public Member Functions inherited from anonymous_namespace{json-validator.cpp}::schema
static std::shared_ptr< schemamake (json &schema, root_schema *root, const std::vector< std::string > &key, std::vector< nlohmann::json_uri > uris)
static std::shared_ptr< schemamake (json &schema, root_schema *root, const std::vector< std::string > &key, std::vector< nlohmann::json_uri > uris)
Protected Member Functions inherited from anonymous_namespace{json-validator.cpp}::schema
virtual std::shared_ptr< schemamake_for_default_ (std::shared_ptr<::schema > &, root_schema *, std::vector< nlohmann::json_uri > &, nlohmann::json &) const
virtual std::shared_ptr< schemamake_for_default_ (std::shared_ptr<::schema > &, root_schema *, std::vector< nlohmann::json_uri > &, nlohmann::json &) const
Protected Attributes inherited from anonymous_namespace{json-validator.cpp}::schema
root_schemaroot_
json default_value_ = nullptr

Detailed Description

template<typename T>
class anonymous_namespace{json-validator.cpp}::numeric< T >

Definition at line 885 of file json-validator.cpp.

Constructor & Destructor Documentation

◆ numeric() [1/2]

template<typename T>
anonymous_namespace{json-validator.cpp}::numeric< T >::numeric ( const json & sch,
root_schema * root,
std::set< std::string > & kw )
inline

Definition at line 941 of file json-validator.cpp.

942 : schema(root)
943 {
944 auto attr = sch.find("maximum");
945 if (attr != sch.end()) {
946 maximum_ = {true, attr.value().get<T>()};
947 kw.insert("maximum");
948 }
949
950 attr = sch.find("minimum");
951 if (attr != sch.end()) {
952 minimum_ = {true, attr.value().get<T>()};
953 kw.insert("minimum");
954 }
955
956 attr = sch.find("exclusiveMaximum");
957 if (attr != sch.end()) {
958 exclusiveMaximum_ = true;
959 maximum_ = {true, attr.value().get<T>()};
960 kw.insert("exclusiveMaximum");
961 }
962
963 attr = sch.find("exclusiveMinimum");
964 if (attr != sch.end()) {
965 exclusiveMinimum_ = true;
966 minimum_ = {true, attr.value().get<T>()};
967 kw.insert("exclusiveMinimum");
968 }
969
970 attr = sch.find("multipleOf");
971 if (attr != sch.end()) {
972 multipleOf_ = {true, attr.value().get<json::number_float_t>()};
973 kw.insert("multipleOf");
974 }
975 }
std::pair< bool, json::number_float_t > multipleOf_
auto schema
Definition id-ref.cpp:69

References exclusiveMaximum_, exclusiveMinimum_, maximum_, minimum_, and multipleOf_.

◆ numeric() [2/2]

template<typename T>
anonymous_namespace{json-validator.cpp}::numeric< T >::numeric ( const json & sch,
root_schema * root,
std::set< std::string > & kw )
inline

Definition at line 941 of file json-validator.cpp.

942 : schema(root)
943 {
944 auto attr = sch.find("maximum");
945 if (attr != sch.end()) {
946 maximum_ = {true, attr.value().get<T>()};
947 kw.insert("maximum");
948 }
949
950 attr = sch.find("minimum");
951 if (attr != sch.end()) {
952 minimum_ = {true, attr.value().get<T>()};
953 kw.insert("minimum");
954 }
955
956 attr = sch.find("exclusiveMaximum");
957 if (attr != sch.end()) {
958 exclusiveMaximum_ = true;
959 maximum_ = {true, attr.value().get<T>()};
960 kw.insert("exclusiveMaximum");
961 }
962
963 attr = sch.find("exclusiveMinimum");
964 if (attr != sch.end()) {
965 exclusiveMinimum_ = true;
966 minimum_ = {true, attr.value().get<T>()};
967 kw.insert("exclusiveMinimum");
968 }
969
970 attr = sch.find("multipleOf");
971 if (attr != sch.end()) {
972 multipleOf_ = {true, attr.value().get<json::number_float_t>()};
973 kw.insert("multipleOf");
974 }
975 }

Member Function Documentation

◆ validate() [1/2]

template<typename T>
void anonymous_namespace{json-validator.cpp}::numeric< T >::validate ( const json::json_pointer & ptr,
const json & instance,
json_patch & ,
error_handler & e ) const
inlineoverrideprivatevirtual

Implements anonymous_namespace{json-validator.cpp}::schema.

Definition at line 908 of file json-validator.cpp.

909 {
910 T value = instance; // conversion of json to value_type
911
913
914 if (multipleOf_.first && value != 0) // zero is multiple of everything
916 oss << "instance is not a multiple of " << json(multipleOf_.second);
917
918 if (maximum_.first) {
919 if (exclusiveMaximum_ && value >= maximum_.second)
920 oss << "instance exceeds or equals maximum of " << json(maximum_.second);
921 else if (value > maximum_.second)
922 oss << "instance exceeds maximum of " << json(maximum_.second);
923 }
924
925 if (minimum_.first) {
926 if (exclusiveMinimum_ && value <= minimum_.second)
927 oss << "instance is below or equals minimum of " << json(minimum_.second);
928 else if (value < minimum_.second)
929 oss << "instance is below minimum of " << json(minimum_.second);
930 }
931
932 oss.seekp(0, std::ios::end);
933 auto size = oss.tellp();
934 if (size != 0) {
935 oss.seekp(0, std::ios::beg);
936 e.error(ptr, instance, oss.str());
937 }
938 }
nlohmann::json json

References nlohmann::json_schema::error_handler::error(), exclusiveMaximum_, exclusiveMinimum_, maximum_, minimum_, multipleOf_, and violates_multiple_of().

Here is the call graph for this function:

◆ validate() [2/2]

template<typename T>
void anonymous_namespace{json-validator.cpp}::numeric< T >::validate ( const json::json_pointer & ptr,
const json & instance,
json_patch & ,
error_handler & e ) const
inlineoverrideprivatevirtual

Implements anonymous_namespace{json-validator.cpp}::schema.

Definition at line 908 of file json-validator.cpp.

909 {
910 T value = instance; // conversion of json to value_type
911
913
914 if (multipleOf_.first && value != 0) // zero is multiple of everything
916 oss << "instance is not a multiple of " << json(multipleOf_.second);
917
918 if (maximum_.first) {
919 if (exclusiveMaximum_ && value >= maximum_.second)
920 oss << "instance exceeds or equals maximum of " << json(maximum_.second);
921 else if (value > maximum_.second)
922 oss << "instance exceeds maximum of " << json(maximum_.second);
923 }
924
925 if (minimum_.first) {
926 if (exclusiveMinimum_ && value <= minimum_.second)
927 oss << "instance is below or equals minimum of " << json(minimum_.second);
928 else if (value < minimum_.second)
929 oss << "instance is below minimum of " << json(minimum_.second);
930 }
931
932 oss.seekp(0, std::ios::end);
933 auto size = oss.tellp();
934 if (size != 0) {
935 oss.seekp(0, std::ios::beg);
936 e.error(ptr, instance, oss.str());
937 }
938 }

◆ violates_multiple_of() [1/2]

template<typename T>
bool anonymous_namespace{json-validator.cpp}::numeric< T >::violates_multiple_of ( T x) const
inlineprivate

Definition at line 896 of file json-validator.cpp.

897 {
898 double res = std::remainder(x, multipleOf_.second);
899 double multiple = std::fabs(static_cast<double>(x) / multipleOf_.second);
900 if (multiple > 1) {
901 res = res / multiple;
902 }
903 double eps = std::nextafter(x, 0) - static_cast<double>(x);
904
905 return std::fabs(res) > std::fabs(eps);
906 }

References multipleOf_.

Referenced by validate().

Here is the caller graph for this function:

◆ violates_multiple_of() [2/2]

template<typename T>
bool anonymous_namespace{json-validator.cpp}::numeric< T >::violates_multiple_of ( T x) const
inlineprivate

Definition at line 896 of file json-validator.cpp.

897 {
898 double res = std::remainder(x, multipleOf_.second);
899 double multiple = std::fabs(static_cast<double>(x) / multipleOf_.second);
900 if (multiple > 1) {
901 res = res / multiple;
902 }
903 double eps = std::nextafter(x, 0) - static_cast<double>(x);
904
905 return std::fabs(res) > std::fabs(eps);
906 }

Member Data Documentation

◆ exclusiveMaximum_

template<typename T>
bool anonymous_namespace{json-validator.cpp}::numeric< T >::exclusiveMaximum_ = false
private

Definition at line 890 of file json-validator.cpp.

Referenced by numeric(), and validate().

◆ exclusiveMinimum_

template<typename T>
bool anonymous_namespace{json-validator.cpp}::numeric< T >::exclusiveMinimum_ = false
private

Definition at line 891 of file json-validator.cpp.

Referenced by numeric(), and validate().

◆ maximum_

template<typename T>
std::pair< bool, T > anonymous_namespace{json-validator.cpp}::numeric< T >::maximum_ {false, 0}
private

Definition at line 887 of file json-validator.cpp.

887{false, 0};

Referenced by numeric(), and validate().

◆ minimum_

template<typename T>
std::pair< bool, T > anonymous_namespace{json-validator.cpp}::numeric< T >::minimum_ {false, 0}
private

Definition at line 888 of file json-validator.cpp.

888{false, 0};

Referenced by numeric(), and validate().

◆ multipleOf_

template<typename T>
std::pair< bool, json::number_float_t > anonymous_namespace{json-validator.cpp}::numeric< T >::multipleOf_ {false, 0}
private

Definition at line 893 of file json-validator.cpp.

893{false, 0};

Referenced by numeric(), validate(), and violates_multiple_of().


The documentation for this class was generated from the following file: