/************************************************************************************ * * * Copyright (c) 2014 - 2018 Axel Menzel * * * * This file is part of RTTR (Run Time Type Reflection) * * License: MIT License * * * * 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 RTTR_PROPERTY_WRAPPER_FUNC_H_ #define RTTR_PROPERTY_WRAPPER_FUNC_H_ ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// // global function getter/setter - function pointer template class property_wrapper : public property_wrapper_base, public metadata_handler { using return_type = typename function_traits::return_type; using arg_type = typename param_types::type; public: property_wrapper(string_view name, Getter get, Setter set, std::array metadata_list) RTTR_NOEXCEPT : property_wrapper_base(name, type::get()), metadata_handler(std::move(metadata_list)), m_getter(get), m_setter(set) { static_assert(function_traits::arg_count == 0, "Invalid number of argument, please provide a getter-function without arguments."); static_assert(function_traits::arg_count == 1, "Invalid number of argument, please provide a setter-function with exactly one argument."); static_assert(std::is_same::value, "Please provide the same signature for getter and setter!"); init(); } access_levels get_access_level() const RTTR_NOEXCEPT { return Acc_Level; } bool is_valid() const RTTR_NOEXCEPT { return true; } bool is_readonly() const RTTR_NOEXCEPT { return false; } bool is_static() const RTTR_NOEXCEPT { return true; } type get_type() const RTTR_NOEXCEPT { return type::get(); } variant get_metadata(const variant& key) const { return metadata_handler::get_metadata(key); } bool set_value(instance& object, argument& arg) const { if (arg.is_type()) { m_setter(arg.get_value()); return true; } return false; } variant get_value(instance& object) const { return variant(m_getter()); } void visit(visitor& visitor, property prop) const RTTR_NOEXCEPT { auto obj = make_property_getter_setter_info(prop, m_getter, m_setter); visitor_iterator::visit(visitor, make_property_getter_setter_visitor_invoker(obj)); } private: Getter m_getter; Setter m_setter; }; ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// // global function getter template class property_wrapper : public property_wrapper_base, public metadata_handler { using return_type = typename function_traits::return_type; public: property_wrapper(string_view name, Getter get, std::array metadata_list) RTTR_NOEXCEPT : property_wrapper_base(name, type::get()), metadata_handler(std::move(metadata_list)), m_accessor(get) { static_assert(function_traits::arg_count == 0, "Invalid number of argument, please provide a getter-function without arguments."); init(); } access_levels get_access_level() const RTTR_NOEXCEPT { return Acc_Level; } bool is_valid() const RTTR_NOEXCEPT { return true; } bool is_readonly() const RTTR_NOEXCEPT { return true; } bool is_static() const RTTR_NOEXCEPT { return true; } type get_type() const RTTR_NOEXCEPT { return type::get(); } bool is_array() const RTTR_NOEXCEPT { return std::is_array::value; } variant get_metadata(const variant& key) const { return metadata_handler::get_metadata(key); } bool set_value(instance& object, argument& arg) const { return false; } variant get_value(instance& object) const { return (variant(m_accessor())); } void visit(visitor& visitor, property prop) const RTTR_NOEXCEPT { auto obj = make_property_info(prop, m_accessor); visitor_iterator::visit(visitor, make_property_visitor_invoker(obj)); } private: Getter m_accessor; }; ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// // global function getter/setter template class property_wrapper : public property_wrapper_base, public metadata_handler { using return_type = typename function_traits::return_type; using arg_type = typename param_types::type; public: property_wrapper(string_view name, Getter get, Setter set, std::array metadata_list) RTTR_NOEXCEPT : property_wrapper_base(name, type::get()), metadata_handler(std::move(metadata_list)), m_getter(get), m_setter(set) { static_assert(std::is_reference::value, "Please provide a getter-function with a reference as return value!"); static_assert(std::is_reference::value, "Please provide a setter-function with a reference as argument!"); static_assert(function_traits::arg_count == 0, "Invalid number of argument, please provide a getter-function without arguments."); static_assert(function_traits::arg_count == 1, "Invalid number of argument, please provide a setter-function with exactly one argument."); static_assert(std::is_same::value, "Please provide the same signature for getter and setter!"); init(); } access_levels get_access_level() const RTTR_NOEXCEPT { return Acc_Level; } bool is_valid() const RTTR_NOEXCEPT { return true; } bool is_readonly() const RTTR_NOEXCEPT { return false; } bool is_static() const RTTR_NOEXCEPT { return true; } type get_type() const RTTR_NOEXCEPT { return type::get::type*>(); } variant get_metadata(const variant& key) const { return metadata_handler::get_metadata(key); } bool set_value(instance& object, argument& arg) const { using arg_type_t = remove_reference_t; if (arg.is_type()) { m_setter(*arg.get_value()); return true; } return false; } variant get_value(instance& object) const { return variant(&(m_getter())); } void visit(visitor& visitor, property prop) const RTTR_NOEXCEPT { auto obj = make_property_getter_setter_info(prop, m_getter, m_setter); visitor_iterator::visit(visitor, make_property_getter_setter_visitor_invoker(obj)); } private: Getter m_getter; Setter m_setter; }; ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// // global function getter template class property_wrapper : public property_wrapper_base, public metadata_handler { using return_type = typename function_traits::return_type; public: property_wrapper(string_view name, Getter get, std::array metadata_list) RTTR_NOEXCEPT : property_wrapper_base(name, type::get()), metadata_handler(std::move(metadata_list)), m_accessor(get) { static_assert(std::is_reference::value, "Please provide a function with a reference as return value!"); init(); } access_levels get_access_level() const RTTR_NOEXCEPT { return Acc_Level; } bool is_valid() const RTTR_NOEXCEPT { return true; } bool is_readonly() const RTTR_NOEXCEPT { return true; } bool is_static() const RTTR_NOEXCEPT { return true; } type get_type() const RTTR_NOEXCEPT { return type::get::type>::type*>(); } variant get_metadata(const variant& key) const { return metadata_handler::get_metadata(key); } bool set_value(instance& object, argument& arg) const { return false; } variant get_value(instance& object) const { return (variant(const_cast::type*>(&(m_accessor())))); } void visit(visitor& visitor, property prop) const RTTR_NOEXCEPT { auto obj = make_property_info(prop, m_accessor); visitor_iterator::visit(visitor, make_property_visitor_invoker(obj)); } private: Getter m_accessor; }; ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// // global function getter/setter template class property_wrapper : public property_wrapper_base, public metadata_handler { using return_type = typename function_traits::return_type; using arg_type = typename param_types::type; public: property_wrapper(string_view name, Getter get, Setter set, std::array metadata_list) RTTR_NOEXCEPT : property_wrapper_base(name, type::get()), metadata_handler(std::move(metadata_list)), m_getter(get), m_setter(set) { static_assert(std::is_reference::value, "Please provide a getter-function with a reference as return value!"); static_assert(std::is_reference::value, "Please provide a setter-function with a reference as argument!"); static_assert(function_traits::arg_count == 0, "Invalid number of argument, please provide a getter-function without arguments."); static_assert(function_traits::arg_count == 1, "Invalid number of argument, please provide a setter-function with exactly one argument."); static_assert(std::is_same::value, "Please provide the same signature for getter and setter!"); init(); } access_levels get_access_level() const RTTR_NOEXCEPT { return Acc_Level; } bool is_valid() const RTTR_NOEXCEPT { return true; } bool is_readonly() const RTTR_NOEXCEPT { return false; } bool is_static() const RTTR_NOEXCEPT { return true; } type get_type() const RTTR_NOEXCEPT { return type::get< std::reference_wrapper> >(); } variant get_metadata(const variant& key) const { return metadata_handler::get_metadata(key); } bool set_value(instance& object, argument& arg) const { using arg_type_t = remove_reference_t; if (arg.is_type>()) { m_setter(arg.get_value>().get()); return true; } return false; } variant get_value(instance& object) const { return variant(std::ref(m_getter())); } void visit(visitor& visitor, property prop) const RTTR_NOEXCEPT { auto obj = make_property_getter_setter_info(prop, m_getter, m_setter); visitor_iterator::visit(visitor, make_property_getter_setter_visitor_invoker(obj)); } private: Getter m_getter; Setter m_setter; }; ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// // global function getter template class property_wrapper : public property_wrapper_base, public metadata_handler { using return_type = typename function_traits::return_type; using policy_type = std::reference_wrapper>>; public: property_wrapper(string_view name, Getter get, std::array metadata_list) RTTR_NOEXCEPT : property_wrapper_base(name, type::get()), metadata_handler(std::move(metadata_list)), m_accessor(get) { static_assert(std::is_reference::value, "Please provide a function with a reference as return value!"); init(); } access_levels get_access_level() const RTTR_NOEXCEPT { return Acc_Level; } bool is_valid() const RTTR_NOEXCEPT { return true; } bool is_readonly() const RTTR_NOEXCEPT { return true; } bool is_static() const RTTR_NOEXCEPT { return true; } type get_type() const RTTR_NOEXCEPT { return type::get(); } variant get_metadata(const variant& key) const { return metadata_handler::get_metadata(key); } bool set_value(instance& object, argument& arg) const { return false; } variant get_value(instance& object) const { return variant(std::cref(m_accessor())); } void visit(visitor& visitor, property prop) const RTTR_NOEXCEPT { auto obj = make_property_info(prop, m_accessor); visitor_iterator::visit(visitor, make_property_visitor_invoker(obj)); } private: Getter m_accessor; }; #endif // RTTR_PROPERTY_WRAPPER_FUNC_H_