32 #ifndef LSST_PEX_POLICY_DICTIONARY_H
33 #define LSST_PEX_POLICY_DICTIONARY_H
37 #include <boost/regex.hpp>
55 class ValidationError :
public lsst::pex::exceptions::LogicError {
93 WRONG_OCCURRENCE_COUNT = 30,
96 VALUE_DISALLOWED = 32,
102 VALUE_OUT_OF_RANGE = 64,
114 BAD_DEFINITION = 256,
123 static const std::string& getErrorMessageFor(ErrorType err) {
124 if (_errmsgs.size() == 0) _loadMessages();
125 MsgLookup::iterator it = _errmsgs.find(err);
126 if (it != _errmsgs.end())
131 static std::string result;
133 std::ostringstream os;
135 for (std::map<int,std::string>::const_iterator j = _errmsgs.begin();
136 j != _errmsgs.end(); ++j) {
137 if (j->first !=
OK && (err & j->first) == j->first) {
138 os << (first ?
"" :
"; ") << j->second;
147 static const std::string EMPTY;
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"),
160 virtual char const *getType(
void)
const throw();
165 ValidationError(const ValidationError& that)
166 : lsst::pex::exceptions::LogicError(that), _errors(that._errors)
169 ValidationError& operator=(
const ValidationError& that) {
170 LogicError::operator=(that);
171 _errors = that._errors;
185 virtual ~ValidationError() throw();
190 int getParamCount()
const {
return _errors.size(); }
196 void paramNames(std::list<std::string>& names)
const {
197 ParamLookup::const_iterator it;
198 for(it = _errors.begin(); it != _errors.end(); it++)
199 names.push_back(it->first);
206 std::vector<std::string> getParamNames()
const;
211 int getErrors(
const std::string&
name)
const {
212 ParamLookup::const_iterator it = _errors.find(name);
213 if (it != _errors.end())
222 void addError(
const std::string& name, ErrorType e) {
230 int getErrors()
const {
232 ParamLookup::const_iterator it;
233 for(it = _errors.begin(); it != _errors.end(); it++)
243 std::string describe(std::string prefix =
"")
const;
248 virtual char const* what(
void)
const throw();
251 typedef std::map<
int, std::
string> MsgLookup;
252 typedef std::map<std::
string,
int> ParamLookup;
254 static MsgLookup _errmsgs;
256 static
void _loadMessages();
265 class Definition : public lsst::daf::base::Citizen {
272 Definition(
const std::string& paramName =
"")
273 : lsst::daf::base::Citizen(typeid(*this)), _type(Policy::UNDETERMINED),
274 _name(paramName), _policy(), _wildcard(false)
276 _policy.reset(
new Policy());
284 Definition(
const std::string& paramName,
const Policy::Ptr& defn)
285 : lsst::daf::base::Citizen(typeid(*this)), _type(Policy::UNDETERMINED),
286 _name(paramName), _policy(defn), _wildcard(false)
294 : lsst::daf::base::Citizen(typeid(*this)), _type(Policy::UNDETERMINED),
295 _name(), _policy(defn), _wildcard(false)
301 Definition(
const Definition& that)
302 : lsst::daf::base::Citizen(typeid(*this)), _type(Policy::UNDETERMINED),
303 _name(that.
_name), _policy(that._policy), _wildcard(false)
309 Definition& operator=(
const Definition& that) {
312 _policy = that._policy;
313 _prefix = that._prefix;
314 _wildcard = that._wildcard;
322 Definition& operator=(
const Policy::Ptr& defdata) {
327 virtual ~Definition();
332 const std::string& getName()
const {
return _name; }
339 const std::string getPrefix()
const {
return _prefix; }
340 void setPrefix(
const std::string& prefix) { _prefix = prefix; }
348 const bool isChildDefinition()
const {
return _wildcard; }
349 void setChildDefinition(
bool wildcard) { _wildcard = wildcard; }
350 const bool isWildcard()
const {
return _wildcard; }
351 void setWildcard(
bool wildcard) { _wildcard = wildcard; }
358 void setName(
const std::string& newname) {
_name = newname; }
363 const Policy::Ptr& getData()
const {
return _policy; }
385 std::string getTypeName()
const {
392 std::string getDefault()
const {
393 return _policy->str(
"default");
400 const std::string getDescription()
const;
406 const int getMaxOccurs()
const;
412 const int getMinOccurs()
const;
420 void setDefaultIn(Policy& policy, ValidationError* errs=0)
const {
421 setDefaultIn(policy,
_name, errs);
429 void setDefaultIn(Policy& policy,
const std::string& withName,
430 ValidationError* errs=0)
const;
435 template <
typename T>
void setDefaultIn(Policy& policy,
436 const std::string& withName,
437 ValidationError* errs=0)
const;
452 void validate(
const Policy& policy,
const std::string& name,
453 ValidationError *errs=0)
const;
467 void validate(
const Policy& policy, ValidationError *errs=0)
const {
468 validate(policy,
_name, errs);
495 void validate(
const std::string& name,
bool value,
int curcount=-1,
496 ValidationError *errs=0)
const;
497 void validate(
const std::string& name,
int value,
int curcount=-1,
498 ValidationError *errs=0)
const;
499 void validate(
const std::string& name,
double value,
int curcount=-1,
500 ValidationError *errs=0)
const;
501 void validate(
const std::string& name, std::string value,
int curcount=-1,
502 ValidationError *errs=0)
const;
503 void validate(
const std::string& name,
const Policy& value,
int curcount=-1,
504 ValidationError *errs=0)
const;
526 ValidationError *errs=0)
const;
528 ValidationError *errs=0)
const;
530 ValidationError *errs=0)
const;
531 void validate(
const std::string& name,
533 ValidationError *errs=0)
const;
534 void validate(
const std::string& name,
536 ValidationError *errs=0)
const;
558 template <
typename T>
void validateBasic
559 (
const std::string& name,
const Policy& policy,
560 ValidationError *errs=0)
const;
572 template <
typename T>
void validateBasic
573 (
const std::string& name,
const T& value,
int curcount=-1,
574 ValidationError *errs=0)
const;
575 template <
typename T>
void validateBasic
576 (
const std::string& name,
const std::vector<T>& value,
577 ValidationError *errs=0)
const;
589 ValidationError *errs)
const;
591 void validateRecurse(
const std::string& name,
const Policy& value,
592 ValidationError *errs)
const;
611 void validateCount(
const std::string& name,
int count,
612 ValidationError *errs)
const;
614 static const std::string EMPTY;
624 inline std::ostream&
operator<<(std::ostream& os,
const Definition& d) {
625 d.getData()->print(os, d.getName());
719 class Dictionary :
public Policy {
723 static const char *KW_DICT;
724 static const char *KW_DICT_FILE;
725 static const char *KW_TYPE;
726 static const char *KW_DESCRIPTION;
727 static const char *KW_DEFAULT;
728 static const char *KW_DEFINITIONS;
729 static const char *KW_CHILD_DEF;
730 static const char *KW_ALLOWED;
731 static const char *KW_MIN_OCCUR;
732 static const char *KW_MAX_OCCUR;
733 static const char *KW_MIN;
734 static const char *KW_MAX;
735 static const char *KW_VALUE;
741 Dictionary() : Policy() { }
749 Dictionary(
const Policy& pol)
750 : Policy( (pol.isPolicy(
"dictionary")) ? *(pol.getPolicy(
"dictionary"))
759 Dictionary(
const Dictionary& dict) : Policy(dict) {
767 explicit Dictionary(
const char *filePath);
768 explicit Dictionary(
const std::string& filePath);
769 explicit Dictionary(
const PolicyFile& filePath);
777 return getPolicy(
"definitions");
780 return getPolicy(
"definitions");
794 int definedNames(std::list<std::string>& names,
bool append=
false)
const {
795 return getDefinitions()->names(names,
true, append);
801 StringArray definedNames()
const {
802 return getDefinitions()->names(
true);
809 Definition getDef(
const std::string& name) {
810 Definition *def = makeDef(name);
811 Definition out(*def);
824 Definition* makeDef(
const std::string& name)
const;
831 bool hasSubDictionary(
const std::string& name)
const {
832 std::string key = std::string(
"definitions.") + name +
"." + KW_DICT;
845 DictPtr getSubDictionary(
const std::string& name)
const;
865 void validate(
const Policy& pol, ValidationError *errs=0)
const;
883 int loadPolicyFiles(
bool strict=
true) {
884 return loadPolicyFiles(boost::filesystem::path(), strict);
895 virtual int loadPolicyFiles(
const boost::filesystem::path& repository,
903 const std::string getPrefix()
const {
return _prefix; }
904 void setPrefix(
const std::string& prefix) { _prefix = prefix; }
909 static const boost::regex FIELDSEP_RE;
915 template <
typename T>
916 void Definition::validateBasic(
const std::string& name,
const Policy& policy,
917 ValidationError *errs)
const
919 validateBasic(name, policy.getValueArray<T>(name), errs);
922 template <
typename T>
923 void Definition::validateBasic(
const std::string& name,
const std::vector<T>& value,
924 ValidationError *errs)
const
927 ValidationError *use = &ve;
928 if (errs != 0) use = errs;
930 validateCount(name, value.size(), use);
932 for (
typename std::vector<T>::const_iterator i = value.begin();
935 validateBasic<T>(name, *i, -1, use);
937 if (errs == 0 && ve.getParamCount() > 0)
throw ve;
940 template <
typename T>
941 void Definition::setDefaultIn(Policy& policy,
const std::string& withName,
942 ValidationError *errs)
const
945 ValidationError *use = (errs == 0 ? &ve : errs);
947 if (_policy->exists(
"default")) {
948 const std::vector<T> defs = _policy->getValueArray<T>(
"default");
949 validateBasic(withName, defs, use);
951 policy.remove(withName);
952 for (
typename std::vector<T>::const_iterator i = defs.begin();
955 policy.addValue<T>(withName, *i);
959 if (errs == 0 && ve.getParamCount() > 0)
throw ve;
964 #endif // LSST_PEX_POLICY_POLICY_H
std::vector< bool > BoolArray
table::Key< std::string > name
for(FootprintList::const_iterator ptr=feet->begin(), end=feet->end();ptr!=end;++ptr)
std::vector< std::string > StringArray
definition of Policy-specific exceptions classes
std::string const & _name
boost::shared_ptr< Policy > Ptr
std::ostream & operator<<(std::ostream &os, const Policy &p)
std::vector< int > IntArray
static const char *const typeName[]
std::vector< double > DoubleArray
std::vector< ConstPtr > ConstPolicyPtrArray
boost::shared_ptr< const Policy > ConstPtr
def setPrefix
Set a prefix based on the EUPS_PATH, the product name, and a versionString from cvs or svn...