Salome HOME
Validators return InfoMessage instead of string as an error
[modules/shaper.git] / src / GeomValidators / GeomValidators_Positive.cpp
index d2261b357c0686c12959376d395498d26fc77579..17c38fb19035600799d5bb7808f814aa15b43347 100644 (file)
@@ -1,12 +1,20 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 // File:        GeomValidators_ValidatorPositive.cpp
 // Created:     16 Sep 2014
 // Author:      Mikhail PONIKAROV
 
 #include "GeomValidators_Positive.h"
+
+#include <Events_InfoMessage.h>
+
 #include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_AttributeInteger.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Validator.h>
 
+#include <cmath>
+
 /// Global instance for validators factory
 GeomValidators_Positive MY_POSITIVE_INSTANCE;
 
@@ -18,10 +26,46 @@ GeomValidators_Positive::GeomValidators_Positive()
   aFactory->registerValidator("GeomValidators_Positive", this);
 }
 
-bool GeomValidators_Positive::isValid(
-    const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const
+bool GeomValidators_Positive::isValid(const AttributePtr& theAttribute, 
+                                      const std::list<std::string>& theArguments,
+                                      Events_InfoMessage& theError) const
 {
-  boost::shared_ptr<ModelAPI_AttributeDouble> aDouble = 
-    boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
-  return aDouble->isInitialized() && aDouble->value() > 1.e-5;
+  double aMinValue = 1.e-5;
+  if(theArguments.size() == 1) {
+    std::list<std::string>::const_iterator anIt = theArguments.begin();
+    char *aErr;
+    double aValue = strtod((*anIt).c_str(), &aErr);
+    if(*aErr == 0) {
+      // very probably ok
+      aMinValue = aValue;
+    }
+  }
+
+  if (theAttribute->attributeType() == ModelAPI_AttributeDouble::typeId()) {
+    AttributeDoublePtr aDouble =
+        std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
+    if (!aDouble->isInitialized()) {
+      theError = "Double is not initialized.";
+      return false;
+    }
+    if (aDouble->value() <= aMinValue) {
+      theError = "Double is not positive.";
+      return false;
+    }
+  }
+
+  if (theAttribute->attributeType() == ModelAPI_AttributeInteger::typeId()) {
+    AttributeIntegerPtr aInteger =
+        std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttribute);
+    if (!aInteger->isInitialized()) {
+      theError = "Integer is not initialized.";
+      return false;
+    }
+    if (aInteger->value() <= floor(aMinValue)) {
+      theError = "Integer is not positive.";
+      return false;
+    }
+  }
+
+  return true;
 }