Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Private Attributes | Related Functions | List of all members
Teuchos::ConstNonconstObjectContainer< ObjType > Class Template Reference

Simple class supporting the "runtime protection of const" idiom. More...

#include <Teuchos_ConstNonconstObjectContainer.hpp>

Public Member Functions

 ConstNonconstObjectContainer ()
 . Constructs to uninitialized More...
 
 ConstNonconstObjectContainer (const RCP< ObjType > &obj)
 . Calls initialize() with a non-const object. More...
 
 ConstNonconstObjectContainer (const RCP< const ObjType > &obj)
 . Calls initialize() with a const object. More...
 
void initialize (const RCP< ObjType > &obj)
 . Initialize using a non-const object. Allows both const and non-const access to the contained object. More...
 
void initialize (const RCP< const ObjType > &obj)
 . Initialize using a const object. Allows only const access enforced with a runtime check. More...
 
void uninitialize ()
 
ConstNonconstObjectContainer
< ObjType > & 
operator= (ENull)
 
bool isConst () const
 Returns true if const-only access to the object is allowed. More...
 
RCP< ObjType > getNonconstObj () const
 Get an RCP to the non-const contained object. More...
 
RCP< const ObjType > getConstObj () const
 Get an RCP to the const contained object. More...
 
RCP< const ObjType > operator() () const
 Perform shorthand for getConstObj(). More...
 
const ObjType * operator-> () const
 Pointer (->) access to underlying const object. More...
 
const ObjType & operator* () const
 Dereference the underlying object. More...
 
 operator RCP< const ObjType > () const
 Perform an implicit conversion to an RCP<const ObjType>. More...
 

Private Attributes

RCP< const ObjType > constObj_
 
bool isConst_
 

Related Functions

(Note that these are not member functions.)

template<class T >
bool is_null (const ConstNonconstObjectContainer< T > &p)
 Returns true if p.get()==NULL. More...
 
template<class T >
bool nonnull (const ConstNonconstObjectContainer< T > &p)
 Returns true if p.get()!=NULL. More...
 

Detailed Description

template<class ObjType>
class Teuchos::ConstNonconstObjectContainer< ObjType >

Simple class supporting the "runtime protection of const" idiom.

This is a foundational class for supporting the "runtime protection of const" idiom. The problem this class is designed to help solve is the general issue of const protection and const handling for "held" objects inside of "container" objects. The use case this class is designed to support involves having the client create the "held" object, give it to the "container" object, and where the "container" object has functions to give the "held" object back again. In this case, there are to specific roles the "container" object is performing. One role, the primary role, is the primary function the "container" object was designed to perform where it needs functionality of the the "held" object where only the const interface of the "held" object is required. If this primary role were the only consideration, we could just write the "container" class as:

// Basic "container" implementation that does not consider general
// const/non-const issues.
class Container {
public:
setHeld(const RCP<const Held> &held)
{ held_ = held; }
RCP<const Held> getHeld() const
{ return held_; }
void doSomething() // The primary role!
{ stuff = held_->computeSomething(...); } // const interface of Held!
private:
RCP<const Held> held_;
};

The problem with this design of the "container" class is that it does not well support the second natural role of any such "container" object, and that is to act as a general object container that can be used to store and extract the "held" object. The difficulty that occurs is when the client has a non-const reference to the "held" object, gives it to the "container" object and then needs to get back a non-const reference to the "held" object later. With the current design, the client code must do a const cast such as in:

void setUpContainer( const Ptr<Container> &container )
{
// A non-const version of Held
RCP<Held> myHeld = createNewHeld(...);
// Give my non-const RCP to Held as a const RCP to held to container
container->setHeld(myHeld);
}
void updateContainer( const Ptr<Container> &container )
{
// Get back a non-const version of Held (WARNING: const_cast!)
RCP<Held> myHeld = rcp_const_cast<Held>(container->getHeld());
// Change Held
myHeld->changeSomething(...);
// Put back Held
container->setHeld(myHeld);
}

Code like shown above if very common and exposes the core problem. The client should not have to const cast to get back a non-const verison of the "held" object that it put in the "container" object in the first place. The "container" object should know that it was given a non-const version of "held" object and it should be able to give back a non-const version of the "held" object. As much as possible, const casting should be eliminated from the code, especially user code. Const casting is a source of defects in C++ programs and violates the flow of C++ programming (See Item 94 "Avoid casting away const" in the book "C++ Coding Standards").

The design of the "container" class using this class ConstNonconstObjectContainer that resolves the problem is:

