ImGraph
 All Classes Functions Variables Enumerations Pages
ParamValidator.h
1 #ifndef _BLOCK_PARAMVALIDATOR_HEADER_
2 #define _BLOCK_PARAMVALIDATOR_HEADER_
3 
4 #ifdef _WIN32
5 #pragma warning(push)
6 #pragma warning(disable:4996 4251 4275 4800 4503)
7 #endif
8 #include <boost/filesystem.hpp>
9 #include <boost/exception/exception.hpp>
10 #include <vector>
11 #include <stdarg.h>
12 #ifdef _WIN32
13 #pragma warning(pop)
14 #endif
15 
16 #include "ParamValue.h"
17 #include "Internationalizator.h"
18 
19 namespace charliesoft
20 {
21  class Block;
22 
24  {
25  public:
26  std::string errorMsg;
27  ErrorValidator(std::string error){
28  errorMsg = error;
29  }
30  };
31 
33  {
34  protected:
35  const ParamValue* paramToValid_;
36  public:
37  ParamValidator(){ paramToValid_ = NULL; };
38  void setParamOrigin(const ParamValue* p){ paramToValid_ = p; };
39  virtual void validate(const ParamValue& value)=0;
40  };
41 
42  class ValNeeded :public ParamValidator
43  {
44  public:
45  ValNeeded(){};
46  virtual void validate(const ParamValue& value)
47  {
48  if (value.isDefaultValue())
49  {
50  if (paramToValid_ == NULL)
51  throw (ErrorValidator(_STR("ERROR_GENERIC")));
52  else
53  throw (ErrorValidator((my_format(_STR("ERROR_PARAM_NEEDED")) % _STR(paramToValid_->getName())).str()));
54  }
55  }
56  };
57 
59  {
60  public:
61  ValFileExist(){};
62  virtual void validate(const ParamValue& value)
63  {
64  if (value.isDefaultValue())
65  return;//nothing to do as value is not set!
66  if (!boost::filesystem::exists(value.toString()))
67  {
68  if (paramToValid_ == NULL)
69  throw (ErrorValidator(_STR("ERROR_GENERIC")));
70  else
71  throw (ErrorValidator((my_format(_STR("BLOCK__INPUT_IN_FILE_NOT_FOUND")) % value.toString()).str()));
72  }
73  }
74  };
75 
77  {
78  public:
79  FileIsFolder(){};
80  virtual void validate(const ParamValue& value)
81  {
82  if (value.isDefaultValue())
83  return;//nothing to do as value is not set!
84  if (!boost::filesystem::exists(value.toString()))
85  {
86  if (paramToValid_ == NULL)
87  throw (ErrorValidator(_STR("ERROR_GENERIC")));
88  else
89  throw (ErrorValidator((my_format(_STR("BLOCK__INPUT_IN_FILE_NOT_FOUND")) % value.toString()).str()));
90  }
91  if (!boost::filesystem::is_directory(value.toString()))
92  {
93  if (paramToValid_ == NULL)
94  throw (ErrorValidator(_STR("ERROR_GENERIC")));
95  else
96  throw (ErrorValidator((my_format(_STR("BLOCK__INPUT_IN_FILE_NOT_FOLDER")) % value.toString()).str()));
97  }
98  }
99  };
100 
102  {
103  bool isStrict_;
104  public:
105  ValPositiv(bool strictOnly = false){
106  isStrict_ = strictOnly;
107  }
108  virtual void validate(const ParamValue& value)
109  {
110  if (value.isDefaultValue())
111  return;//nothing to do as value is not set!
112  bool isOK = true;
113  if (isStrict_)
114  isOK &= value > 0;
115  else
116  isOK &= value >= 0;
117  if (!isOK)
118  {
119  if (paramToValid_ == NULL)
120  throw (ErrorValidator(_STR("ERROR_GENERIC")));
121  else
122  {
123  std::string errorTmp = "";
124  if (isStrict_) errorTmp = "ERROR_PARAM_ONLY_POSITIF_STRICT";
125  else errorTmp = "ERROR_PARAM_ONLY_POSITIF";
126  throw (ErrorValidator((my_format(_STR(errorTmp)) % _STR(paramToValid_->getName())).str()));
127  }
128  }
129  }
130  };
131 
133  {
134  bool isStrict_;
135  public:
136  ValNegativ(bool strictOnly = false){
137  isStrict_ = strictOnly;
138  }
139  virtual void validate(const ParamValue& value)
140  {
141  if (value.isDefaultValue())
142  return;//nothing to do as value is not set!
143  bool isOK = true;
144  if (isStrict_)
145  isOK &= value < 0;
146  else
147  isOK &= value <= 0;
148  if (!isOK)
149  {
150  if (paramToValid_ == NULL)
151  throw (ErrorValidator(_STR("ERROR_GENERIC")));
152  else
153  {
154  std::string errorTmp = "";
155  if (isStrict_) errorTmp = "ERROR_PARAM_ONLY_NEGATIF_STRICT";
156  else errorTmp = "ERROR_PARAM_ONLY_NEGATIF";
157  throw (ErrorValidator((my_format(_STR(errorTmp)) % _STR(paramToValid_->getName())).str()));
158  }
159  }
160  }
161  };
162 
164  {
165  const ParamValue& otherVal_;
166  public:
167  ValExclusif(const ParamValue& dest):
168  otherVal_(dest){}
169  virtual void validate(const ParamValue& value)
170  {
171  if (value.isDefaultValue())
172  return;//nothing to do as value is not set!
173  if (paramToValid_ == NULL)
174  throw (ErrorValidator(_STR("ERROR_GENERIC")));
175  else
176  {
177  if (otherVal_ == value)
178  throw (ErrorValidator((my_format(_STR("ERROR_PARAM_EXCLUSIF")) % _STR(paramToValid_->getName()) % _STR(otherVal_.getName())).str()));
179  }
180  }
181  };
182 
183  class ValRange :public ParamValidator
184  {
185  double inf_;
186  double sup_;
187  public:
188  ValRange(double minima, double maxima) :
189  inf_(minima), sup_(maxima){}
190  virtual void validate(const ParamValue& value)
191  {
192  if (value < inf_ || value > sup_)
193  {
194  if (paramToValid_ == NULL)
195  throw (ErrorValidator(_STR("ERROR_GENERIC")));
196  else
197  throw (ErrorValidator((my_format(_STR("ERROR_PARAM__valueBETWEEN")) %
198  _STR(paramToValid_->getName()) % value.get<double>() % inf_ % sup_).str()));
199  }
200  }
201  };
202 }
203 
204 #endif
Definition: ParamValidator.h:76
Definition: ParamValidator.h:101
Definition: ParamValidator.h:163
Definition: ParamValidator.h:58
Definition: ParamValidator.h:132
Definition: ParamValidator.h:32
Definition: ParamValidator.h:23
Definition: ParamValidator.h:42
Definition: ParamValue.h:52
Definition: ParamValidator.h:183