Mir
module_deleter.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2015 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Andreas Pokorny <andreas.pokorny@canonical.com>
17  */
18 
19 #ifndef MIR_MODULE_DELETER_H_
20 #define MIR_MODULE_DELETER_H_
21 
22 #include <memory>
23 
24 namespace mir
25 {
26 class SharedLibrary;
27 
28 namespace detail
29 {
31 {
32 public:
33  RefCountedLibrary(void* address);
37 private:
38  std::shared_ptr<mir::SharedLibrary> internal_state;
39 };
40 }
41 
42 template<typename T>
43 struct ModuleDeleter : std::default_delete<T>
44 {
45  ModuleDeleter() : library(nullptr) {}
46  template<typename U>
48  : std::default_delete<T>{other},
49  library{other.get_library()}
50  {
51  }
52 
53  detail::RefCountedLibrary get_library() const
54  {
55  return library;
56  }
57 
58 protected:
59  ModuleDeleter(void *address_in_module)
60  : library{address_in_module}
61  {
62  }
63 private:
65 };
66 
82 template<typename T>
83 using UniqueModulePtr = std::unique_ptr<T,ModuleDeleter<T>>;
84 
85 namespace
86 {
90 template<typename Type, typename... Args>
91 inline auto make_module_ptr(Args&&... args)
93 {
94  struct Deleter : ModuleDeleter<Type>
95  {
96  Deleter(void* address)
97  : ModuleDeleter<Type>(address) {}
98  } deleter(reinterpret_cast<void*>(&make_module_ptr<Type, Args...>));
99 
100  return UniqueModulePtr<Type>(new Type(std::forward<Args>(args)...), std::move(deleter));
101 }
102 }
103 
104 }
105 #endif
Definition: as_render_target.h:27
ModuleDeleter(ModuleDeleter< U > const &other)
Definition: module_deleter.h:47
Definition: module_deleter.h:43
STL namespace.
RefCountedLibrary & operator=(RefCountedLibrary const &)
std::unique_ptr< T, ModuleDeleter< T >> UniqueModulePtr
Use UniqueModulePtr to ensure that your loadable libray outlives instances created within it...
Definition: module_deleter.h:83
Definition: module_deleter.h:30
ModuleDeleter()
Definition: module_deleter.h:45

Copyright © 2012-2016 Canonical Ltd.
Generated on Mon Jun 5 11:05:03 UTC 2017