32 #ifndef LSST_PEX_POLICY_DICTIONARY_H
33 #define LSST_PEX_POLICY_DICTIONARY_H
37 #include <boost/regex.hpp>
92 WRONG_OCCURRENCE_COUNT = 30,
95 VALUE_DISALLOWED = 32,
101 VALUE_OUT_OF_RANGE = 64,
113 BAD_DEFINITION = 256,
122 static const std::string& getErrorMessageFor(ErrorType err) {
123 if (_errmsgs.size() == 0) _loadMessages();
124 MsgLookup::iterator it = _errmsgs.find(err);
125 if (it != _errmsgs.end())
135 if (j->first != OK && (err & j->first) == j->first) {
136 os << (
first ?
"" :
"; ") << j->second;
147 ValidationError(
std::string const& message) :
lsst::pex::exceptions::LogicError(message), _errors() {}
153 ValidationError(
char const* ex_file,
int ex_line,
char const* ex_func)
154 :
lsst::pex::exceptions::LogicError(ex_file, ex_line, ex_func,
155 "Policy has unknown validation errors"),
159 virtual char const* getType(
void)
const throw();
164 ValidationError(
const ValidationError& that)
165 :
lsst::pex::exceptions::LogicError(that), _errors(that._errors) {}
167 ValidationError& operator=(
const ValidationError& that) {
168 LogicError::operator=(that);
169 _errors = that._errors;
183 virtual ~ValidationError() throw();
188 int getParamCount()
const {
return _errors.size(); }
195 ParamLookup::const_iterator it;
196 for (it = _errors.begin(); it != _errors.end(); it++) names.
push_back(it->first);
209 ParamLookup::const_iterator it = _errors.find(
name);
210 if (it != _errors.end())
225 int getErrors()
const {
227 ParamLookup::const_iterator it;
228 for (it = _errors.begin(); it != _errors.end(); it++) out |= it->second;
242 virtual char const* what(
void)
const throw();
248 static MsgLookup _errmsgs;
250 static void _loadMessages();
266 : _type(Policy::UNDETERMINED), _name(paramName), _policy(), _wildcard(false) {
267 _policy.reset(
new Policy());
276 : _type(Policy::UNDETERMINED), _name(paramName), _policy(defn), _wildcard(false) {}
283 : _type(Policy::UNDETERMINED), _name(), _policy(defn), _wildcard(false) {}
288 Definition(
const Definition& that)
289 : _type(Policy::UNDETERMINED), _name(that._name), _policy(that._policy), _wildcard(false) {}
294 Definition& operator=(
const Definition& that) {
297 _policy = that._policy;
298 _prefix = that._prefix;
299 _wildcard = that._wildcard;
307 Definition& operator=(
const Policy::Ptr& defdata) {
312 virtual ~Definition();
317 const std::string& getName()
const {
return _name; }
324 const std::string getPrefix()
const {
return _prefix; }
333 const bool isChildDefinition()
const {
return _wildcard; }
334 void setChildDefinition(
bool wildcard) { _wildcard = wildcard; }
335 const bool isWildcard()
const {
return _wildcard; }
336 void setWildcard(
bool wildcard) { _wildcard = wildcard; }
343 void setName(
const std::string& newname) { _name = newname; }
348 const Policy::Ptr& getData()
const {
return _policy; }
375 std::string getDefault()
const {
return _policy->str(
"default"); }
387 const int getMaxOccurs()
const;
393 const int getMinOccurs()
const;
401 void setDefaultIn(Policy& policy, ValidationError* errs = 0)
const { setDefaultIn(policy, _name, errs); }
408 void setDefaultIn(Policy& policy,
const std::string& withName, ValidationError* errs = 0)
const;
413 template <
typename T>
414 void setDefaultIn(Policy& policy,
const std::string& withName, ValidationError* errs = 0)
const;
443 void validate(
const Policy& policy, ValidationError* errs = 0)
const {
validate(policy, _name, errs); }
473 ValidationError* errs = 0)
const;
475 ValidationError* errs = 0)
const;
501 ValidationError* errs = 0)
const;
523 template <
typename T>
524 void validateBasic(
const std::string&
name,
const Policy& policy, ValidationError* errs = 0)
const;
536 template <
typename T>
537 void validateBasic(
const std::string&
name,
const T& value,
int curcount = -1,
538 ValidationError* errs = 0)
const;
539 template <
typename T>
552 ValidationError* errs)
const;
554 void validateRecurse(
const std::string&
name,
const Policy& value, ValidationError* errs)
const;
573 void validateCount(
const std::string&
name,
int count, ValidationError* errs)
const;
586 d.getData()->print(
os, d.getName());
680 class Dictionary :
public Policy {
683 static const char* KW_DICT;
684 static const char* KW_DICT_FILE;
685 static const char* KW_TYPE;
686 static const char* KW_DESCRIPTION;
687 static const char* KW_DEFAULT;
688 static const char* KW_DEFINITIONS;
689 static const char* KW_CHILD_DEF;
690 static const char* KW_ALLOWED;
691 static const char* KW_MIN_OCCUR;
692 static const char* KW_MAX_OCCUR;
693 static const char* KW_MIN;
694 static const char* KW_MAX;
695 static const char* KW_VALUE;
701 Dictionary() : Policy() {}
709 Dictionary(
const Policy& pol)
710 : Policy((pol.isPolicy(
"dictionary")) ? *(pol.getPolicy(
"dictionary")) : pol) {
717 Dictionary(
const Dictionary& dict) : Policy(dict) { check(); }
723 explicit Dictionary(
const char* filePath);
725 explicit Dictionary(
const PolicyFile& filePath);
732 Policy::ConstPtr getDefinitions()
const {
return getPolicy(
"definitions"); }
733 Policy::Ptr getDefinitions() {
return getPolicy(
"definitions"); }
747 return getDefinitions()->names(names,
true,
append);
753 StringArray definedNames()
const {
return getDefinitions()->names(
true); }
760 Definition* def = makeDef(
name);
761 Definition out(*def);
815 void validate(
const Policy& pol, ValidationError* errs = 0)
const;
833 int loadPolicyFiles(
bool strict =
true) {
return loadPolicyFiles(boost::filesystem::path(), strict); }
843 virtual int loadPolicyFiles(
const boost::filesystem::path& repository,
bool strict =
true);
850 const std::string getPrefix()
const {
return _prefix; }
855 static const boost::regex FIELDSEP_RE;
861 template <
typename T>
862 void Definition::validateBasic(
const std::string&
name,
const Policy& policy, ValidationError* errs)
const {
863 validateBasic(
name, policy.getValueArray<T>(
name), errs);
866 template <
typename T>
868 ValidationError* errs)
const {
870 ValidationError* use = &ve;
871 if (errs != 0) use = errs;
873 validateCount(
name, value.
size(), use);
876 validateBasic<T>(
name, *i, -1, use);
878 if (errs == 0 && ve.getParamCount() > 0)
throw ve;
881 template <
typename T>
882 void Definition::setDefaultIn(Policy& policy,
const std::string& withName, ValidationError* errs)
const {
884 ValidationError* use = (errs == 0 ? &ve : errs);
886 if (_policy->exists(
"default")) {
888 validateBasic(withName, defs, use);
889 if (use->getErrors(withName) == ValidationError::OK) {
890 policy.remove(withName);
892 policy.addValue<T>(withName, *i);
896 if (errs == 0 && ve.getParamCount() > 0)
throw ve;
903 #endif // LSST_PEX_POLICY_POLICY_H