Salome HOME
Improvement #630: Positive validator: avoid hard constants
[modules/shaper.git] / src / GeomValidators / GeomValidators_Positive.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomValidators_ValidatorPositive.cpp
4 // Created:     16 Sep 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "GeomValidators_Positive.h"
8 #include <ModelAPI_AttributeDouble.h>
9 #include <ModelAPI_AttributeInteger.h>
10 #include <ModelAPI_Session.h>
11 #include <ModelAPI_Validator.h>
12
13 /// Global instance for validators factory
14 GeomValidators_Positive MY_POSITIVE_INSTANCE;
15
16 GeomValidators_Positive::GeomValidators_Positive()
17 {
18   // this validator is registered in the factory on this library loading
19   SessionPtr aMgr = ModelAPI_Session::get();
20   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
21   aFactory->registerValidator("GeomValidators_Positive", this);
22 }
23
24 bool GeomValidators_Positive::isValid(const AttributePtr& theAttribute, 
25                                       const std::list<std::string>& theArguments,
26                                       std::string& theError) const
27 {
28   double aMinValue = 1.e-5;
29   if(theArguments.size() == 1) {
30     std::list<std::string>::const_iterator anIt = theArguments.begin();
31     char *aErr;
32     double aValue = strtod((*anIt).c_str(), &aErr);
33     if(*aErr == 0) {
34       // very probably ok
35       aMinValue = aValue;
36     }
37   }
38
39   if (theAttribute->attributeType() == ModelAPI_AttributeDouble::typeId()) {
40     AttributeDoublePtr aDouble =
41         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
42     if (!aDouble->isInitialized()) {
43       theError = "Double is not initialized.";
44       return false;
45     }
46     if (aDouble->value() <= aMinValue) {
47       theError = "Double is not positive.";
48       return false;
49     }
50   }
51
52   if (theAttribute->attributeType() == ModelAPI_AttributeInteger::typeId()) {
53     AttributeIntegerPtr aInteger =
54         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttribute);
55     if (!aInteger->isInitialized()) {
56       theError = "Integer is not initialized.";
57       return false;
58     }
59     if (aInteger->value() <= floor(aMinValue)) {
60       theError = "Integer is not positive.";
61       return false;
62     }
63   }
64
65   return true;
66 }