/* * Registry.h, part of VCMI engine * * Authors: listed in file AUTHORS in main folder * * License: GNU General Public License v2.0 or later * Full text of license available in license.txt file, in main folder * */ #pragma once #include #include #define VCMI_REGISTER_SCRIPT_API(Type, Name) \ namespace\ {\ RegisterAPI _register ## Type (Name);\ }\ \ #define VCMI_REGISTER_CORE_SCRIPT_API(Type, Name) \ namespace\ {\ RegisterCoreAPI _register ## Type (Name);\ }\ \ VCMI_LIB_NAMESPACE_BEGIN namespace scripting { namespace api { class TypeRegistry; class Registar { public: virtual ~Registar() = default; virtual void pushMetatable(lua_State * L) const = 0; }; class Registry : public boost::noncopyable { public: using RegistryData = std::map>; static Registry * get(); const Registar * find(const std::string & name) const; void add(const std::string & name, std::shared_ptr item); void addCore(const std::string & name, std::shared_ptr item); const RegistryData & getCoreData() const { return coreData; } private: RegistryData data; RegistryData coreData; Registry(); }; template class RegisterAPI { public: RegisterAPI(const std::string & name) { auto r = std::make_shared(); Registry::get()->add(name, r); } }; template class RegisterCoreAPI { public: RegisterCoreAPI(const std::string & name) { auto r = std::make_shared(); Registry::get()->addCore(name, r); } }; class TypeRegistry : public boost::noncopyable { public: template const char * getKey() { return getKeyForType(typeid(T)); } static TypeRegistry * get(); private: size_t nextIndex; boost::mutex mutex; std::map keys; TypeRegistry(); const char * getKeyForType(const std::type_info & type); }; } } VCMI_LIB_NAMESPACE_END