WSF
RangeValidator.hpp
Go to the documentation of this file.
1// ****************************************************************************
2// CUI
3//
4// The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
5//
6// Copyright 2019 Infoscitex, a DCS Company. All rights reserved.
7//
8// The use, dissemination or disclosure of data in this file is subject to
9// limitation or restriction. See accompanying README and LICENSE for details.
10// ****************************************************************************
11
12
13#ifndef RANGEDVALIDATOR_HPP
14#define RANGEDVALIDATOR_HPP
15
16#include <iostream>
17#include <sstream>
18#include <string>
19#include <type_traits>
20
21#include "FFIRN_Validator.hpp"
22
23
24namespace usmtf
25{
26template<typename T>
27class USMTF_EXPORT RangeValidator : public FFIRN_Validator
28{
29 static_assert(std::is_arithmetic<T>::value, "Type must pass is_arithmetic<T>");
30
31public:
32 bool ParseAndValidate(const usmtf::Field& aField) override
33 {
34 SetField(aField);
35 return IsInvalidRange(aField.GetContent());
36 }
37
38 void SetValidRange(T aLowerBound, T aUpperBound)
39 {
40 mLowerBound = aLowerBound;
41 mUpperBound = aUpperBound;
42 }
43
44 T GetCastedValue() const noexcept { return mCastedRangeValue; }
45
46protected:
47 bool IsInvalidRange(T aValue) noexcept
48 {
50 {
51 return true;
52 }
53 std::string suggestedAction =
54 "Value must fall in the range [" + std::to_string(mLowerBound) + ", " + std::to_string(mUpperBound) + "]";
55 AddError("Value failed range check.", std::to_string(aValue), suggestedAction);
57 return false;
58 }
59
60 // For use on sub values in a field
61 bool IsInvalidRange(const std::string& aValue) noexcept
62 {
63 T casted;
64 std::stringstream(aValue) >> casted;
65 if (!casted && aValue != "0")
66 {
67 AddError("Error Casting value", aValue, "Please make sure value is valid int/double/float");
69 return false;
70 }
71 mCastedRangeValue = casted;
72 return IsInvalidRange(casted);
73 }
74
78};
79} // namespace usmtf
80
81#endif
void AddError(const ValidationError &aError) noexcept
Definition ErrorManager.cpp:36
static bool IsInRange(T aValue, T aLowerBound, T aUpperBound) noexcept
Definition FFIRN_Validator.hpp:78
void SetField(const Field &aField) noexcept
Field Setter for internal use only.
Definition FFIRN_Validator.cpp:57
Definition Field.hpp:48
const std::string & GetContent() const noexcept
Definition Field.cpp:24
Definition RangeValidator.hpp:28
bool IsInvalidRange(const std::string &aValue) noexcept
Definition RangeValidator.hpp:61
T GetCastedValue() const noexcept
Definition RangeValidator.hpp:44
bool IsInvalidRange(T aValue) noexcept
Definition RangeValidator.hpp:47
bool ParseAndValidate(const usmtf::Field &aField) override
Definition RangeValidator.hpp:32
T mCastedRangeValue
Definition RangeValidator.hpp:75
void SetValidRange(T aLowerBound, T aUpperBound)
Definition RangeValidator.hpp:38
T mLowerBound
Definition RangeValidator.hpp:76
T mUpperBound
Definition RangeValidator.hpp:77
Definition Acmid.cpp:17
Copyrights Multiple, All Rights Reserved