10 #ifndef Teuchos_OBJECT_BUILDER_H
11 #define Teuchos_OBJECT_BUILDER_H
14 #include "Teuchos_ParameterListAcceptor.hpp"
15 #include "Teuchos_AbstractFactoryStd.hpp"
16 #include "Teuchos_StandardParameterEntryValidators.hpp"
56 template<
class ObjectType>
69 const std::string &objectName
74 const std::string &objectTypeName
80 const std::string &objectFactoryName
96 const std::string &objectName =
""
133 std::string object_name_;
134 std::string objectType_name_;
138 std::string defaultObject_name_;
143 void initializeDefaults_();
151 template<
class ObjectType>
159 template<
class ObjectType>
160 RCP<ObjectBuilder<ObjectType> >
161 objectBuilder(
const std::string& objectName,
const std::string& objectTypeName)
163 RCP<ObjectBuilder<ObjectType> > ob =
rcp(
new ObjectBuilder<ObjectType>() );
164 ob->setObjectName(objectName);
165 ob->setObjectTypeName(objectTypeName);
175 template<
class ObjectType>
178 this->initializeDefaults_();
182 template<
class ObjectType>
188 template<
class ObjectType>
191 const std::string &objectName
195 validObjectNames_.push_back(objectName);
196 objectArray_.push_back(objectFactory);
197 defaultObject_name_ = objectName;
198 validParamList_ = null;
200 this->getValidParameters();
201 #endif // TEUCHOS_DEBUG
205 template<
class ObjectType>
210 this->getValidParameters();
217 pl = parameterList();
220 return objectValidator_->
getStringValue(*pl, objectType_name_, defaultObject_name_);
224 template<
class ObjectType>
231 paramList_ = paramList;
236 template<
class ObjectType>
244 template<
class ObjectType>
251 paramList_->validateParameters(*this->getValidParameters());
259 template<
class ObjectType>
267 template<
class ObjectType>
271 if(!validParamList_.get()) {
274 objectValidator_ =
rcp(
276 validObjectNames_, objectType_name_
279 objectValidator_->validateString(defaultObject_name_,objectType_name_);
281 objectType_name_, defaultObject_name_,
282 (std::string(
"Determines the type of " + object_name_ +
" object that will be built.\n")
283 +
"The parameters for each " + objectType_name_ +
" are specified in this sublist"
287 for(
int i = 0; i < static_cast<int>(objectArray_.size()); ++i ) {
289 &sname = validObjectNames_[i+1];
291 object = objectArray_[i]->create();
293 *object->getValidParameters()).disableRecursiveValidation();
295 validParamList_ = validParamList;
297 return validParamList_;
300 template<
class ObjectType>
302 const std::string &defaultObject_name
306 if (
is_null(validParamList_)) {
307 this->getValidParameters();
309 objectValidator_->validateString(defaultObject_name,objectType_name_);
310 #endif // TEUCHOS_DEBUG
311 defaultObject_name_ = defaultObject_name;
313 validParamList_ = null;
316 template<
class ObjectType>
319 const std::string &objectName
322 if (
is_null(validParamList_)) {
323 this->getValidParameters();
326 sname = ( objectName.length()
328 : this->getObjectName() );
332 s_idx = objectValidator_->getIntegralValue(sname, objectType_name_);
335 object = objectArray_[s_idx-1]->create();
337 (std::string(
"Error! ObjectBuilder attempted to create an object of type ")
338 + validObjectNames_[s_idx] +
" and it came back as a null RCP!").c_str()
344 pl = parameterList();
345 pl->
setParameters(this->getValidParameters()->sublist(sname));
351 #endif // TEUCHOS_DEBUG
352 pl = sublist(paramList_,sname);
355 object->setParameterList(pl);
361 template<
class ObjectType>
363 const std::string &objectName
367 object_name_ = objectName;
368 validParamList_ = null;
372 template<
class ObjectType>
374 const std::string &objectTypeName
378 objectType_name_ = objectTypeName;
379 validParamList_ = null;
383 template<
class ObjectType>
387 object_name_ =
"Object";
388 objectType_name_ =
"Object Type";
390 defaultObject_name_ =
"None";
391 validObjectNames_.resize(0);
392 validObjectNames_.push_back(defaultObject_name_);
400 #endif //Teuchos_OBJECT_BUILDER_H
void setDefaultObject(const std::string &defaultObject_name)
Set the name of the desired object to be created when the parameter list does not specify which objec...
RCP< ParameterList > getNonconstParameterList()
Generic parameterlist driven bulider class.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
RCP< const ParameterList > getValidParameters() const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
RCP< ParameterList > unsetParameterList()
void setObjectName(const std::string &objectName)
Set the name of the object this will be a builder for, e.g. "Object".
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Templated set method.
RCP< ObjectType > create(const std::string &objectName="") const
void setObjectFactory(const RCP< const AbstractFactory< ObjectType > > &objectFactory, const std::string &objectFactoryName)
Set a new Object factory object.
void setObjectTypeName(const std::string &objectTypeName)
Set the name of the parameterlist selector, e.g. "Object Type".
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Templated Parameter List class.
std::string getStringValue(ParameterList const ¶mList, std::string const ¶mName)
Get a std::string value for a parameter that is assumed to already be set.
void setParameterList(const RCP< ParameterList > ¶mList)
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
Validate the parameters in this list given valid selections in the input list.
ParameterList & setParameters(const ParameterList &source)
std::string getObjectName() const
Get the name of the Object that will be created on the next call to this->create().
Interface for objects that can accept a ParameterList.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Creates an empty sublist and returns a reference to the sublist name. If the list already exists...
Smart reference counting pointer class for automatic garbage collection.
Simple, universal "Abstract Factory" interface for the dynamic creation of objects.
RCP< const ParameterList > getParameterList() const
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call...