Teuchos - Trilinos Tools Package
Version of the Day
|
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... | |
int | count () const |
Return the internal count. More... | |
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... | |
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:
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:
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:
Now the client code can be written with no const casting as:
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:
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:
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 275 of file Teuchos_ConstNonconstObjectContainer.hpp.
|
inline |
. Constructs to uninitialized
Definition at line 278 of file Teuchos_ConstNonconstObjectContainer.hpp.
|
inline |
. Calls initialize()
with a non-const object.
Definition at line 281 of file Teuchos_ConstNonconstObjectContainer.hpp.
|
inline |
. Calls initialize()
with a const object.
Definition at line 284 of file Teuchos_ConstNonconstObjectContainer.hpp.
|
inline |
. Initialize using a non-const object. Allows both const and non-const access to the contained object.
Definition at line 288 of file Teuchos_ConstNonconstObjectContainer.hpp.
|
inline |
. Initialize using a const object. Allows only const access enforced with a runtime check.
Definition at line 296 of file Teuchos_ConstNonconstObjectContainer.hpp.
|
inline |
Uninitialize.
Definition at line 303 of file Teuchos_ConstNonconstObjectContainer.hpp.
|
inline |
Assign to null.
Definition at line 306 of file Teuchos_ConstNonconstObjectContainer.hpp.
|
inline |
Returns true if const-only access to the object is allowed.
Definition at line 309 of file Teuchos_ConstNonconstObjectContainer.hpp.
|
inline |
Get an RCP to the non-const contained object.
Preconditions:
getConstObj().get()!=NULL
] isConst()==false
(throws NonconstAccessError
) Postconditions:
getConstObj().get()==NULL
] return.get()==NULL
getConstObj().get()!=NULL
] return.get()!=NULL
Definition at line 325 of file Teuchos_ConstNonconstObjectContainer.hpp.
|
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 339 of file Teuchos_ConstNonconstObjectContainer.hpp.
|
inline |
Perform shorthand for getConstObj().
Definition at line 342 of file Teuchos_ConstNonconstObjectContainer.hpp.
|
inline |
Pointer (->
) access to underlying const object.
Preconditions:
this->get() != NULL
(throws NullReferenceError
) Definition at line 350 of file Teuchos_ConstNonconstObjectContainer.hpp.
|
inline |
Dereference the underlying object.
Preconditions:
this->get() != NULL
(throws NullReferenceError
) Definition at line 358 of file Teuchos_ConstNonconstObjectContainer.hpp.
|
inline |
Perform an implicit conversion to an RCP<const ObjType>.
Definition at line 361 of file Teuchos_ConstNonconstObjectContainer.hpp.
|
inline |
Return the internal count.
Definition at line 364 of file Teuchos_ConstNonconstObjectContainer.hpp.
|
related |
Returns true if p.get()==NULL
.
Definition at line 378 of file Teuchos_ConstNonconstObjectContainer.hpp.
|
related |
Returns true if p.get()!=NULL
.
Definition at line 387 of file Teuchos_ConstNonconstObjectContainer.hpp.