Lumiera  0.pre.03
»edit your freedom«
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) Lumiera.org
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
43 #ifndef LIB_HANDLE_H
44 #define LIB_HANDLE_H
45 
46 #include "lib/nobug-init.hpp"
47 
48 #include <memory>
49 #include <utility>
50 
51 
52 namespace lib {
53 
54  using std::shared_ptr;
55  using std::unique_ptr;
56  using std::weak_ptr;
57  using std::move;
58 
59 
60 
71  template<class IMP>
72  class Handle
73  {
74  protected:
76 
77  SmPtr smPtr_;
78 
79  public:
80 
85  Handle() = default;
86 
90  explicit
91  Handle (IMP* imp)
92  : smPtr_{imp}
93  { }
94 
95  Handle (Handle const& r) = default;
96  Handle (Handle && rr) = default;
97  template<class Y> explicit Handle (shared_ptr<Y> const& r) : smPtr_{r} { }
98  template<class Y> explicit Handle (shared_ptr<Y> && srr) : smPtr_{move(srr)} { }
99  template<class Y> explicit Handle (weak_ptr<Y> const& wr) : smPtr_{wr} { }
100  template<class Y> explicit Handle (unique_ptr<Y> && urr) : smPtr_{move(urr)} { }
101 
102  Handle& operator=(Handle const& r) = default;
103  Handle& operator=(Handle && rr) = default;
104  template<class Y> Handle& operator=(shared_ptr<Y> const& sr) { smPtr_ = sr; return *this; }
105  template<class Y> Handle& operator=(shared_ptr<Y> && srr) { smPtr_ = move(srr); return *this; }
106  template<class Y> Handle& operator=(unique_ptr<Y> && urr) { smPtr_ = move(urr); return *this; }
107 
108 
109  explicit operator bool() const { return bool(smPtr_); }
110  bool isValid() const { return bool(smPtr_); }
111 
112 
113 
119  template<typename DEL>
120  Handle&
121  activate (IMP* impl, DEL whenDead)
122  {
123  smPtr_.reset (impl, whenDead);
124  return *this;
125  }
126 
129  Handle&
131  {
132  smPtr_ = impl;
133  return *this;
134  }
135 
136  Handle&
137  activate(shared_ptr<IMP> && impl)
138  {
139  smPtr_ = move (impl);
140  return *this;
141  }
142 
149  void close ()
150  {
151  smPtr_.reset();
152  }
153 
154 
155 
156  protected:
157  IMP&
158  impl() const
159  {
160  REQUIRE (smPtr_.get(), "Lifecycle-Error");
161  return *(smPtr_.get());
162  }
163  };
164 
165 
166 } // namespace lib
167 #endif
Generic opaque reference counting handle, for accessing a service and managing its lifecycle...
Definition: handle.hpp:72
Handle & activate(shared_ptr< IMP > const &impl)
another way of activating a handle by sharing ownership with an existing smart-ptr ...
Definition: handle.hpp:130
Trigger the basic NoBug initialisation by placing a static variable.
Implementation namespace for support and library code.
Handle()=default
by default create an Null handle.
Handle(IMP *imp)
directly establish handle from an implementation, which typically way just heap allocated beforehand...
Definition: handle.hpp:91
void close()
deactivate this handle, so it isn&#39;t tied any longer to the associated implementation or service objec...
Definition: handle.hpp:149
Handle & activate(IMP *impl, DEL whenDead)
Activation of the handle by the managing service.
Definition: handle.hpp:121