Templatized handle class that wraps a Boost shared pointer and behaves like a reference.
More...
|
| handle (T *held_=nullptr) |
| A constructor from a pointer to some on-the-fly allocated instance. More...
|
|
template<typename Q , typename = std::enable_if< std::is_same<const Q, T>::value && std::is_const<T>::value && !std::is_const<Q>::value>> |
| handle (Q *held=nullptr) |
| A constructor from a pointer to some on-the-fly allocated instance. More...
|
|
| handle (const boost::shared_ptr< T > &sp_) |
| Constructor on a boost shared_ptr. More...
|
|
virtual | ~handle () |
| Destructor. More...
|
|
bool | unique () const |
| Check if the current handle holds an uniquely referenced object. More...
|
|
bool | has_data () const |
| Return true if the internal shared pointer holds something. More...
|
|
| operator bool () const |
| Return true if the internal shared pointer holds something. More...
|
|
void | swap (handle< T > &other_) |
|
const T & | get () const |
|
template<typename Q = T> |
std::enable_if< std::is_same< Q, T >::value &&!std::is_const< Q >::value &&!std::is_const< T >::value, Q & >::type | grab () |
|
T * | operator-> () |
|
T const * | operator-> () const |
|
T & | operator * () |
|
T const & | operator * () const |
|
void | reset (T *elem_=nullptr) |
| Reset the internal shared pointer with a new instance. More...
|
|
template<typename Q = T> |
std::enable_if< std::is_same< Q, T >::value &&!std::is_const< Q >::value &&!std::is_const< T >::value, handle< const Q > >::type | to_const () const |
| Return a handle instance that hosts the const instance. More...
|
|
template<typename T>
class datatools::handle< T >
Templatized handle class that wraps a Boost shared pointer and behaves like a reference.
A handle object is given the responsability to handle a class instance through its pointer using the shared pointer mechanism from Boost. The inner hidden pointer can be null, but accessors will throw exceptions if called when this is the case. The held instance must be initialized with the 'new' construction operator, or you can use the make_handle() free function.
Given handle
and held type T
(no additional qualifiers), an instance of handle<T>
has the following access behaviour in different const
contexts:
handle<T>
: all members of handle
and T
accessible
const handle<T>
: can only access const members of both
const handle<const T>
: can only access const members of both
handle<const T>
: can access all methods of handle, only const
members of T
Access attempts outside these contexts will give a compile-time error.
The handle class is copyable and can be used within STL containers.
Example:
{
cout <<
"h0 = " << h0.
get () << endl;
{
h2.grab () = 666;
}
cout << "h1 = " << h1.get () << endl;
auto h3 = make_handle<std::string>("foo");
cout << (*h3) << endl;
cout << h3->size() << endl;
h3.reset();
h3->size();
};
- See also
- make_handle()