Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_ConstNonconstObjectContainer.hpp
Go to the documentation of this file.
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_CONST_NONCONST_OBJECT_CONTAINER_HPP
11 #define TEUCHOS_CONST_NONCONST_OBJECT_CONTAINER_HPP
12 
13 #include "Teuchos_RCP.hpp"
14 
15 
16 namespace Teuchos {
17 
18 
242 template<class ObjType>
244 public:
247  :constObj_(null),isConst_(true) {}
250  { initialize(obj); }
253  { initialize(obj); }
256  void initialize( const RCP<ObjType> &obj )
257  {
259  constObj_ = obj;
260  isConst_ = false;
261  }
264  void initialize( const RCP<const ObjType> &obj )
265  {
267  constObj_ = obj;
268  isConst_ = true;
269  }
272  { constObj_=null; isConst_=true; }
275  { uninitialize(); return *this; }
277  bool isConst() const
278  { return isConst_; }
294  {
297  "Error, the object of reference type \""<<TypeNameTraits<ObjType>::name()
298  <<"\" was given as a const-only object and non-const access is not allowed."
299  );
300  return rcp_const_cast<ObjType>(constObj_);
301  }
308  { return constObj_; }
311  { return getConstObj(); }
318  const ObjType* operator->() const
319  { return &*getConstObj(); } // Does assert also!
326  const ObjType& operator*() const
327  { return *getConstObj(); }
329  operator RCP<const ObjType>() const
330  { return getConstObj(); }
332  int count() const
333  { return constObj_.count(); }
334 
335 private:
337  bool isConst_;
338 };
339 
340 
345 template<class T>
347 { return is_null(p.getConstObj()); }
348 
349 
354 template<class T>
356 { return nonnull(p.getConstObj()); }
357 
358 
359 } // namespace Teuchos
360 
361 
362 #endif // TEUCHOS_CONST_NONCONST_OBJECT_CONTAINER_HPP
Null reference error exception class.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
void initialize(const RCP< ObjType > &obj)
. Initialize using a non-const object. Allows both const and non-const access to the contained object...
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
ConstNonconstObjectContainer(const RCP< ObjType > &obj)
. Calls initialize() with a non-const object.
bool isConst() const
Returns true if const-only access to the object is allowed.
T * get() const
Get the raw C++ pointer to the underlying object.
ENull
Used to initialize a RCP object to NULL using an implicit conversion!
ConstNonconstObjectContainer(const RCP< const ObjType > &obj)
. Calls initialize() with a const object.
RCP< const ObjType > getConstObj() const
Get an RCP to the const contained object.
void initialize(const RCP< const ObjType > &obj)
. Initialize using a const object. Allows only const access enforced with a runtime check...
const ObjType * operator->() const
Pointer (-&gt;) access to underlying const object.
ConstNonconstObjectContainer< ObjType > & operator=(ENull)
bool nonnull(const ConstNonconstObjectContainer< T > &p)
Returns true if p.get()!=NULL.
bool is_null(const ConstNonconstObjectContainer< T > &p)
Returns true if p.get()==NULL.
Simple class supporting the &quot;runtime protection of const&quot; idiom.
Smart reference counting pointer class for automatic garbage collection.
RCP< ObjType > getNonconstObj() const
Get an RCP to the non-const contained object.
RCP< const ObjType > operator()() const
Perform shorthand for getConstObj().
Reference-counted pointer class and non-member templated function implementations.
const ObjType & operator*() const
Dereference the underlying object.
Null reference error exception class.