Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_Handle.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_HANDLE_HPP
43 #define TEUCHOS_HANDLE_HPP
44 
45 #include "Teuchos_ConfigDefs.hpp"
46 #include "Teuchos_RCP.hpp"
47 #include "Teuchos_Describable.hpp"
48 #include "Teuchos_Handleable.hpp"
49 
50 namespace Teuchos
51 {
52 
78  template <typename PointerType>
79  class ConstHandle : public virtual Describable
80  {
81  public:
83  ConstHandle(const RCP<const PointerType>& ptr) : ptr_(ptr) {;}
86  explicit ConstHandle(const ConstHandleable<PointerType>* ptr) : ptr_(ptr->getConstRcp()) {;}
88  const RCP<const PointerType>& constPtr() const {return ptr_;}
90  const PointerType * const rawPtr() {return this->constPtr().get();}
91  protected:
93  explicit ConstHandle() : ptr_() {;}
98  void setRcp(const RCP<PointerType>& ptr)
99  {ptr_=rcp_const_cast<const PointerType>(ptr);}
105  {return rcp_const_cast<PointerType>(ptr_);}
106  private:
109  };
110 
134  template <typename PointerType>
135  class Handle : public virtual ConstHandle<PointerType>
136  {
137  public:
140  : ConstHandle<PointerType>() {}
142  Handle(const RCP<PointerType>& smartPtr)
143  : ConstHandle<PointerType>()
144  {
145  /* \brief We need to set the rcp in the base class */
146  setRcp(smartPtr);
147  }
154  : ConstHandle<PointerType>()
155  {
156  /* \brief We need to set the rcp in the base class. */
157  setRcp(rawPtr->getRcp());
158  }
161  RCP<PointerType> ptr() const {return this->nonConstPtr();}
163  PointerType* rawPtr() const {return this->nonConstPtr().get();}
164  };
165 
166 } // namespace Teuchos
167 
179 #define TEUCHOS_HANDLE_CTORS(handle, contents) \
180 handle() : Teuchos::Handle<contents >() {;} \
181 handle(Teuchos::Handleable<contents >* rawPtr) : Teuchos::Handle<contents >(rawPtr) {;} \
182 handle(const Teuchos::RCP<contents >& smartPtr) : Teuchos::Handle<contents >(smartPtr){;}
183 
195 #define TEUCHOS_CONST_HANDLE_CTORS(handle, contents) \
196 handle( Teuchos::ENull _null = Teuchos::null ) : Teuchos::ConstHandle<contents >() {;} \
197 handle(const Teuchos::ConstHandleable<contents >* rawPtr) : Teuchos::ConstHandle<contents >(rawPtr) {;} \
198 handle(const Teuchos::RCP<const contents >& smartPtr) : Teuchos::ConstHandle<contents >(smartPtr){;}
199 
200 #endif // TEUCHOS_CONSTHANDLE_HPP
Teuchos header file which uses auto-configuration information to include necessary C++ headers.
Reference-counted pointer class and non-member templated function implementations.
Templated handle class with strong const protection.
RCP< PointerType > nonConstPtr() const
Protected non-const access to the underlying smart pointer.
void setRcp(const RCP< PointerType > &ptr)
This function is needed in Handle ctors.
ConstHandle(const ConstHandleable< PointerType > *ptr)
Construct with a raw pointer to a ConstHandleable. This will make a call to rcp(),...
const PointerType *const rawPtr()
Access to raw pointer.
ConstHandle(const RCP< const PointerType > &ptr)
Construct with an existing RCP.
const RCP< const PointerType > & constPtr() const
Read-only access to the underlying smart pointer.
ConstHandle()
The empty ctor will only be called by Handle ctors.
Class ConstHandleable provides an abstract interface for polymorphic conversion from raw pointers to ...
Base class for all objects that can describe themselves.
Generic templated handle class.
PointerType * rawPtr() const
Access to non-const raw pointer.
RCP< PointerType > ptr() const
Read/write access to the underlying smart pointer.
Handle(const RCP< PointerType > &smartPtr)
Construct with an existing RCP.
Handle(Handleable< PointerType > *rawPtr)
Construct with a raw pointer to a Handleable.
Class Handleable provides an abstract interface for polymorphic conversion from raw pointers to smart...
T * get() const
Get the raw C++ pointer to the underlying object.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...