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"
56template<
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_();
151template<
class ObjectType>
159template<
class ObjectType>
160RCP<ObjectBuilder<ObjectType> >
161objectBuilder(
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);
175template<
class ObjectType>
178 this->initializeDefaults_();
182template<
class ObjectType>
188template<
class ObjectType>
191 const std::string &objectName
195 validObjectNames_.push_back(objectName);
196 objectArray_.push_back(objectFactory);
197 defaultObject_name_ = objectName;
198 validParamList_ = null;
205template<
class ObjectType>
220 return objectValidator_->getStringValue(*pl, objectType_name_, defaultObject_name_);
224template<
class ObjectType>
231 paramList_ = paramList;
236template<
class ObjectType>
244template<
class ObjectType>
259template<
class ObjectType>
267template<
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();
292 validParamList->sublist(sname).setParameters(
293 *object->getValidParameters()).disableRecursiveValidation();
295 validParamList_ = validParamList;
297 return validParamList_;
300template<
class ObjectType>
302 const std::string &defaultObject_name
306 if (
is_null(validParamList_)) {
309 objectValidator_->validateString(defaultObject_name,objectType_name_);
311 defaultObject_name_ = defaultObject_name;
313 validParamList_ = null;
316template<
class ObjectType>
319 const std::string &objectName
322 if (
is_null(validParamList_)) {
326 sname = ( objectName.length()
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()
352 pl =
sublist(paramList_,sname);
355 object->setParameterList(pl);
361template<
class ObjectType>
363 const std::string &objectName
367 object_name_ = objectName;
368 validParamList_ = null;
372template<
class ObjectType>
374 const std::string &objectTypeName
378 objectType_name_ = objectTypeName;
379 validParamList_ = null;
383template<
class ObjectType>
384void ObjectBuilder<ObjectType>::initializeDefaults_()
387 object_name_ =
"Object";
388 objectType_name_ =
"Object Type";
390 defaultObject_name_ =
"None";
391 validObjectNames_.resize(0);
392 validObjectNames_.push_back(defaultObject_name_);
Templated Parameter List class.
Simple, universal "Abstract Factory" interface for the dynamic creation of objects.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes.
Generic parameterlist driven bulider class.
RCP< const ParameterList > getValidParameters() const
RCP< ObjectType > create(const std::string &objectName="") const
RCP< const ParameterList > getParameterList() 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".
RCP< ParameterList > getNonconstParameterList()
void setObjectName(const std::string &objectName)
Set the name of the object this will be a builder for, e.g. "Object".
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...
std::string getObjectName() const
Get the name of the Object that will be created on the next call to this->create().
RCP< ParameterList > unsetParameterList()
void setParameterList(const RCP< ParameterList > ¶mList)
Interface for objects that can accept a ParameterList.
RCP< ParameterList > sublist(const RCP< ParameterList > ¶mList, const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Return a RCP to a sublist in another RCP-ed parameter list.
RCP< ParameterList > parameterList()
Nonmember constructor.
Smart reference counting pointer class for automatic garbage collection.
Standard implementation of a ParameterEntryValidator that maps from a list of strings to an enum or i...
#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.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.