Пример #1
0
    const reflectable_base* find_reflectable(const std::string& class_name) const {
        auto it = std::find_if(m_types.begin(), m_types.end(), [class_name](const auto& type) {
            return type->get_name() == class_name;
        });

        if (it == m_types.end())
            throw reflection_exception("Class " + class_name + " is not registered");

        return *it;
    }
Пример #2
0
    ReturnType _make_reflection(const std::string& class_name, const std::string& function_name, Params... params) {
        //! we first search for the reflectable object matching the class name
        auto it = std::find_if(m_types.begin(), m_types.end(), [class_name](const auto& type) {
            return type->get_name() == class_name;
        });

        //! if nothing is found, class is not registered
        if (it == m_types.end())
            throw reflection_exception("Class " + class_name + " is not registered as reflectable.");

        //! we ask the reflectable object for a callable<> object matching the function name
        //! get_function will throw a reflection_exception if function is not registered
        //!
        //! it also returns a generic std::shared_ptr<callable_base>
        //! we dynamically cast this pointer to a callable<> object templated on the expected function signature
        auto fct = std::dynamic_pointer_cast<callable<ReturnType(Params...)>>((*it)->get_function(function_name));

        //! if dynamic_cast failed, it means that the registered function has a different signature than the expected signature
        if (not fct)
            throw reflection_exception("invalid function signature for " + class_name + "::" + function_name);

        //! if everything is ok, we call the function
        return (*fct)(params...);
    }