32 #ifndef LSST_PEX_POLICY_DICTIONARY_H 33 #define LSST_PEX_POLICY_DICTIONARY_H 37 #include <boost/regex.hpp> 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())
136 j != _errmsgs.end(); ++j) {
137 if (j->first != OK && (err & j->first) == j->first) {
138 os << (
first ?
"" :
"; ") << j->second;
156 ValidationError(
char const* ex_file,
int ex_line,
char const* ex_func)
158 "Policy has unknown validation errors"),
163 virtual char const *getType(
void)
const throw();
168 ValidationError(
const ValidationError& that)
172 ValidationError& operator=(
const ValidationError& that) {
173 LogicError::operator=(that);
174 _errors = that._errors;
188 virtual ~ValidationError() throw();
193 int getParamCount()
const {
return _errors.size(); }
200 ParamLookup::const_iterator it;
201 for(it = _errors.begin(); it != _errors.end(); it++)
215 ParamLookup::const_iterator it = _errors.find(name);
216 if (it != _errors.end())
225 void addError(
const std::string& name, ErrorType e) {
233 int getErrors()
const {
235 ParamLookup::const_iterator it;
236 for(it = _errors.begin(); it != _errors.end(); it++)
251 virtual char const* what(
void)
const throw();
257 static MsgLookup _errmsgs;
259 static void _loadMessages();
276 :
lsst::daf::
base::Citizen(typeid(*this)), _type(Policy::UNDETERMINED),
277 _name(paramName), _policy(), _wildcard(false)
279 _policy.reset(
new Policy());
288 :
lsst::daf::
base::Citizen(typeid(*this)), _type(Policy::UNDETERMINED),
289 _name(paramName), _policy(defn), _wildcard(false)
297 :
lsst::daf::
base::Citizen(typeid(*this)), _type(Policy::UNDETERMINED),
298 _name(), _policy(defn), _wildcard(false)
304 Definition(
const Definition& that)
305 :
lsst::daf::
base::Citizen(typeid(*this)), _type(Policy::UNDETERMINED),
306 _name(that._name), _policy(that._policy), _wildcard(false)
312 Definition& operator=(
const Definition& that) {
315 _policy = that._policy;
316 _prefix = that._prefix;
317 _wildcard = that._wildcard;
325 Definition& operator=(
const Policy::Ptr& defdata) {
330 virtual ~Definition();
335 const std::string& getName()
const {
return _name; }
342 const std::string getPrefix()
const {
return _prefix; }
351 const bool isChildDefinition()
const {
return _wildcard; }
352 void setChildDefinition(
bool wildcard) { _wildcard = wildcard; }
353 const bool isWildcard()
const {
return _wildcard; }
354 void setWildcard(
bool wildcard) { _wildcard = wildcard; }
361 void setName(
const std::string& newname) { _name = newname; }
366 const Policy::Ptr& getData()
const {
return _policy; }
396 return _policy->str(
"default");
409 const int getMaxOccurs()
const;
415 const int getMinOccurs()
const;
423 void setDefaultIn(Policy& policy, ValidationError* errs=0)
const {
424 setDefaultIn(policy, _name, errs);
432 void setDefaultIn(Policy& policy,
const std::string& withName,
433 ValidationError* errs=0)
const;
438 template <
typename T>
void setDefaultIn(Policy& policy,
440 ValidationError* errs=0)
const;
456 ValidationError *errs=0)
const;
470 void validate(
const Policy& policy, ValidationError *errs=0)
const {
499 ValidationError *errs=0)
const;
501 ValidationError *errs=0)
const;
503 ValidationError *errs=0)
const;
505 ValidationError *errs=0)
const;
507 ValidationError *errs=0)
const;
529 ValidationError *errs=0)
const;
531 ValidationError *errs=0)
const;
533 ValidationError *errs=0)
const;
536 ValidationError *errs=0)
const;
539 ValidationError *errs=0)
const;
561 template <
typename T>
void validateBasic
563 ValidationError *errs=0)
const;
575 template <
typename T>
void validateBasic
576 (
const std::string& name,
const T& value,
int curcount=-1,
577 ValidationError *errs=0)
const;
578 template <
typename T>
void validateBasic
580 ValidationError *errs=0)
const;
592 ValidationError *errs)
const;
594 void validateRecurse(
const std::string& name,
const Policy& value,
595 ValidationError *errs)
const;
614 void validateCount(
const std::string& name,
int count,
615 ValidationError *errs)
const;
628 d.getData()->print(os, d.getName());
722 class Dictionary :
public Policy {
726 static const char *KW_DICT;
727 static const char *KW_DICT_FILE;
728 static const char *KW_TYPE;
729 static const char *KW_DESCRIPTION;
730 static const char *KW_DEFAULT;
731 static const char *KW_DEFINITIONS;
732 static const char *KW_CHILD_DEF;
733 static const char *KW_ALLOWED;
734 static const char *KW_MIN_OCCUR;
735 static const char *KW_MAX_OCCUR;
736 static const char *KW_MIN;
737 static const char *KW_MAX;
738 static const char *KW_VALUE;
744 Dictionary() : Policy() { }
752 Dictionary(
const Policy& pol)
753 : Policy( (pol.isPolicy(
"dictionary")) ? *(pol.getPolicy(
"dictionary"))
762 Dictionary(
const Dictionary& dict) : Policy(dict) {
770 explicit Dictionary(
const char *filePath);
772 explicit Dictionary(
const PolicyFile& filePath);
780 return getPolicy(
"definitions");
783 return getPolicy(
"definitions");
798 return getDefinitions()->names(names,
true,
append);
804 StringArray definedNames()
const {
805 return getDefinitions()->names(
true);
813 Definition *def = makeDef(name);
814 Definition out(*def);
827 Definition* makeDef(
const std::string& name)
const;
834 bool hasSubDictionary(
const std::string& name)
const {
848 DictPtr getSubDictionary(
const std::string& name)
const;
868 void validate(
const Policy& pol, ValidationError *errs=0)
const;
886 int loadPolicyFiles(
bool strict=
true) {
887 return loadPolicyFiles(boost::filesystem::path(), strict);
898 virtual int loadPolicyFiles(
const boost::filesystem::path& repository,
906 const std::string getPrefix()
const {
return _prefix; }
912 static const boost::regex FIELDSEP_RE;
918 template <
typename T>
919 void Definition::validateBasic(
const std::string& name,
const Policy& policy,
920 ValidationError *errs)
const 922 validateBasic(name, policy.getValueArray<T>(name), errs);
925 template <
typename T>
927 ValidationError *errs)
const 930 ValidationError *use = &ve;
931 if (errs != 0) use = errs;
933 validateCount(name, value.
size(), use);
938 validateBasic<T>(name, *i, -1, use);
940 if (errs == 0 && ve.getParamCount() > 0)
throw ve;
943 template <
typename T>
944 void Definition::setDefaultIn(Policy& policy,
const std::string& withName,
945 ValidationError *errs)
const 948 ValidationError *use = (errs == 0 ? &ve : errs);
950 if (_policy->exists(
"default")) {
952 validateBasic(withName, defs, use);
953 if (use->getErrors(withName) == ValidationError::OK) {
954 policy.remove(withName);
958 policy.addValue<T>(withName, *i);
962 if (errs == 0 && ve.getParamCount() > 0)
throw ve;
967 #endif // LSST_PEX_POLICY_POLICY_H definition of Policy-specific exceptions classes
std::vector< bool > BoolArray
std::vector< std::string > StringArray
std::vector< double > DoubleArray
Provides consistent interface for LSST exceptions.
#define LSST_EXCEPT_HERE
For internal use; gathers the file, line, and function for a tracepoint.
std::shared_ptr< FrameSet > append(FrameSet const &first, FrameSet const &second)
Construct a FrameSet that performs two transformations in series.
std::ostream & operator<<(std::ostream &os, const Policy &p)
std::vector< ConstPtr > ConstPolicyPtrArray
std::shared_ptr< const Policy > ConstPtr
A base class for image defects.
std::shared_ptr< Policy > Ptr
Reports errors in the logical structure of the program.
static const char *const typeName[]
c-string forms for the supported value types.
std::vector< int > IntArray
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
ValueType
an enumeration for the supported policy types