Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_ObjectBuilder.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef Teuchos_OBJECT_BUILDER_H
43 #define Teuchos_OBJECT_BUILDER_H
44 
46 #include "Teuchos_ParameterListAcceptor.hpp"
47 #include "Teuchos_AbstractFactoryStd.hpp"
48 #include "Teuchos_StandardParameterEntryValidators.hpp"
49 
50 
51 namespace Teuchos {
52 
53 
88 template<class ObjectType>
89 class ObjectBuilder : virtual public ParameterListAcceptor
90 {
91 public:
92 
94  ObjectBuilder();
95 
98 
100  void setObjectName(
101  const std::string &objectName
102  );
103 
105  void setObjectTypeName(
106  const std::string &objectTypeName
107  );
108 
110  void setObjectFactory(
111  const RCP<const AbstractFactory<ObjectType> > &objectFactory,
112  const std::string &objectFactoryName
113  );
114 
118  std::string getObjectName() const;
119 
124  void setDefaultObject( const std::string &defaultObject_name );
125 
128  const std::string &objectName = ""
129  ) const;
130 
133 
135  void setParameterList(const RCP<ParameterList> & paramList);
136 
139 
142 
145 
148 
150 
151 private:
152 
153  // //////////////////////////////////////
154  // Private types
155 
157 
158  // //////////////////////////////////////
159  // Private data members
160 
161  RCP<ParameterList> paramList_;
162  mutable RCP<const ParameterList> validParamList_;
164 
165  std::string object_name_;
166  std::string objectType_name_;
167 
168  Array<std::string> validObjectNames_;
169  Array<object_fcty_t> objectArray_;
170  std::string defaultObject_name_;
171 
172  // //////////////////////////////////////
173  // Private member functions
174 
175  void initializeDefaults_();
176 
177 };
178 
179 
180 // Nonmember constructors
181 
182 
183 template<class ObjectType>
184 RCP<ObjectBuilder<ObjectType> > objectBuilder()
185 {
187  return ob;
188 }
189 
190 
191 template<class ObjectType>
192 RCP<ObjectBuilder<ObjectType> >
193 objectBuilder(const std::string& objectName, const std::string& objectTypeName)
194 {
195  RCP<ObjectBuilder<ObjectType> > ob = rcp(new ObjectBuilder<ObjectType>() );
196  ob->setObjectName(objectName);
197  ob->setObjectTypeName(objectTypeName);
198  return ob;
199 }
200 
201 
202 //
203 // Implementation
204 //
205 
206 
207 template<class ObjectType>
209 {
210  this->initializeDefaults_();
211 }
212 
213 
214 template<class ObjectType>
216 {
217 }
218 
219 
220 template<class ObjectType>
222  const RCP<const AbstractFactory<ObjectType > > &objectFactory,
223  const std::string &objectName
224  )
225 {
226  TEUCHOS_TEST_FOR_EXCEPT( objectName.length() == 0 );
227  validObjectNames_.push_back(objectName);
228  objectArray_.push_back(objectFactory);
229  defaultObject_name_ = objectName;
230  validParamList_ = null;
231 #ifdef TEUCHOS_DEBUG
232  this->getValidParameters();
233 #endif // TEUCHOS_DEBUG
234 }
235 
236 
237 template<class ObjectType>
238 std::string
240 {
241  if(is_null(validParamList_)) {
242  this->getValidParameters();
243  }
244  // If the user has not specified a ParameterList, then use the ValidParameterList.
245  RCP<ParameterList> pl = null;
246  if (!is_null(paramList_)) {
247  pl = paramList_;
248  } else {
249  pl = parameterList();
250  pl->setParameters(*this->getValidParameters());
251  }
252  return objectValidator_->getStringValue(*pl, objectType_name_, defaultObject_name_);
253 }
254 
255 
256 template<class ObjectType>
258  RCP<ParameterList> const& paramList
259  )
260 {
261  if (!is_null(paramList)) {
262  paramList->validateParameters(*this->getValidParameters());
263  paramList_ = paramList;
264  }
265 }
266 
267 
268 template<class ObjectType>
271 {
272  return paramList_;
273 }
274 
275 
276 template<class ObjectType>
279 {
280 #ifdef TEUCHOS_DEBUG
281  // Validate that we read the parameters correctly!
282  if(!is_null(paramList_))
283  paramList_->validateParameters(*this->getValidParameters());
284 #endif
285  RCP<ParameterList> _paramList = paramList_;
286  paramList_ = null;
287  return _paramList;
288 }
289 
290 
291 template<class ObjectType>
294 {
295  return paramList_;
296 }
297 
298 
299 template<class ObjectType>
302 {
303  if(!validParamList_.get()) {
304  RCP<ParameterList> validParamList = parameterList();
305  // Object Types
306  objectValidator_ = rcp(
308  validObjectNames_, objectType_name_
309  )
310  );
311  objectValidator_->validateString(defaultObject_name_,objectType_name_);
312  validParamList->set(
313  objectType_name_, defaultObject_name_,
314  (std::string("Determines the type of " + object_name_ + " object that will be built.\n")
315  + "The parameters for each " + objectType_name_ + " are specified in this sublist"
316  ).c_str(),
317  objectValidator_
318  );
319  for( int i = 0; i < static_cast<int>(objectArray_.size()); ++i ) {
320  const std::string
321  &sname = validObjectNames_[i+1];
322  const RCP<ObjectType >
323  object = objectArray_[i]->create();
324  validParamList->sublist(sname).setParameters(
325  *object->getValidParameters()).disableRecursiveValidation();
326  }
327  validParamList_ = validParamList;
328  }
329  return validParamList_;
330 }
331 
332 template<class ObjectType>
334  const std::string &defaultObject_name
335  )
336 {
337 #ifdef TEUCHOS_DEBUG
338  if (is_null(validParamList_)) { // We need the objectValidator_
339  this->getValidParameters();
340  }
341  objectValidator_->validateString(defaultObject_name,objectType_name_);
342 #endif // TEUCHOS_DEBUG
343  defaultObject_name_ = defaultObject_name;
344  // This is necessary to change the default in the valid parameter list
345  validParamList_ = null;
346 }
347 
348 template<class ObjectType>
351  const std::string &objectName
352  ) const
353 {
354  if (is_null(validParamList_)) { // We need the objectValidator_
355  this->getValidParameters();
356  }
357  const std::string
358  sname = ( objectName.length()
359  ? objectName
360  : this->getObjectName() );
361  RCP<ObjectType> object = null;
362  // Get the index of this object factory (this will validate!)
363  const int
364  s_idx = objectValidator_->getIntegralValue(sname, objectType_name_);
365  if (s_idx != 0) {
366  // Create the uninitialized object
367  object = objectArray_[s_idx-1]->create();
368  TEUCHOS_TEST_FOR_EXCEPTION( is_null(object), std::logic_error,
369  (std::string("Error! ObjectBuilder attempted to create an object of type ")
370  + validObjectNames_[s_idx] + " and it came back as a null RCP!").c_str()
371  );
372  // Allow the user to not set a parameterlist (this requires copying the
373  // parameters in the valid parameter list into a new parameter list:
374  RCP<ParameterList> pl = null;
375  if (is_null(paramList_)) {
376  pl = parameterList();
377  pl->setParameters(this->getValidParameters()->sublist(sname));
378  } else {
379 #ifdef TEUCHOS_DEBUG
380  // We're validating the parameter list here again because we're storing a
381  // pointer to it and the user could have changed it.
382  paramList_->validateParameters(*this->getValidParameters());
383 #endif // TEUCHOS_DEBUG
384  pl = sublist(paramList_,sname);
385  }
386  // Now set the parameters for the object
387  object->setParameterList(pl);
388  }
389  return object;
390 }
391 
392 
393 template<class ObjectType>
395  const std::string &objectName
396  )
397 {
398  TEUCHOS_TEST_FOR_EXCEPT(objectName.length() == 0);
399  object_name_ = objectName;
400  validParamList_ = null;
401 }
402 
403 
404 template<class ObjectType>
406  const std::string &objectTypeName
407  )
408 {
409  TEUCHOS_TEST_FOR_EXCEPT(objectTypeName.length() == 0);
410  objectType_name_ = objectTypeName;
411  validParamList_ = null;
412 }
413 
414 
415 template<class ObjectType>
417 {
418 
419  object_name_ = "Object";
420  objectType_name_ = "Object Type";
421 
422  defaultObject_name_ = "None";
423  validObjectNames_.resize(0);
424  validObjectNames_.push_back(defaultObject_name_);
425 
426 }
427 
428 
429 } // namespace Teuchos
430 
431 
432 #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()
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Set a parameter whose value has type T.
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. &quot;Object&quot;.
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. &quot;Object Type&quot;.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Templated Parameter List class.
std::string getStringValue(ParameterList const &paramList, std::string const &paramName)
Get a std::string value for a parameter that is assumed to already be set.
void setParameterList(const RCP< ParameterList > &paramList)
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-&gt;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 &quot;Abstract Factory&quot; 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...