/usr/include/trilinos/Teuchos_ObjectBuilder.hpp is in libtrilinos-teuchos-dev 12.12.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | // @HEADER
// ***********************************************************************
//
// Teuchos: Common Tools Package
// Copyright (2004) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ***********************************************************************
// @HEADER
#ifndef Teuchos_OBJECT_BUILDER_H
#define Teuchos_OBJECT_BUILDER_H
#include "Teuchos_ParameterList.hpp"
#include "Teuchos_ParameterListAcceptor.hpp"
#include "Teuchos_AbstractFactoryStd.hpp"
#include "Teuchos_StandardParameterEntryValidators.hpp"
namespace Teuchos {
/** \brief Generic parameterlist driven bulider class.
*
* This is a generic builder class that provides a validated parameter list
* and can build anything that can be constructed with a default constructor
* and accepts a parameter list through setParameterList (e.g. it derives from
* ParameterListAcceptor).
*
* Note the following:<ul>
*
* <li> The default object name is "Object" (this can be changed through
* setObjectName)
*
* <li> The default object type name is "Object Type" (this can be changed
* through setObjectTypeName)
*
* <li> The valid parameter list has a parameter named "Object Type" with a
* default value of "None"
*
* <li> The builder will create a null RCP if no factories have been set on
* it with setObjectFactory
*
* <li> A parameter list need not be set on the builder to call create, it
* will simply create the default factory which is either "None" if no
* factories have been set or it will be the last factory that was set
*
* <li> Setting a parameter list on the builder allows you to specify which
* object will be created by default and allows you to control what options
* will be used in each object.
*
* </ul>
*
*
* \author Todd Coffey <tscoffe@sandia.gov>
*/
template<class ObjectType>
class ObjectBuilder : virtual public ParameterListAcceptor
{
public:
/** \brief . */
ObjectBuilder();
/** \brief . */
~ObjectBuilder();
/** \brief Set the name of the object this will be a builder for, e.g. "Object". */
void setObjectName(
const std::string &objectName
);
/** \brief Set the name of the parameterlist selector, e.g. "Object Type". */
void setObjectTypeName(
const std::string &objectTypeName
);
/** \brief Set a new Object factory object. */
void setObjectFactory(
const RCP<const AbstractFactory<ObjectType> > &objectFactory,
const std::string &objectFactoryName
);
/** \brief Get the name of the Object that will be created
* on the next call to <tt>this->create()</tt>.
*/
std::string getObjectName() const;
/** \brief Set the name of the desired object to be created when the
* parameter list does not specify which object you want and when create is
* called without arguments.
*/
void setDefaultObject( const std::string &defaultObject_name );
/** \brief . */
RCP<ObjectType> create(
const std::string &objectName = ""
) const;
/** \name Overridden from ParameterListAcceptor */
//@{
/** \brief . */
void setParameterList(const RCP<ParameterList> & paramList);
/** \brief . */
RCP<ParameterList> getNonconstParameterList();
/** \brief . */
RCP<ParameterList> unsetParameterList();
/** \brief. */
RCP<const ParameterList> getParameterList() const;
/** \brief. */
RCP<const ParameterList> getValidParameters() const;
//@}
private:
// //////////////////////////////////////
// Private types
typedef RCP<const AbstractFactory<ObjectType > > object_fcty_t;
// //////////////////////////////////////
// Private data members
RCP<ParameterList> paramList_;
mutable RCP<const ParameterList> validParamList_;
mutable RCP<const StringToIntegralParameterEntryValidator<int> > objectValidator_;
std::string object_name_;
std::string objectType_name_;
Array<std::string> validObjectNames_;
Array<object_fcty_t> objectArray_;
std::string defaultObject_name_;
// //////////////////////////////////////
// Private member functions
void initializeDefaults_();
};
// Nonmember constructors
template<class ObjectType>
RCP<ObjectBuilder<ObjectType> > objectBuilder()
{
RCP<ObjectBuilder<ObjectType> > ob = rcp(new ObjectBuilder<ObjectType>() );
return ob;
}
template<class ObjectType>
RCP<ObjectBuilder<ObjectType> >
objectBuilder(const std::string& objectName, const std::string& objectTypeName)
{
RCP<ObjectBuilder<ObjectType> > ob = rcp(new ObjectBuilder<ObjectType>() );
ob->setObjectName(objectName);
ob->setObjectTypeName(objectTypeName);
return ob;
}
//
// Implementation
//
template<class ObjectType>
ObjectBuilder<ObjectType>::ObjectBuilder()
{
this->initializeDefaults_();
}
template<class ObjectType>
ObjectBuilder<ObjectType>::~ObjectBuilder()
{
#ifdef TEUCHOS_DEBUG
// Validate that we read the parameters correctly!
if(!is_null(paramList_)) {
paramList_->validateParameters(*this->getValidParameters());
}
#endif
}
template<class ObjectType>
void ObjectBuilder<ObjectType>::setObjectFactory(
const RCP<const AbstractFactory<ObjectType > > &objectFactory,
const std::string &objectName
)
{
TEUCHOS_TEST_FOR_EXCEPT( objectName.length() == 0 );
validObjectNames_.push_back(objectName);
objectArray_.push_back(objectFactory);
defaultObject_name_ = objectName;
validParamList_ = null;
#ifdef TEUCHOS_DEBUG
this->getValidParameters();
#endif // TEUCHOS_DEBUG
}
template<class ObjectType>
std::string
ObjectBuilder<ObjectType>::getObjectName() const
{
if(is_null(validParamList_)) {
this->getValidParameters();
}
// If the user has not specified a ParameterList, then use the ValidParameterList.
RCP<ParameterList> pl = null;
if (!is_null(paramList_)) {
pl = paramList_;
} else {
pl = parameterList();
pl->setParameters(*this->getValidParameters());
}
return objectValidator_->getStringValue(*pl, objectType_name_, defaultObject_name_);
}
template<class ObjectType>
void ObjectBuilder<ObjectType>::setParameterList(
RCP<ParameterList> const& paramList
)
{
if (!is_null(paramList)) {
paramList->validateParameters(*this->getValidParameters());
paramList_ = paramList;
}
}
template<class ObjectType>
RCP<ParameterList>
ObjectBuilder<ObjectType>::getNonconstParameterList()
{
return paramList_;
}
template<class ObjectType>
RCP<ParameterList>
ObjectBuilder<ObjectType>::unsetParameterList()
{
#ifdef TEUCHOS_DEBUG
// Validate that we read the parameters correctly!
if(!is_null(paramList_))
paramList_->validateParameters(*this->getValidParameters());
#endif
RCP<ParameterList> _paramList = paramList_;
paramList_ = null;
return _paramList;
}
template<class ObjectType>
RCP<const ParameterList>
ObjectBuilder<ObjectType>::getParameterList() const
{
return paramList_;
}
template<class ObjectType>
RCP<const ParameterList>
ObjectBuilder<ObjectType>::getValidParameters() const
{
if(!validParamList_.get()) {
RCP<ParameterList> validParamList = parameterList();
// Object Types
objectValidator_ = rcp(
new StringToIntegralParameterEntryValidator<int>(
validObjectNames_, objectType_name_
)
);
objectValidator_->validateString(defaultObject_name_,objectType_name_);
validParamList->set(
objectType_name_, defaultObject_name_,
(std::string("Determines the type of " + object_name_ + " object that will be built.\n")
+ "The parameters for each " + objectType_name_ + " are specified in this sublist"
).c_str(),
objectValidator_
);
for( int i = 0; i < static_cast<int>(objectArray_.size()); ++i ) {
const std::string
&sname = validObjectNames_[i+1];
const RCP<ObjectType >
object = objectArray_[i]->create();
validParamList->sublist(sname).setParameters(
*object->getValidParameters()).disableRecursiveValidation();
}
validParamList_ = validParamList;
}
return validParamList_;
}
template<class ObjectType>
void ObjectBuilder<ObjectType>::setDefaultObject(
const std::string &defaultObject_name
)
{
#ifdef TEUCHOS_DEBUG
if (is_null(validParamList_)) { // We need the objectValidator_
this->getValidParameters();
}
objectValidator_->validateString(defaultObject_name,objectType_name_);
#endif // TEUCHOS_DEBUG
defaultObject_name_ = defaultObject_name;
// This is necessary to change the default in the valid parameter list
validParamList_ = null;
}
template<class ObjectType>
RCP<ObjectType >
ObjectBuilder<ObjectType>::create(
const std::string &objectName
) const
{
if (is_null(validParamList_)) { // We need the objectValidator_
this->getValidParameters();
}
const std::string
sname = ( objectName.length()
? objectName
: this->getObjectName() );
RCP<ObjectType> object = null;
// Get the index of this object factory (this will validate!)
const int
s_idx = objectValidator_->getIntegralValue(sname, objectType_name_);
if (s_idx != 0) {
// Create the uninitialized object
object = objectArray_[s_idx-1]->create();
TEUCHOS_TEST_FOR_EXCEPTION( is_null(object), std::logic_error,
(std::string("Error! ObjectBuilder attempted to create an object of type ")
+ validObjectNames_[s_idx] + " and it came back as a null RCP!").c_str()
);
// Allow the user to not set a parameterlist (this requires copying the
// parameters in the valid parameter list into a new parameter list:
RCP<ParameterList> pl = null;
if (is_null(paramList_)) {
pl = parameterList();
pl->setParameters(this->getValidParameters()->sublist(sname));
} else {
#ifdef TEUCHOS_DEBUG
// We're validating the parameter list here again because we're storing a
// pointer to it and the user could have changed it.
paramList_->validateParameters(*this->getValidParameters());
#endif // TEUCHOS_DEBUG
pl = sublist(paramList_,sname);
}
// Now set the parameters for the object
object->setParameterList(pl);
}
return object;
}
template<class ObjectType>
void ObjectBuilder<ObjectType>::setObjectName(
const std::string &objectName
)
{
TEUCHOS_TEST_FOR_EXCEPT(objectName.length() == 0);
object_name_ = objectName;
validParamList_ = null;
}
template<class ObjectType>
void ObjectBuilder<ObjectType>::setObjectTypeName(
const std::string &objectTypeName
)
{
TEUCHOS_TEST_FOR_EXCEPT(objectTypeName.length() == 0);
objectType_name_ = objectTypeName;
validParamList_ = null;
}
template<class ObjectType>
void ObjectBuilder<ObjectType>::initializeDefaults_()
{
object_name_ = "Object";
objectType_name_ = "Object Type";
defaultObject_name_ = "None";
validObjectNames_.resize(0);
validObjectNames_.push_back(defaultObject_name_);
}
} // namespace Teuchos
#endif //Teuchos_OBJECT_BUILDER_H
|