Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_Handle.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TEUCHOS_HANDLE_HPP
11 #define TEUCHOS_HANDLE_HPP
12 
13 #include "Teuchos_ConfigDefs.hpp"
14 #include "Teuchos_RCP.hpp"
15 #include "Teuchos_Describable.hpp"
16 #include "Teuchos_Handleable.hpp"
17 
18 namespace Teuchos
19 {
20 
46  template <typename PointerType>
47  class ConstHandle : public virtual Describable
48  {
49  public:
51  ConstHandle(const RCP<const PointerType>& ptr) : ptr_(ptr) {;}
54  explicit ConstHandle(const ConstHandleable<PointerType>* ptr) : ptr_(ptr->getConstRcp()) {;}
56  const RCP<const PointerType>& constPtr() const {return ptr_;}
58  const PointerType * const rawPtr() {return this->constPtr().get();}
59  protected:
61  explicit ConstHandle() : ptr_() {;}
66  void setRcp(const RCP<PointerType>& ptr)
67  {ptr_=rcp_const_cast<const PointerType>(ptr);}
73  {return rcp_const_cast<PointerType>(ptr_);}
74  private:
77  };
78 
102  template <typename PointerType>
103  class Handle : public virtual ConstHandle<PointerType>
104  {
105  public:
108  : ConstHandle<PointerType>() {}
110  Handle(const RCP<PointerType>& smartPtr)
111  : ConstHandle<PointerType>()
112  {
113  /* \brief We need to set the rcp in the base class */
114  setRcp(smartPtr);
115  }
122  : ConstHandle<PointerType>()
123  {
124  /* \brief We need to set the rcp in the base class. */
125  setRcp(rawPtr->getRcp());
126  }
129  RCP<PointerType> ptr() const {return this->nonConstPtr();}
131  PointerType* rawPtr() const {return this->nonConstPtr().get();}
132  };
133 
134 } // namespace Teuchos
135 
147 #define TEUCHOS_HANDLE_CTORS(handle, contents) \
148 handle() : Teuchos::Handle<contents >() {;} \
149 handle(Teuchos::Handleable<contents >* rawPtr) : Teuchos::Handle<contents >(rawPtr) {;} \
150 handle(const Teuchos::RCP<contents >& smartPtr) : Teuchos::Handle<contents >(smartPtr){;}
151 
163 #define TEUCHOS_CONST_HANDLE_CTORS(handle, contents) \
164 handle( Teuchos::ENull _null = Teuchos::null ) : Teuchos::ConstHandle<contents >() {;} \
165 handle(const Teuchos::ConstHandleable<contents >* rawPtr) : Teuchos::ConstHandle<contents >(rawPtr) {;} \
166 handle(const Teuchos::RCP<const contents >& smartPtr) : Teuchos::ConstHandle<contents >(smartPtr){;}
167 
168 #endif // TEUCHOS_CONSTHANDLE_HPP
Templated handle class with strong const protection.
RCP< PointerType > nonConstPtr() const
Protected non-const 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.
Generic templated handle class.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
ConstHandle()
The empty ctor will only be called by Handle ctors.
T * get() const
Get the raw C++ pointer to the underlying object.
virtual RCP< Base > getRcp()=0
Return a safely-created RCP to the base type.
Class Handleable provides an abstract interface for polymorphic conversion from raw pointers to smart...
RCP< PointerType > ptr() const
Read/write access to the underlying smart pointer.
void setRcp(const RCP< PointerType > &ptr)
This function is needed in Handle ctors.
const PointerType *const rawPtr()
Access to raw pointer.
ConstHandle(const ConstHandleable< PointerType > *ptr)
Construct with a raw pointer to a ConstHandleable. This will make a call to rcp(), thus removing that call from the user interface.
ConstHandle(const RCP< const PointerType > &ptr)
Construct with an existing RCP.
Class ConstHandleable provides an abstract interface for polymorphic conversion from raw pointers to ...
Base class for all objects that can describe themselves.
PointerType * rawPtr() const
Access to non-const raw pointer.
const RCP< const PointerType > & constPtr() const
Read-only access to the underlying smart pointer.
Reference-counted pointer class and non-member templated function implementations.