Salome HOME
Add error descriptions for some feture and attribute validators
[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   if (theAttribute->attributeType() == ModelAPI_AttributeDouble::typeId()) {
29     AttributeDoublePtr aDouble =
30         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
31     if (!aDouble->isInitialized()) {
32       theError = "Double is not initialized.";
33       return false;
34     }
35     if (aDouble->value() <= 1.e-5) {
36       theError = "Double is not positive.";
37       return false;
38     }
39   }
40
41   if (theAttribute->attributeType() == ModelAPI_AttributeInteger::typeId()) {
42     AttributeIntegerPtr aInteger =
43         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttribute);
44     if (!aInteger->isInitialized()) {
45       theError = "Integer is not initialized.";
46       return false;
47     }
48     if (aInteger->value() <= 0) {
49       theError = "Integer is not positive.";
50       return false;
51     }
52   }
53
54   return true;
55 }