25 #ifndef LSST_PEX_POLICY_POLICY_H
26 #define LSST_PEX_POLICY_POLICY_H
34 #include <boost/filesystem/path.hpp>
35 #include <boost/filesystem/operations.hpp>
37 #include "lsst/daf/base/Citizen.h"
38 #include "lsst/daf/base/Persistable.h"
39 #include "lsst/daf/base/PropertySet.h"
50 class ValidationError;
52 #define POL_GETSCALAR(name, type, vtype) \
54 return _data->get<type>(name); \
55 } catch (lsst::pex::exceptions::NotFoundError&) { \
56 throw LSST_EXCEPT(NameNotFound, name); \
57 } catch (lsst::pex::exceptions::TypeError&) { \
58 throw LSST_EXCEPT(TypeError, name, std::string(typeName[vtype])); \
59 } catch (boost::bad_any_cast&) { \
60 throw LSST_EXCEPT(TypeError, name, std::string(typeName[vtype])); \
63 #define POL_GETLIST(name, type, vtype) \
65 return _data->getArray<type>(name); \
66 } catch (lsst::pex::exceptions::NotFoundError&) { \
67 throw LSST_EXCEPT(NameNotFound, name); \
68 } catch (lsst::pex::exceptions::TypeError&) { \
69 throw LSST_EXCEPT(TypeError, name, std::string(typeName[vtype])); \
70 } catch (boost::bad_any_cast&) { \
71 throw LSST_EXCEPT(TypeError, name, std::string(typeName[vtype])); \
169 class Policy :
public lsst::daf::base::Citizen,
public lsst::daf::base::Persistable {
172 typedef std::shared_ptr<Policy>
Ptr;
219 explicit Policy(
const std::string& pathOrUrn);
220 explicit Policy(
const char *pathOrUrn);
243 const boost::filesystem::path& repository=
"");
313 const boost::filesystem::path& repos,
320 const boost::filesystem::path& repos,
323 const std::string& repos,
bool validate=
true);
363 return _data->nameCount();
384 bool topLevelOnly=
false,
385 bool append=
false)
const;
387 bool topLevelOnly=
false,
388 bool append=
false)
const;
390 bool topLevelOnly=
false,
391 bool append=
false)
const;
393 bool topLevelOnly=
false,
394 bool append=
false)
const;
451 void validate(ValidationError *errs=0)
const;
458 size_t valueCount(
const std::string& name)
const;
467 bool isArray(
const std::string& name)
const;
476 bool exists(
const std::string& name)
const;
481 bool isBool(
const std::string& name)
const;
486 bool isInt(
const std::string& name)
const;
491 bool isDouble(
const std::string& name)
const;
496 bool isString(
const std::string& name)
const;
501 bool isPolicy(
const std::string& name)
const;
506 bool isFile(
const std::string& name)
const;
513 const std::type_info&
getTypeInfo(
const std::string& name)
const;
520 const std::type_info&
typeOf(
const std::string& name)
const;
545 template <
typename T> T
getValue(
const std::string& name)
const;
553 template <
typename T> std::vector<T>
getValueArray(
const std::string& name)
const;
603 int getInt(
const std::string& name)
const {
631 const std::string
getString(
const std::string& name)
const {
708 template <
typename T>
void setValue(
const std::string& name,
const T& value);
709 void set(
const std::string& name,
const Ptr& value);
710 void set(
const std::string& name,
const FilePtr& value);
711 void set(
const std::string& name,
bool value);
712 void set(
const std::string& name,
int value);
713 void set(
const std::string& name,
double value);
714 void set(
const std::string& name,
const std::string& value);
715 void set(
const std::string& name,
const char *value);
745 template <
typename T>
void addValue(
const std::string& name,
const T& value);
746 void add(
const std::string& name,
const Ptr& value);
747 void add(
const std::string& name,
const FilePtr& value);
748 void add(
const std::string& name,
bool value);
749 void add(
const std::string& name,
int value);
750 void add(
const std::string& name,
double value);
751 void add(
const std::string& name,
const std::string& value);
752 void add(
const std::string& name,
const char *value);
760 void remove(
const std::string& name);
811 ValidationError *errs=0);
821 virtual std::string
str(
const std::string& name,
822 const std::string& indent=
"")
const;
831 virtual void print(std::ostream& out,
const std::string& label=
"Policy",
832 const std::string& indent=
"")
const;
850 Policy(
const lsst::daf::base::PropertySet::Ptr ps)
851 : Citizen(typeid(this)), lsst::daf::base::Persistable(), _data(ps)
855 lsst::daf::base::PropertySet::Ptr _data;
859 int _names(std::list<std::string>&
names,
bool topLevelOnly=
false,
860 bool append=
false,
int want=3)
const;
861 int _names(std::vector<std::string>&
names,
bool topLevelOnly=
false,
862 bool append=
false,
int want=3)
const;
868 template <
typename T>
869 void _validate(
const std::string& name,
const T& value,
int curCount=0);
871 std::vector<lsst::daf::base::Persistable::Ptr>
872 _getPersistList(
const std::string& name)
const
876 std::vector<lsst::daf::base::PropertySet::Ptr>
877 _getPropSetList(
const std::string& name)
const
882 static Policy *_createPolicy(PolicySource& input,
bool doIncludes,
883 const boost::filesystem::path& repos,
885 static Policy *_createPolicy(
const std::string& input,
bool doIncludes,
886 const boost::filesystem::path& repos,
900 return _names(names, topLevelOnly, append, 7);
905 return _names(names, topLevelOnly, append, 4);
911 return _names(names, topLevelOnly, append, 1);
916 return _names(names, topLevelOnly, append, 2);
921 _names(out, topLevelOnly,
true, 7);
926 _names(out, topLevelOnly,
true, 4);
931 _names(out, topLevelOnly,
true, 1);
936 _names(out, topLevelOnly,
true, 2);
941 return _data->valueCount(name);
945 return _data->isArray(name);
949 return _data->exists(name);
954 return (_data->typeOf(name) ==
typeid(bool));
963 return (_data->typeOf(name) ==
typeid(int));
972 return (_data->typeOf(name) ==
typeid(double));
981 return (_data->typeOf(name) ==
typeid(std::string));
990 return (_data->typeOf(name) ==
typeid(lsst::daf::base::PropertySet::Ptr));
1009 try {
return _data->typeOf(name); }
1010 catch (lsst::pex::exceptions::NotFoundError& e) {
1020 return ConstPtr(
new Policy(_data->get<lsst::daf::base::PropertySet::Ptr>(name)));
1023 return Ptr(
new Policy(_data->get<lsst::daf::base::PropertySet::Ptr>(name)));
1028 return _data->getArray<std::string>(name);
1045 _validate(name, value);
1046 _data->set(name, value->asPropertySet());
1049 _validate(name, value);
1050 _data->set(name, value);
1053 _validate(name, value);
1054 _data->set(name, value);
1057 _validate(name, value);
1058 _data->set(name, value);
1060 inline void Policy::set(
const std::string& name,
const std::string& value) {
1061 _validate(name, value);
1062 _data->set(name, value);
1066 throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
1067 std::string(
"Attempted to assign NULL value to ")
1069 _validate(name, std::string(value));
1070 _data->set(name, std::string(value));
1073 #define POL_ADD(name, value) \
1074 try { _data->add(name, value); } \
1075 catch(lsst::pex::exceptions::TypeError&) { \
1076 throw LSST_EXCEPT(TypeError, name, getTypeName(name)); \
1081 POL_ADD(name, value->asPropertySet())
1095 inline void Policy::add(
const std::string& name,
const std::string& value) {
1100 std::string v(value);
1107 _data->remove(name);
1113 return _createPolicy(input, doIncludes, boost::filesystem::path(), validate);
1119 return _createPolicy(input, doIncludes, boost::filesystem::path(), validate);
1123 const boost::filesystem::path& repository,
1126 return _createPolicy(input,
true, repository, validate);
1130 const std::string& repository,
1133 return _createPolicy(input,
true, boost::filesystem::path(repository), validate);
1137 const char *repository,
1140 return _createPolicy(input,
true, boost::filesystem::path(repository), validate);
1144 const boost::filesystem::path& repository,
1147 return _createPolicy(input,
true, repository, validate);
1151 const std::string& repository,
1154 return _createPolicy(input,
true, boost::filesystem::path(repository), validate);
1158 const char *repository,
1161 return _createPolicy(input,
true, boost::filesystem::path(repository), validate);
1168 throw LSST_EXCEPT(
TypeError, name,
"not implemented for this type");
1170 template <>
bool Policy::getValue<bool>(
const std::string& name)
const;
1171 template <>
int Policy::getValue<int>(
const std::string& name)
const;
1172 template <>
double Policy::getValue<double>(
const std::string& name)
const;
1173 template <> std::string Policy::getValue<std::string>(
const std::string& name)
const;
1174 template <>
Policy::FilePtr Policy::getValue<Policy::FilePtr>(
const std::string& name)
const;
1175 template <>
Policy::ConstPtr Policy::getValue<Policy::ConstPtr>(
const std::string& name)
const;
1179 throw LSST_EXCEPT(
TypeError, name,
"not implemented for this type");
1181 template <> std::vector<bool> Policy::getValueArray<bool>(
const std::string& name)
const;
1182 template <> std::vector<int> Policy::getValueArray<int>(
const std::string& name)
const;
1183 template <> std::vector<double> Policy::getValueArray<double>(
const std::string& name)
const;
1184 template <> std::vector<std::string> Policy::getValueArray<std::string>(
const std::string& name)
const;
1185 template <>
Policy::FilePtrArray Policy::getValueArray<Policy::FilePtr>(
const std::string& name)
const;
1191 throw LSST_EXCEPT(
TypeError,
"unknown",
"not implemented for this type");
1204 throw LSST_EXCEPT(
TypeError, name,
"not implemented for this type");
1206 template <>
void Policy::setValue<bool>(
const std::string& name,
const bool& value);
1207 template <>
void Policy::setValue<int>(
const std::string& name,
const int& value);
1208 template <>
void Policy::setValue<double>(
const std::string& name,
const double& value);
1209 template <>
void Policy::setValue<std::string>(
const std::string& name,
const std::string& value);
1210 template <>
void Policy::setValue<Policy::Ptr>(
const std::string& name,
const Policy::Ptr& value);
1211 template <>
void Policy::setValue<Policy::FilePtr>(
const std::string& name,
const Policy::FilePtr& value);
1215 throw LSST_EXCEPT(
TypeError, name,
"not implemented for this type");
1217 template <>
void Policy::addValue<bool>(
const std::string& name,
const bool& value);
1218 template <>
void Policy::addValue<int>(
const std::string& name,
const int& value);
1219 template <>
void Policy::addValue<double>(
const std::string& name,
const double& value);
1220 template <>
void Policy::addValue<std::string>(
const std::string& name,
const std::string& value);
1221 template <>
void Policy::addValue<Policy::Ptr>(
const std::string& name,
const Policy::Ptr& value);
1222 template <>
void Policy::addValue<Policy::FilePtr>(
const std::string& name,
const Policy::FilePtr& value);
1226 #endif // LSST_PEX_POLICY_POLICY_H
bool isDictionary() const
return true if it appears that this Policy actually contains dictionary definition data...
std::vector< int > IntArray
std::shared_ptr< PolicyFile > FilePtr
std::shared_ptr< Policy > Ptr
int getInt(const std::string &name) const
return an integer value associated with the given name.
void set(const std::string &name, const Ptr &value)
Set a value with the given name.
size_t valueCount(const std::string &name) const
return the number of values currently associated with a given name
bool isDouble(const std::string &name) const
return true if the value pointed to by the given name is a double
std::vector< Ptr > PolicyPtrArray
bool exists(const std::string &name) const
return true if a value exists in this policy for the given name.
ConstPolicyPtrArray getConstPolicyArray(const std::string &name) const
return an array of Policy pointers associated with the given name.
static Policy * createPolicyFromUrn(const std::string &urn, bool validate=true)
Create a Policy from a file specified by a URN.
static Policy * createPolicy(PolicySource &input, bool doIncludes=true, bool validate=true)
create a Policy from a file.
bool isInt(const std::string &name) const
return true if the value pointed to by the given name is an integer
PolicyPtrArray getPolicyArray(const std::string &name) const
return an array of Policy pointers associated with the given name.
#define POL_ADD(name, value)
std::vector< T > getValueArray(const std::string &name) const
Template-ized version of getIntArray, getPolicyPtrArray, etc.
lsst::daf::base::PropertySet::Ptr asPropertySet()
return the internal policy data as a PropertySet pointer.
bool getBool(const std::string &name) const
return a boolean value associated with the given name.
a container for holding hierarchical configuration data in memory.
std::shared_ptr< const Dictionary > ConstDictPtr
virtual std::string str(const std::string &name, const std::string &indent="") const
return a string representation of the value given by a name.
const std::string getString(const std::string &name) const
return a string value associated with the given name .
StringArray getStringArray(const std::string &name) const
return an array of values associated with the given name
const std::type_info & getTypeInfo(const std::string &name) const
return the type information for the underlying type associated with a given name. ...
bool isBool(const std::string &name) const
return true if the value pointed to by the given name is a boolean
void setDictionary(const Dictionary &dict)
Update this policy's dictionary that it uses to validate itself.
an exception indicating that a policy parameter of a given name can not be found in a Policy object...
bool canValidate() const
Can this policy validate itself – that is, does it have a dictionary that it can use to validate itse...
virtual void print(std::ostream &out, const std::string &label="Policy", const std::string &indent="") const
print the contents of this policy to an output stream.
static const char *const typeName[]
c-string forms for the supported value types.
std::shared_ptr< Dictionary > DictPtr
FilePtr getFile(const std::string &name) const
return a PolicyFile (a reference to a file with "sub-Policy" data) identified by a given name...
std::ostream & operator<<(std::ostream &os, const Policy &p)
int nameCount() const
How many names of parameters does this policy file have?
an abstract class representing a source of serialized Policy parameter data.
static FilePtr createPolicyFile(const std::string &pathOrUrn, bool strict=false)
Create a PolicyFile or UrnPolicyFile from pathOrUrn.
const ConstDictPtr getDictionary() const
The dictionary (if any) that this policy uses to validate itself, including checking set() and add() ...
std::vector< std::string > StringArray
#define POL_GETSCALAR(name, type, vtype)
definition of Policy-specific exceptions classes
void addValue(const std::string &name, const T &value)
Add a value to an array of values with a given name.
FilePtrArray getFileArray(const std::string &name) const
return an array of PolicyFile pointers associated with the given name.
void setValue(const std::string &name, const T &value)
Set a value with the given name.
int mergeDefaults(const Policy &defaultPol, bool keepForValidation=true, ValidationError *errs=0)
use the values found in the given policy as default values for parameters not specified in this polic...
bool isString(const std::string &name) const
return true if the value pointed to by the given name is a string
Policy(const lsst::daf::base::PropertySet::Ptr ps)
use a PropertySet as the data for a new Policy object
#define POL_GETLIST(name, type, vtype)
std::string toString() const
convert the entire contents of this policy to a string.
int fileNames(std::list< std::string > &names, bool topLevelOnly=false, bool append=false) const
load the names of parameters into a given list.
bool isPolicy(const std::string &name) const
return true if the value pointed to by the given name is a Policy
std::vector< FilePtr > FilePtrArray
bool isArray(const std::string &name) const
return true if multiple values can be retrieved via the given name.
BoolArray getBoolArray(const std::string &name) const
return an array of values associated with the given name
double getDouble(const std::string &name) const
return a double value associated with the given name.
std::vector< double > DoubleArray
DoubleArray getDoubleArray(const std::string &name) const
return an array of values associated with the given name
void remove(const std::string &name)
Remove all values with a given name.
int names(std::list< std::string > &names, bool topLevelOnly=false, bool append=false) const
load the names of parameters into a given list.
bool isFile(const std::string &name) const
return true if the value pointed to by the given name is a PolicyFile
void validate(ValidationError *errs=0) const
Validate this policy, using its stored dictionary.
const std::type_info & typeOf(const std::string &name) const
return the type information for the underlying type associated with a given name. ...
static ValueType getTypeByName(const std::string &name)
Given the human-readable name of a type ("bool", "int", "policy", etc), what is its ValueType (BOOL...
static ValueType getValueType()
A template-ized way to get the ValueType.
const char * getTypeName(const std::string &name) const
return a string name for the type associated with the parameter of a given name.
T getValue(const std::string &name) const
Template-ized version of getInt, getPolicy, etc.
int loadPolicyFiles(bool strict=true)
Recursively replace all PolicyFile values with the contents of the files they refer to...
ConstPtr getPolicy(const std::string &name) const
return a "sub-Policy" identified by a given name.
int paramNames(std::list< std::string > &names, bool topLevelOnly=false, bool append=false) const
load the names of parameters into a given list.
std::vector< bool > BoolArray
an exception indicating that a policy parameter with a given name has a type different from the one t...
int policyNames(std::list< std::string > &names, bool topLevelOnly=false, bool append=false) const
load the names of parameters into a given list.
void add(const std::string &name, const Ptr &value)
Add a value to an array of values with a given name.
std::shared_ptr< const Policy > ConstPtr
Policy()
Create an empty policy.
std::vector< ConstPtr > ConstPolicyPtrArray
IntArray getIntArray(const std::string &name) const
return an array of values associated with the given name
ValueType
an enumeration for the supported policy types
virtual ~Policy()
destroy this policy