SSAGES  0.9.3
Software Suite for Advanced General Ensemble Simulations
NumberRequirement.h
1 
26 #pragma once
27 
28 #include <cmath>
29 #include "Requirement.h"
30 
31 namespace Json
32 {
34 
40  {
41  private:
42  std::string path_;
43  double multipleOf_;
44  double min_;
45  double max_;
46  bool multSet_;
47  bool minSet_;
48  bool maxSet_;
49  bool exclMin_;
50  bool exclMax_;
51 
52 
53  public:
56  path_(), multipleOf_(0), min_(0), max_(0), multSet_(false),
57  minSet_(false), maxSet_(false), exclMin_(false), exclMax_(false)
58  {}
59 
61  virtual void Reset() override
62  {
63  multipleOf_ = 0;
64  minSet_ = maxSet_ = false;
65  exclMin_ = exclMax_ = false;
66  min_ = max_ = 0;
67  multSet_ = false;
68  ClearErrors();
69  ClearNotices();
70  }
71 
73 
77  virtual void Parse(Value json, const std::string& path) override
78  {
79  Reset();
80 
81  path_ = path;
82  if(json.isMember("multipleOf") && json["multipleOf"].isNumeric())
83  {
84  multSet_ = true;
85  multipleOf_ = json["multipleOf"].asDouble();
86  }
87 
88  if(json.isMember("minimum") && json["minimum"].isNumeric())
89  {
90  minSet_ = true;
91  min_ = json["minimum"].asDouble();
92  }
93 
94  if(json.isMember("maximum") && json["maximum"].isNumeric())
95  {
96  maxSet_ = true;
97  max_ = json["maximum"].asDouble();
98  }
99 
100  if(json.isMember("exclusiveMinimum") && json["exclusiveMinimum"].isBool())
101  {
102  exclMin_ = json["exclusiveMinimum"].asBool();
103  }
104 
105  if(json.isMember("exclusiveMaximum") && json["exclusiveMaximum"].isBool())
106  {
107  exclMax_ = json["exclusiveMaximum"].asBool();
108  }
109  }
110 
112 
120  virtual void Validate(const Value& json, const std::string& path) override
121  {
122  if(!json.isNumeric())
123  {
124  PushError(path + ": Must be of type \"number\".");
125  return;
126  }
127 
128  if(multSet_ && fmod(json.asDouble(), multipleOf_) != 0)
129  PushError(path + ": Value must be a multiple of " + std::to_string(multipleOf_));
130 
131  if(minSet_)
132  {
133  if(exclMin_ && json.asDouble() <= min_)
134  PushError(path + ": Value must be greater than " + std::to_string(min_));
135  else if(json.asDouble() < min_)
136  PushError(path + ": Value cannot be less than " + std::to_string(min_));
137  }
138 
139  if(maxSet_)
140  {
141  if(exclMax_ && json.asDouble() >= max_)
142  PushError(path + ": Value must be less than " + std::to_string(max_));
143  else if(json.asDouble() > max_)
144  PushError(path + ": Value cannot be greater than " + std::to_string(max_));
145  }
146  }
147  };
148 }
Requirements on a numeric value.
virtual void Parse(Value json, const std::string &path) override
Parse JSON value to set up Requirement.
double max_
Upper bound for range requirement.
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.
bool exclMax_
If True, upper bound is exclusive.
bool exclMin_
If True, lower bound is exclusive.
double min_
Lower bound for range requirement.
bool multSet_
If True, "Multiple of" requirement is active.
NumberRequirement()
Constructor.
bool maxSet_
If True, Upper bound for range requirement is active.
bool minSet_
If True, Lower bound for range requirement is active.
double multipleOf_
Base value for "multiple of" requirement.
virtual void Reset() override
Reset Requirement.
std::string path_
JSON path.
Requirements on input files.
Definition: Requirement.h:40
virtual void ClearNotices()
Clear list of notice messages.
Definition: Requirement.h:110
virtual void ClearErrors()
Clear list of error messages.
Definition: Requirement.h:95
void PushError(const std::string &error)
Add error to list of error messages.
Definition: Requirement.h:53