// Implementation of container that uses the "runtime protection of const"
// to hold and give up the "held" object.
class Container {
public:
setNonconstHeld(const RCP<Held> &held)
{ held_ = held; }
setHeld(const RCP<const Held> &held)
{ held_ = held; }
RCP<const Held> getNonconstHeld()
{ return held_.getNonconstObj(); }
RCP<const Held> getHeld() const
{ return held_.getConstObj(); }
void doSomething() // The primary role!
{ stuff = held_->computeSomething(...); } // const interface of Held
private:
ConstNonconstObjectContainer<Held> held_;
};

Now the client code can be written with no const casting as:

void setUpContainer( const Ptr<Container> &container )
{
// A non-const version of Held
RCP<Held> myHeld = createNewHeld(...);
// Give my non-const RCP to Held now stored as a non-const object
container->setNonconstHeld(myHeld);
}
void updateContainer( const Ptr<Container> &container )
{
// Get back a non-const version of Held (No const cating!)
RCP<Held> myHeld = container->getNonconstHeld();
// Change Held
myHeld->changeSomething(...);
// Put back Held
container->setNonconstHeld(myHeld);
}

The "runtime protection of const" idiom allows you to write a single "container" class that can hold both non-const and const forms of a "held" object, protects the const of objects being set as const, and can give back non-const references to objects set as non-const. The price one pays for this is that the typical compile-time const protection provided by C++ is instead replaced with a runtime check. For example, the following code with thrown a NonconstAccessError exception object:

void fooThatThrows(const Ptr<Container> &container)
{
// A non-const version of Held
RCP<Held> myHeld = createNewHeld(...);
// Accidentally set a const version of Held
container->setHeld(myHeld);
// Try to get back a non-const version of Held
RCP<Held> myHeldAgain = container->getNonconstHeld(); // Throws NonconstAccessError!
}

These types of exceptions can be confuing to developers if they don't understand the idiom.

The alternative to the "runtime protection of const" idiom is to use compile-time protection. However, using compile-time const protection would require two different versions of a the "container" class: a "Container" class and a "ConstContainer" class. I will not go into detail about what these classes look like but this is ugly, more confusing, and hard to maintain.

Note that classes like RCP and boost:shared_ptr provide for compile-time protection of const with just one (template) class definition. RCP objects of type RCP<Held> allow non-const access while RCP objects of type RCP<const Held> only allow const access and protect const at compile time. How can one class like RCP protect const at compile-time while a class like Container shown above can't? The reason of course is that RCP<Held> and RCP<const Held> are realy two different C++ classes. The template mechanism in C++ made it easy to create these two different class types but they are two seperate types none the less.

Note that the "runtime protection of const" idiom using this ConstNonconstObjectContainer is not necessary when the "container" object needs a non-const "held" object to do its primary work. In this case, a client can't give a "container" object a non-const version of the "held" object because it could not even do its primary role. In cases where a non-const version of "held" is needed for the primary role, the "container" class can be written more simply without ConstNonconstObjectContainer as:

// Simpler implementation of "container" where a non-const version of the
// "held" object is needed to perform the primary role.
class Container {
public:
setHeld(const RCP<Held> &held)
{ held_ = held; }
RCP<const Held> getNonconstHeld()
{ return held_; }
RCP<const Held> getHeld() const
{ return held_.; }
void doSomething() // The primary role!
{ held_->changeSomething(...); } // non-const interface of Held
private:
RCP<Held> held_;
};

NOTE: The default copy constructor and assignment operator functions are allowed and result in shallow copy (i.e. just the RCP objects are copied). However, the protection of const will be maintained in the copied/assigned objects correctly.

NOTE: Assignment for an RCP<const ObjType> is also supported due to the implicit conversion from RCP<const ObjType> to ConstNonconstObjectContainer<ObjType> that this class supports through its constructor.

Definition at line 243 of file Teuchos_ConstNonconstObjectContainer.hpp.

Constructor & Destructor Documentation

template<class ObjType>
Teuchos::ConstNonconstObjectContainer< ObjType >::ConstNonconstObjectContainer ( )
inline

. Constructs to uninitialized

Definition at line 246 of file Teuchos_ConstNonconstObjectContainer.hpp.

template<class ObjType>
Teuchos::ConstNonconstObjectContainer< ObjType >::ConstNonconstObjectContainer ( const RCP< ObjType > &  obj)
inline

. Calls initialize() with a non-const object.

