Caffa  1.1.0
C++ Application Framework for Embedded Systems with introspection
cafRangeValidator.h
1 // ##################################################################################################
2 //
3 // Custom Visualization Core library
4 // Copyright (C) 2021- 3D-Radar AS
5 //
6 // This library may be used under the terms of the GNU Lesser General Public License as follows:
7 //
8 // GNU Lesser General Public License Usage
9 // This library is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU Lesser General Public License as published by
11 // the Free Software Foundation; either version 2.1 of the License, or
12 // (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful, but WITHOUT ANY
15 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 // FITNESS FOR A PARTICULAR PURPOSE.
17 //
18 // See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
19 // for more details.
20 //
21 // ##################################################################################################
22 #pragma once
23 
24 #include "cafAssert.h"
25 #include "cafFieldValidator.h"
26 
27 #include <nlohmann/json.hpp>
28 
29 #include <memory>
30 #include <sstream>
31 #include <string>
32 #include <utility>
33 
34 namespace caffa
35 {
36 class JsonSerializer;
42 template <typename DataType>
43 class RangeValidator : public FieldValidator<DataType>
44 {
45 public:
47 
48  RangeValidator( DataType minimum, DataType maximum, FailureSeverity failureSeverity = FailureSeverity::VALIDATOR_ERROR )
50  , m_minimum( minimum )
51  , m_maximum( maximum )
52  {
53  }
54 
55  void readFromString( const std::string& string ) override
56  {
57  auto jsonFieldObject = nlohmann::json::parse( string );
58  if ( jsonFieldObject.is_object() )
59  {
60  if ( jsonFieldObject.contains( "minimum" ) )
61  {
62  m_minimum = jsonFieldObject["minimum"];
63  }
64  if ( jsonFieldObject.contains( "maximum" ) )
65  {
66  m_maximum = jsonFieldObject["maximum"];
67  }
68  }
69  }
70 
71  std::string writeToString() const override
72  {
73  auto jsonFieldObject = nlohmann::json::object();
74  jsonFieldObject["minimum"] = m_minimum;
75  jsonFieldObject["maximum"] = m_maximum;
76  return jsonFieldObject.dump();
77  }
78 
79  std::pair<bool, std::string> validate( const DataType& value ) const override
80  {
81  bool valid = m_minimum <= value && value <= m_maximum;
82  if ( !valid )
83  {
84  std::stringstream ss;
85  ss << "The value " << value << " is outside the limits [" << m_minimum << ", " << m_maximum << "]";
86  return std::make_pair( false, ss.str() );
87  }
88  return std::make_pair( true, "" );
89  }
90 
91  static std::unique_ptr<RangeValidator<DataType>>
92  create( DataType minimum, DataType maximum, FailureSeverity failureSeverity = FailureSeverity::VALIDATOR_ERROR )
93  {
94  return std::make_unique<RangeValidator<DataType>>( minimum, maximum, failureSeverity );
95  }
96 
97 private:
98  DataType m_minimum;
99  DataType m_maximum;
100 };
101 
102 } // namespace caffa
Simple range validator.
Definition: cafRangeValidator.h:43
Used to validate the value of data fields Implementations need the the validate method as well as rea...
Definition: cafFieldValidator.h:94
std::string writeToString() const override
Write the validator tostring.
Definition: cafRangeValidator.h:71
std::pair< bool, std::string > validate(const DataType &value) const override
Validate the value.
Definition: cafRangeValidator.h:79
FailureSeverity failureSeverity() const
Get the severity of a failure of the validator.
Definition: cafFieldValidator.h:81
FailureSeverity
The severity of failure. Essentially tells the application how to treat a validator failure: VALIDATO...
Definition: cafFieldValidator.h:44
void readFromString(const std::string &string) override
Read the validator from string.
Definition: cafRangeValidator.h:55
Main Caffa namespace.
Definition: cafApplication.h:30