Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_ConstNonconstObjectContainer.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_CONST_NONCONST_OBJECT_CONTAINER_HPP
11#define TEUCHOS_CONST_NONCONST_OBJECT_CONTAINER_HPP
12
13#include "Teuchos_RCP.hpp"
14
15
16namespace Teuchos {
17
18
242template<class ObjType>
244public:
247 :constObj_(null),isConst_(true) {}
256 void initialize( const RCP<ObjType> &obj )
257 {
259 constObj_ = obj;
260 isConst_ = false;
261 }
265 {
267 constObj_ = obj;
268 isConst_ = true;
269 }
272 { constObj_=null; isConst_=true; }
277 bool isConst() const
278 { return isConst_; }
294 {
296 constObj_.get() && isConst_, NonconstAccessError,
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(); }
331
332private:
333 RCP<const ObjType> constObj_;
334 bool isConst_;
335};
336
337
342template<class T>
344{ return is_null(p.getConstObj()); }
345
346
351template<class T>
353{ return nonnull(p.getConstObj()); }
354
355
356} // namespace Teuchos
357
358
359#endif // TEUCHOS_CONST_NONCONST_OBJECT_CONTAINER_HPP
Reference-counted pointer class and non-member templated function implementations.
Simple class supporting the "runtime protection of const" idiom.
const ObjType & operator*() const
Dereference the underlying object.
ConstNonconstObjectContainer< ObjType > & operator=(ENull)
RCP< const ObjType > operator()() const
Perform shorthand for getConstObj().
RCP< ObjType > getNonconstObj() const
Get an RCP to the non-const contained object.
bool is_null(const ConstNonconstObjectContainer< T > &p)
Returns true if p.get()==NULL.
ConstNonconstObjectContainer(const RCP< ObjType > &obj)
. Calls initialize() with a non-const object.
RCP< const ObjType > getConstObj() const
Get an RCP to the const contained object.
ConstNonconstObjectContainer(const RCP< const ObjType > &obj)
. Calls initialize() with a const object.
void initialize(const RCP< const ObjType > &obj)
. Initialize using a const object. Allows only const access enforced with a runtime check.
bool isConst() const
Returns true if const-only access to the object is allowed.
void initialize(const RCP< ObjType > &obj)
. Initialize using a non-const object. Allows both const and non-const access to the contained object...
const ObjType * operator->() const
Pointer (->) access to underlying const object.
bool nonnull(const ConstNonconstObjectContainer< T > &p)
Returns true if p.get()!=NULL.
Null reference error exception class.
Null reference error exception class.
Smart reference counting pointer class for automatic garbage collection.
T * get() const
Get the raw C++ pointer to the underlying object.
Default traits class that just returns typeid(T).name().
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...