2
* Copyright © 2015 Canonical Ltd.
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.
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.
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/>.
16
* Authored by: Andreas Pokorny <andreas.pokorny@canonical.com>
19
#ifndef MIR_MODULE_DELETER_H_
20
#define MIR_MODULE_DELETER_H_
30
class RefCountedLibrary
33
RefCountedLibrary(void* adrress);
34
RefCountedLibrary(RefCountedLibrary const&);
36
RefCountedLibrary& operator=(RefCountedLibrary const&);
38
std::shared_ptr<mir::SharedLibrary> internal_state;
49
* \brief Use UniqueModulePtr to ensure that your loadable libray outlives
50
* instances created within it.
52
* Use mir::make_module_ptr(...) or pass a function from your library to the
53
* constructor, to increase the lifetime of your library:
55
* mir::UniqueModulePtr<ExampleInterface> library_entry_point()
57
* return mir::UniqueModulePtr<SomeInterface>(new Implementation, &library_entry_point);
61
* The default constructor will not try to infer the dynamic library.
64
using UniqueModulePtr = std::unique_ptr<T,ModuleDeleter<T>>;
69
* \brief make_unique like creation function for UniqueModulePtr
71
template<typename Type, typename... Args>
72
inline auto make_module_ptr(Args&&... args)
73
-> UniqueModulePtr<Type>
75
return UniqueModulePtr<Type>(new Type(std::forward<Args>(args)...), &make_module_ptr<Type, Args...>);
79
struct ModuleDeleter : std::default_delete<T>
81
ModuleDeleter() : library(nullptr) {}
83
ModuleDeleter(ModuleDeleter<U> const& other)
84
: std::default_delete<T>{other},
85
library{other.get_library()}
89
detail::RefCountedLibrary get_library() const
94
template<typename R, typename... Rs>
95
ModuleDeleter(R (*function)(Rs...))
96
: library{reinterpret_cast<void*>(function)}
100
detail::RefCountedLibrary library;
101
template<typename Type, typename... Args>
102
friend std::unique_ptr<Type, ModuleDeleter<Type>> make_module_ptr(Args&&... args);