// // VarVisitor.h // // Library: Foundation // Package: Dynamic // Module: VarVisitor // // Definition of the VarVisitor class. // // Copyright (c) 2023, Applied Informatics Software Engineering GmbH. // and Contributors. // // SPDX-License-Identifier: BSL-1.0 // #ifndef Foundation_VarVisitor_INCLUDED #define Foundation_VarVisitor_INCLUDED #include "Poco/Dynamic/Var.h" #include #include namespace Poco { namespace Details { #ifndef POCO_DOC struct TypeInfoHash { inline std::size_t operator()(std::type_info const& t) const { return t.hash_code(); } }; struct EqualRef { template bool operator()(std::reference_wrapper a, std::reference_wrapper b) const { return a.get() == b.get(); } }; using TypeInfoRef = std::reference_wrapper; using HandlerCaller = std::function; template using HandlerPointer = void (*)(const T&); template using Handler = std::function; #endif // POCO_DOC } // Details namespace Dynamic { class Foundation_API Visitor /// VarVisitor class. { std::unordered_map _handlers; public: template bool addHandler(const Details::Handler& f) /// Add handler for specific type T which holds in Var. /// This method is more safe, because it saves copy of handler : lambda or std::function. /// Returns true if handler was added. { auto result = _handlers.emplace(std::ref(typeid(T)), Details::HandlerCaller([handler = f](const Poco::Dynamic::Var& x) { handler(x.extract()); })); return result.second; } template bool addHandler(Details::HandlerPointer f) /// Add handler for specific type T which holds in Var. /// This method is less safe, because it saves only copy of function pointer. /// Returns true if handler was added. { auto result = _handlers.emplace(std::ref(typeid(T)), Details::HandlerCaller([handlerPointer = f](const Poco::Dynamic::Var& x) { handlerPointer(x.extract()); })); return result.second; } bool visit(const Poco::Dynamic::Var& x) const; /// Find handler for held type and if it exists call handler. /// Returns true if hanlder was found othrewise returns false. }; } } // namespace Poco::Dynamic #endif // Foundation_VarVisitor_INCLUDED