#pragma once #include "IJsonCommandCreator.h" #include "CppCommon/Common/Basic/StringExtensions.h" #include #include #include namespace Incart::Net::WebApi { struct JsonCommandFactoryItem final { std::shared_ptr description; std::shared_ptr commandCreator; JsonCommandFactoryItem(std::shared_ptr description_, std::shared_ptr commandCreator_) : description(description_), commandCreator(commandCreator_) { } }; class JsonCommandFactory final { private: std::unordered_map> m_items; public: JsonCommandFactory() { } void add(std::shared_ptr description, std::shared_ptr creator) { m_items[description->name] = std::make_shared(description, creator); } std::vector getDescriptions() const { std::vector descriptions; for (auto it = m_items.begin(); it != m_items.end(); ++it) { descriptions.push_back(it->second->description->toString(0, true)); } return descriptions; } std::shared_ptr getDescription(const std::string& name) const { std::shared_ptr item = getItem(name); if (item == nullptr) { return nullptr; } return item->description; } std::shared_ptr getCreator(const std::string& name) const { std::shared_ptr item = getItem(name); if (item == nullptr) { return nullptr; } return item->commandCreator; } std::shared_ptr create(const std::string& name) const { std::shared_ptr item = getItem(name); if (item == nullptr) { return nullptr; } return item->commandCreator->create(); } private: std::shared_ptr getItem(const std::string& name) const { for (auto it = m_items.begin(); it != m_items.end(); it++) { if (Common::StringExtensions::isEqualsInsensitive(name, it->first)) { return it->second; } } return nullptr; } }; } // namespace Incart::Net::WebApi