#include <ModelAPI_Feature.h>
#include <Events_Error.h>
-using namespace std;
-
void Model_ValidatorsFactory::registerValidator(
- const string& theID, ModelAPI_Validator* theValidator)
+ const std::string& theID, ModelAPI_Validator* theValidator)
{
if (myIDs.find(theID) != myIDs.end()) {
- Events_Error::send(string("Validator ") + theID + " is already registered");
+ Events_Error::send(std::string("Validator ") + theID + " is already registered");
} else {
myIDs[theID] = theValidator;
}
}
-void Model_ValidatorsFactory::assignValidator(const string& theID, const string& theFeatureID)
+void Model_ValidatorsFactory::assignValidator(
+ const std::string& theID, const std::string& theFeatureID)
{
- bool isError = false;
- map<string, ModelAPI_Validator*>::iterator aVal = myIDs.find(theID);
- if (aVal == myIDs.end()) {
- Events_Error::send(
- string("Validator ") + theID + " for feature " + theFeatureID + " was not registered");
- isError = true;
- }
- if (myFeatures.find(theFeatureID) != myFeatures.end()) {
- // TODO: it is called many times now because of not-optimized XML reader
- //Events_Error::send(
- // string("Validator for feature ") + theFeatureID + " is already registered");
- isError = true;
+ if (myFeatures.find(theFeatureID) == myFeatures.end()) {
+ myFeatures[theFeatureID] = std::set<std::string>();
}
- if (!isError)
- myFeatures[theFeatureID] = aVal->second;
+ myFeatures[theFeatureID].insert(theID);
}
-void Model_ValidatorsFactory::assignValidator(const string& theID,
- const string& theFeatureID, const string& theAttrID, const list<string>& theArguments)
+void Model_ValidatorsFactory::assignValidator(const std::string& theID,
+ const std::string& theFeatureID, const std::string& theAttrID,
+ const std::list<std::string>& theArguments)
{
- bool isError = false;
- map<string, ModelAPI_Validator*>::iterator aVal = myIDs.find(theID);
- if (aVal == myIDs.end()) {
- Events_Error::send(
- string("Validator ") + theID + " for feature " + theFeatureID + " was not registered");
- isError = true;
- }
// create feature-structures if not exist
- map<string, map<string, pair<ModelAPI_Validator*, list<string> > > >::iterator
- aFeature = myAttrs.find(theFeatureID);
+ std::map<std::string, std::map<std::string, AttrValidators> >::iterator aFeature =
+ myAttrs.find(theFeatureID);
if (aFeature == myAttrs.end()) {
- myAttrs[theFeatureID] = map<string, pair<ModelAPI_Validator*, list<string> > >();
+ myAttrs[theFeatureID] = std::map<std::string, AttrValidators>();
aFeature = myAttrs.find(theFeatureID);
}
// add attr-structure if not exist, or generate error if already exist
- map<string, pair<ModelAPI_Validator*, list<string> > >::iterator
- anAttr = aFeature->second.find(theAttrID);
+ std::map<std::string, AttrValidators>::iterator anAttr = aFeature->second.find(theAttrID);
if (anAttr == aFeature->second.end()) {
- if (!isError) {
- aFeature->second[theAttrID] =
- pair<ModelAPI_Validator*, list<string> >(aVal->second, theArguments);
- }
- } else {
- // TODO: it is called many times now because of not-optimized XML reader
- //Events_Error::send(
- // string("Validator ") + theID + " for feature " + theFeatureID +
- // "attribute " + theAttrID + " is already registered");
- isError = true;
+ aFeature->second[theAttrID] = AttrValidators();
}
+ aFeature->second[theAttrID].insert(
+ std::pair<std::string, std::list<std::string> >(theID, theArguments));
}
-const ModelAPI_Validator* Model_ValidatorsFactory::validator(const string& theFeatureID) const
+void Model_ValidatorsFactory::validators(
+ const std::string& theFeatureID, std::list<ModelAPI_Validator*>& theResult ) const
{
- map<string, ModelAPI_Validator*>::const_iterator aFeature = myFeatures.find(theFeatureID);
- if (aFeature != myFeatures.cend())
- return aFeature->second;
- return NULL; // not found
+ std::map<std::string, std::set<std::string> >::const_iterator aFeature =
+ myFeatures.find(theFeatureID);
+ if (aFeature != myFeatures.cend()) {
+ std::set<std::string>::const_iterator aValIter = aFeature->second.cbegin();
+ for(; aValIter != aFeature->second.cend(); aValIter++) {
+ std::map<std::string, ModelAPI_Validator*>::const_iterator aFound = myIDs.find(*aValIter);
+ if (aFound == myIDs.end()) {
+ Events_Error::send(std::string("Validator ") + *aValIter + " was not registered");
+ } else {
+ theResult.push_back(aFound->second);
+ }
+ }
+ }
}
-const ModelAPI_Validator* Model_ValidatorsFactory::validator(
- const std::string& theFeatureID, const std::string& theAttrID) const
+void Model_ValidatorsFactory::validators(const std::string& theFeatureID,
+ const std::string& theAttrID, std::list<ModelAPI_Validator*>& theValidators,
+ std::list<std::list<std::string> >& theArguments) const
{
- map<string, map<string, pair<ModelAPI_Validator*, list<string> > > >::const_iterator
- aFeature = myAttrs.find(theFeatureID);
- if (aFeature == myAttrs.cend()) return NULL; // feature is not found
- map<string, pair<ModelAPI_Validator*, list<string> > >::const_iterator
- anAttr = aFeature->second.find(theAttrID);
- if (anAttr == aFeature->second.cend()) return NULL; // attribute is not found
- return anAttr->second.first;
+ std::map<std::string, std::map<std::string, AttrValidators> >::const_iterator aFeature =
+ myAttrs.find(theFeatureID);
+ if (aFeature != myAttrs.cend()) {
+ std::map<std::string, AttrValidators>::const_iterator anAttr =
+ aFeature->second.find(theAttrID);
+ if (anAttr != aFeature->second.end()) {
+ AttrValidators::const_iterator aValIter = anAttr->second.cbegin();
+ for(; aValIter != anAttr->second.cend(); aValIter++) {
+ std::map<std::string, ModelAPI_Validator*>::const_iterator aFound =
+ myIDs.find(aValIter->first);
+ if (aFound == myIDs.end()) {
+ Events_Error::send(std::string("Validator ") + aValIter->first + " was not registered");
+ } else {
+ theValidators.push_back(aFound->second);
+ theArguments.push_back(aValIter->second);
+ }
+ }
+ }
+ }
}
-/*bool Model_ValidatorsFactory::validate(
- const boost::shared_ptr<ModelAPI_Feature>& theFeature, const string& theAttrID ) const
-{
- map<string, map<string, pair<ModelAPI_Validator*, list<string> > > >::const_iterator
- aFeature = myAttrs.find(theFeature->getKind());
- if (aFeature == myAttrs.cend()) return true; // feature is not found
- map<string, pair<ModelAPI_Validator*, list<string> > >::const_iterator
- anAttr = aFeature->second.find(theAttrID);
- if (anAttr == aFeature->second.cend()) return true; // attribute is not found
- return anAttr->second.first->validate(theFeature, theAttrID, anAttr->second.second);
-}*/
-
Model_ValidatorsFactory::Model_ValidatorsFactory() : ModelAPI_ValidatorsFactory()
{
}
#include <Model.h>
#include <ModelAPI_Validator.h>
#include <map>
+#include <set>
/**\class Model_ValidatorsFactory
* \ingroup DataModel
*/
class Model_ValidatorsFactory: public ModelAPI_ValidatorsFactory
{
+private:
std::map<std::string, ModelAPI_Validator*> myIDs; ///< map from ID to registered validator
- std::map<std::string, ModelAPI_Validator*> myFeatures; ///< validators by feature ID
- std::map<std::string, std::map<std::string, std::pair<ModelAPI_Validator*,
- std::list<std::string> > > > myAttrs; ///< validators and arguments by feature and attribute IDs
+ /// validators IDs by feature ID
+ std::map<std::string, std::set<std::string> > myFeatures;
+ /// set of pairs: validators IDs, list of arguments
+ typedef std::set<std::pair<std::string, std::list<std::string> > > AttrValidators;
+ /// validators IDs and arguments by feature and attribute IDs
+ std::map<std::string, std::map<std::string, AttrValidators> > myAttrs;
public:
/// Registers the instance of the validator by the ID
MODEL_EXPORT virtual void registerValidator(
const std::list<std::string>& theArguments);
/// Provides a validator for the feature, returns NULL if no validator
- MODEL_EXPORT virtual const ModelAPI_Validator* validator(const std::string& theFeatureID) const;
+ MODEL_EXPORT virtual void validators(const std::string& theFeatureID,
+ std::list<ModelAPI_Validator*>& theResult) const;
/// Provides a validator for the attribute, returns NULL if no validator
- MODEL_EXPORT virtual const ModelAPI_Validator* validator(
- const std::string& theFeatureID, const std::string& theAttrID) const;
+ MODEL_EXPORT virtual void validators(
+ const std::string& theFeatureID, const std::string& theAttrID,
+ std::list<ModelAPI_Validator*>& theValidators,
+ std::list<std::list<std::string> >& theArguments) const;
/// Returns the result of "validate" method for attribute of validator.
/// If validator is not exists, returns true: everything is valid by default.