Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
handle.hpp
Go to the documentation of this file.
1/*
2 HANDLE.hpp - opaque handle to an implementation entity, automatically managing lifecycle
3
4 Copyright (C)
5 2009, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12*/
13
34#ifndef LIB_HANDLE_H
35#define LIB_HANDLE_H
36
37#include "lib/nobug-init.hpp"
38
39#include <memory>
40#include <utility>
41
42
43namespace lib {
44
45 using std::shared_ptr;
46 using std::unique_ptr;
47 using std::weak_ptr;
48 using std::move;
49
50
51
62 template<class IMP>
63 class Handle
64 {
65 protected:
66 typedef std::shared_ptr<IMP> SmPtr;
67
69
70 public:
71
76 Handle() = default;
77
81 explicit
82 Handle (IMP* imp)
83 : smPtr_{imp}
84 { }
85
86 Handle (Handle const& r) = default;
87 Handle (Handle && rr) = default;
88 template<class Y> explicit Handle (shared_ptr<Y> const& r) : smPtr_{r} { }
89 template<class Y> explicit Handle (shared_ptr<Y> && srr) : smPtr_{move(srr)} { }
90 template<class Y> explicit Handle (weak_ptr<Y> const& wr) : smPtr_{wr} { }
91 template<class Y> explicit Handle (unique_ptr<Y> && urr) : smPtr_{move(urr)} { }
92
93 Handle& operator=(Handle const& r) = default;
94 Handle& operator=(Handle && rr) = default;
95 template<class Y> Handle& operator=(shared_ptr<Y> const& sr) { smPtr_ = sr; return *this; }
96 template<class Y> Handle& operator=(shared_ptr<Y> && srr) { smPtr_ = move(srr); return *this; }
97 template<class Y> Handle& operator=(unique_ptr<Y> && urr) { smPtr_ = move(urr); return *this; }
98
99
100 explicit operator bool() const { return bool(smPtr_); }
101 bool isValid() const { return bool(smPtr_); }
102
103
104
110 template<typename DEL>
111 Handle&
112 activate (IMP* impl, DEL whenDead)
113 {
114 smPtr_.reset (impl, whenDead);
115 return *this;
116 }
117
120 Handle&
121 activate(shared_ptr<IMP> const& impl)
122 {
123 smPtr_ = impl;
124 return *this;
125 }
126
127 Handle&
128 activate(shared_ptr<IMP> && impl)
129 {
130 smPtr_ = move (impl);
131 return *this;
132 }
133
140 void close ()
141 {
142 smPtr_.reset();
143 }
144
145
146
147 protected:
148 IMP&
149 impl() const
150 {
151 REQUIRE (smPtr_.get(), "Lifecycle-Error");
152 return *(smPtr_.get());
153 }
154 };
155
156
157} // namespace lib
158#endif
Generic opaque reference counting handle, for accessing a service and managing its lifecycle.
Definition handle.hpp:64
Handle(weak_ptr< Y > const &wr)
Definition handle.hpp:90
Handle(IMP *imp)
directly establish handle from an implementation, which typically way just heap allocated beforehand.
Definition handle.hpp:82
Handle(Handle const &r)=default
Handle(unique_ptr< Y > &&urr)
Definition handle.hpp:91
void close()
deactivate this handle, so it isn't tied any longer to the associated implementation or service objec...
Definition handle.hpp:140
bool isValid() const
Definition handle.hpp:101
IMP & impl() const
Definition handle.hpp:149
Handle & operator=(Handle &&rr)=default
SmPtr smPtr_
Definition handle.hpp:68
Handle(shared_ptr< Y > &&srr)
Definition handle.hpp:89
std::shared_ptr< IMP > SmPtr
Definition handle.hpp:66
Handle & operator=(unique_ptr< Y > &&urr)
Definition handle.hpp:97
Handle & operator=(shared_ptr< Y > &&srr)
Definition handle.hpp:96
Handle(Handle &&rr)=default
Handle & operator=(shared_ptr< Y > const &sr)
Definition handle.hpp:95
Handle()=default
by default create an Null handle.
Handle & activate(shared_ptr< IMP > &&impl)
Definition handle.hpp:128
Handle & operator=(Handle const &r)=default
Handle(shared_ptr< Y > const &r)
Definition handle.hpp:88
Handle & activate(IMP *impl, DEL whenDead)
Activation of the handle by the managing service.
Definition handle.hpp:112
Handle & activate(shared_ptr< IMP > const &impl)
another way of activating a handle by sharing ownership with an existing smart-ptr
Definition handle.hpp:121
Implementation namespace for support and library code.
Trigger the basic NoBug initialisation by placing a static variable.