Salome HOME
b127ffe033a688edf64981f0c222287d5ea8cb71
[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 #include <cmath>
14
15 /// Global instance for validators factory
16 GeomValidators_Positive MY_POSITIVE_INSTANCE;
17
18 GeomValidators_Positive::GeomValidators_Positive()
19 {
20   // this validator is registered in the factory on this library loading
21   SessionPtr aMgr = ModelAPI_Session::get();
22   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
23   aFactory->registerValidator("GeomValidators_Positive", this);
24 }
25
26 bool GeomValidators_Positive::isValid(const AttributePtr& theAttribute, 
27                                       const std::list<std::string>& theArguments,
28                                       std::string& theError) const
29 {
30   double aMinValue = 1.e-5;
31   if(theArguments.size() == 1) {
32     std::list<std::string>::const_iterator anIt = theArguments.begin();
33     char *aErr;
34     double aValue = strtod((*anIt).c_str(), &aErr);
35     if(*aErr == 0) {
36       // very probably ok
37       aMinValue = aValue;
38     }
39   }
40
41   if (theAttribute->attributeType() == ModelAPI_AttributeDouble::typeId()) {
42     AttributeDoublePtr aDouble =
43         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
44     if (!aDouble->isInitialized()) {
45       theError = "Double is not initialized.";
46       return false;
47     }
48     if (aDouble->value() <= aMinValue) {
49       theError = "Double is not positive.";
50       return false;
51     }
52   }
53
54   if (theAttribute->attributeType() == ModelAPI_AttributeInteger::typeId()) {
55     AttributeIntegerPtr aInteger =
56         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttribute);
57     if (!aInteger->isInitialized()) {
58       theError = "Integer is not initialized.";
59       return false;
60     }
61     if (aInteger->value() <= floor(aMinValue)) {
62       theError = "Integer is not positive.";
63       return false;
64     }
65   }
66
67   return true;
68 }