Definition at line 249 of file Teuchos_ConstNonconstObjectContainer.hpp.

template<class ObjType>
Teuchos::ConstNonconstObjectContainer< ObjType >::ConstNonconstObjectContainer ( const RCP< const ObjType > &  obj)
inline

. Calls initialize() with a const object.

Definition at line 252 of file Teuchos_ConstNonconstObjectContainer.hpp.

Member Function Documentation

template<class ObjType>
void Teuchos::ConstNonconstObjectContainer< ObjType >::initialize ( const RCP< ObjType > &  obj)
inline

. Initialize using a non-const object. Allows both const and non-const access to the contained object.

Definition at line 256 of file Teuchos_ConstNonconstObjectContainer.hpp.

template<class ObjType>
void Teuchos::ConstNonconstObjectContainer< ObjType >::initialize ( const RCP< const ObjType > &  obj)
inline

. Initialize using a const object. Allows only const access enforced with a runtime check.

Definition at line 264 of file Teuchos_ConstNonconstObjectContainer.hpp.

template<class ObjType>
void Teuchos::ConstNonconstObjectContainer< ObjType >::uninitialize ( )
inline

Uninitialize.

Definition at line 271 of file Teuchos_ConstNonconstObjectContainer.hpp.

template<class ObjType>
ConstNonconstObjectContainer<ObjType>& Teuchos::ConstNonconstObjectContainer< ObjType >::operator= ( ENull  )
inline

Assign to null.

Definition at line 274 of file Teuchos_ConstNonconstObjectContainer.hpp.

template<class ObjType>
bool Teuchos::ConstNonconstObjectContainer< ObjType >::isConst ( ) const
inline

Returns true if const-only access to the object is allowed.

Definition at line 277 of file Teuchos_ConstNonconstObjectContainer.hpp.

template<class ObjType>
RCP<ObjType> Teuchos::ConstNonconstObjectContainer< ObjType >::getNonconstObj ( ) const
inline

Get an RCP to the non-const contained object.

Preconditions:

Postconditions:

Definition at line 293 of file Teuchos_ConstNonconstObjectContainer.hpp.

template<class ObjType>
RCP<const ObjType> Teuchos::ConstNonconstObjectContainer< ObjType >::getConstObj ( ) const
inline

Get an RCP to the const contained object.

If return.get()==NULL, then this means that no object was given to *this data container object.

Definition at line 307 of file Teuchos_ConstNonconstObjectContainer.hpp.

template<class ObjType>
RCP<const ObjType> Teuchos::ConstNonconstObjectContainer< ObjType >::operator() ( ) const
inline

Perform shorthand for getConstObj().

Definition at line 310 of file Teuchos_ConstNonconstObjectContainer.hpp.

template<class ObjType>
const ObjType* Teuchos::ConstNonconstObjectContainer< ObjType >::operator-> ( ) const
inline

Pointer (->) access to underlying const object.

Preconditions:

Definition at line 318 of file Teuchos_ConstNonconstObjectContainer.hpp.

template<class ObjType>
const ObjType& Teuchos::ConstNonconstObjectContainer< ObjType >::operator* ( ) const
inline

Dereference the underlying object.

Preconditions:

Definition at line 326 of file Teuchos_ConstNonconstObjectContainer.hpp.

template<class ObjType>
Teuchos::ConstNonconstObjectContainer< ObjType >::operator RCP< const ObjType > ( ) const
inline

Perform an implicit conversion to an RCP<const ObjType>.

Definition at line 329 of file Teuchos_ConstNonconstObjectContainer.hpp.

Friends And Related Function Documentation

template<class T >
bool is_null ( const ConstNonconstObjectContainer< T > &  p)
related

Returns true if p.get()==NULL.

Definition at line 343 of file Teuchos_ConstNonconstObjectContainer.hpp.

template<class T >
bool nonnull ( const ConstNonconstObjectContainer< T > &  p)
related

Returns true if p.get()!=NULL.

Definition at line 352 of file Teuchos_ConstNonconstObjectContainer.hpp.

Member Data Documentation

template<class ObjType>
RCP<const ObjType> Teuchos::ConstNonconstObjectContainer< ObjType >::constObj_
private

Definition at line 333 of file Teuchos_ConstNonconstObjectContainer.hpp.

template<class ObjType>
bool Teuchos::ConstNonconstObjectContainer< ObjType >::isConst_
private

Definition at line 334 of file Teuchos_ConstNonconstObjectContainer.hpp.


The documentation for this class was generated from the following file: