2021-03-06 23:14:48 +08:00
|
|
|
// sol2
|
2017-12-20 17:58:32 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
// The MIT License (MIT)
|
2016-02-22 08:26:58 +08:00
|
|
|
|
2022-06-25 16:00:53 +08:00
|
|
|
// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
|
2016-02-22 08:26:58 +08:00
|
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
// this software and associated documentation files (the "Software"), to deal in
|
|
|
|
// the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
// subject to the following conditions:
|
|
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
#ifndef SOL_RAII_HPP
|
|
|
|
#define SOL_RAII_HPP
|
|
|
|
|
2020-08-10 05:17:06 +08:00
|
|
|
#include <sol/traits.hpp>
|
|
|
|
#include <sol/compatibility.hpp>
|
2016-02-22 08:26:58 +08:00
|
|
|
|
2017-08-22 03:25:43 +08:00
|
|
|
#include <memory>
|
|
|
|
|
2016-02-22 08:26:58 +08:00
|
|
|
namespace sol {
|
2016-06-20 05:59:40 +08:00
|
|
|
namespace detail {
|
|
|
|
struct default_construct {
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, typename... Args>
|
2016-06-20 05:59:40 +08:00
|
|
|
static void construct(T&& obj, Args&&... args) {
|
2017-12-07 21:24:50 +08:00
|
|
|
typedef meta::unqualified_t<T> Tu;
|
2020-12-17 04:25:35 +08:00
|
|
|
std::allocator<Tu> alloc {};
|
2018-07-30 01:11:21 +08:00
|
|
|
std::allocator_traits<std::allocator<Tu>>::construct(alloc, std::forward<T>(obj), std::forward<Args>(args)...);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, typename... Args>
|
2016-06-20 05:59:40 +08:00
|
|
|
void operator()(T&& obj, Args&&... args) const {
|
|
|
|
construct(std::forward<T>(obj), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-21 02:55:44 +08:00
|
|
|
struct default_destroy {
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T>
|
2016-06-20 05:59:40 +08:00
|
|
|
static void destroy(T&& obj) {
|
2020-12-17 04:25:35 +08:00
|
|
|
std::allocator<meta::unqualified_t<T>> alloc {};
|
2016-06-20 05:59:40 +08:00
|
|
|
alloc.destroy(obj);
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T>
|
2016-06-20 05:59:40 +08:00
|
|
|
void operator()(T&& obj) const {
|
|
|
|
destroy(std::forward<T>(obj));
|
|
|
|
}
|
|
|
|
};
|
2016-06-30 02:08:26 +08:00
|
|
|
|
|
|
|
struct deleter {
|
|
|
|
template <typename T>
|
|
|
|
void operator()(T* p) const {
|
|
|
|
delete p;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-10 21:40:37 +08:00
|
|
|
struct state_deleter {
|
|
|
|
void operator()(lua_State* L) const {
|
|
|
|
lua_close(L);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-30 02:08:26 +08:00
|
|
|
template <typename T, typename Dx, typename... Args>
|
|
|
|
inline std::unique_ptr<T, Dx> make_unique_deleter(Args&&... args) {
|
|
|
|
return std::unique_ptr<T, Dx>(new T(std::forward<Args>(args)...));
|
|
|
|
}
|
2016-07-08 04:52:39 +08:00
|
|
|
|
2016-09-14 12:02:15 +08:00
|
|
|
template <typename Tag, typename T>
|
2016-07-12 00:41:17 +08:00
|
|
|
struct tagged {
|
2019-04-29 04:45:40 +08:00
|
|
|
private:
|
|
|
|
T value_;
|
2020-12-17 04:25:35 +08:00
|
|
|
|
2019-04-29 04:45:40 +08:00
|
|
|
public:
|
2016-09-14 12:02:15 +08:00
|
|
|
template <typename Arg, typename... Args, meta::disable<std::is_same<meta::unqualified_t<Arg>, tagged>> = meta::enabler>
|
2020-12-17 04:25:35 +08:00
|
|
|
tagged(Arg&& arg, Args&&... args) : value_(std::forward<Arg>(arg), std::forward<Args>(args)...) {
|
2019-04-29 04:45:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
T& value() & {
|
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
T const& value() const& {
|
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
T&& value() && {
|
|
|
|
return std::move(value_);
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
2016-07-08 04:52:39 +08:00
|
|
|
};
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace detail
|
2016-06-20 05:59:40 +08:00
|
|
|
|
|
|
|
template <typename... Args>
|
2020-12-17 04:25:35 +08:00
|
|
|
struct constructor_list { };
|
2016-06-20 05:59:40 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename... Args>
|
2016-06-20 05:59:40 +08:00
|
|
|
using constructors = constructor_list<Args...>;
|
|
|
|
|
2020-12-17 04:25:35 +08:00
|
|
|
const auto default_constructor = constructors<types<>> {};
|
2016-06-20 05:59:40 +08:00
|
|
|
|
2020-12-17 04:25:35 +08:00
|
|
|
struct no_construction { };
|
|
|
|
const auto no_constructor = no_construction {};
|
2016-06-20 05:59:40 +08:00
|
|
|
|
2020-12-17 04:25:35 +08:00
|
|
|
struct call_construction { };
|
|
|
|
const auto call_constructor = call_construction {};
|
2016-06-20 05:59:40 +08:00
|
|
|
|
|
|
|
template <typename... Functions>
|
|
|
|
struct constructor_wrapper {
|
2016-09-14 12:02:15 +08:00
|
|
|
std::tuple<Functions...> functions;
|
|
|
|
template <typename Arg, typename... Args, meta::disable<std::is_same<meta::unqualified_t<Arg>, constructor_wrapper>> = meta::enabler>
|
2020-12-17 04:25:35 +08:00
|
|
|
constructor_wrapper(Arg&& arg, Args&&... args) : functions(std::forward<Arg>(arg), std::forward<Args>(args)...) {
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename... Functions>
|
|
|
|
inline auto initializers(Functions&&... functions) {
|
|
|
|
return constructor_wrapper<std::decay_t<Functions>...>(std::forward<Functions>(functions)...);
|
|
|
|
}
|
|
|
|
|
2016-08-17 13:16:44 +08:00
|
|
|
template <typename... Functions>
|
|
|
|
struct factory_wrapper {
|
2016-09-14 12:02:15 +08:00
|
|
|
std::tuple<Functions...> functions;
|
|
|
|
template <typename Arg, typename... Args, meta::disable<std::is_same<meta::unqualified_t<Arg>, factory_wrapper>> = meta::enabler>
|
2020-12-17 04:25:35 +08:00
|
|
|
factory_wrapper(Arg&& arg, Args&&... args) : functions(std::forward<Arg>(arg), std::forward<Args>(args)...) {
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
2016-08-17 13:16:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename... Functions>
|
|
|
|
inline auto factories(Functions&&... functions) {
|
|
|
|
return factory_wrapper<std::decay_t<Functions>...>(std::forward<Functions>(functions)...);
|
|
|
|
}
|
|
|
|
|
2016-06-20 05:59:40 +08:00
|
|
|
template <typename Function>
|
|
|
|
struct destructor_wrapper {
|
|
|
|
Function fx;
|
2020-12-17 04:25:35 +08:00
|
|
|
destructor_wrapper(Function f) : fx(std::move(f)) {
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2020-12-17 04:25:35 +08:00
|
|
|
struct destructor_wrapper<void> { };
|
2016-06-20 05:59:40 +08:00
|
|
|
|
2020-12-17 04:25:35 +08:00
|
|
|
const destructor_wrapper<void> default_destructor {};
|
2016-06-20 05:59:40 +08:00
|
|
|
|
|
|
|
template <typename Fx>
|
|
|
|
inline auto destructor(Fx&& fx) {
|
|
|
|
return destructor_wrapper<std::decay_t<Fx>>(std::forward<Fx>(fx));
|
|
|
|
}
|
2016-02-22 08:26:58 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace sol
|
2016-02-22 08:26:58 +08:00
|
|
|
|
|
|
|
#endif // SOL_RAII_HPP
|