// Copyright 2025-present the zvec project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #pragma once #include #include #include #include #include #include #include namespace zvec { namespace ailego { /*! Factory */ template class Factory { public: /*! Factory Register */ template ::value>::type> class Register { public: //! Constructor Register(const char *key) { Factory::Instance()->set(key, [] { return Register::Construct(); }); } //! Constructor template Register(const char *key, TArgs &&...args) { std::tuple tuple(std::forward(args)...); Factory::Instance()->set(key, [tuple] { return Register::Construct( tuple, typename TupleIndexMaker::Type()); }); } protected: //! Tuple Index Maker template struct TupleIndexMaker : TupleIndexMaker {}; //! Tuple Index template struct TupleIndex {}; //! Tuple Index Maker (special) template struct TupleIndexMaker<0, I...> { typedef TupleIndex Type; }; //! Construct a register object template static TImpl *Construct(const std::tuple &tuple, TupleIndex) { return new (std::nothrow) TImpl(std::get(tuple)...); } //! Construct a register object static TImpl *Construct(void) { return new (std::nothrow) TImpl(); } }; //! Produce an instance (c_ptr) static TBase *Make(const char *key) { return Factory::Instance()->produce(key); } //! Produce an instance (shared_ptr) static std::shared_ptr MakeShared(const char *key) { return std::shared_ptr(Factory::Make(key)); } //! Produce an instance (unique_ptr) static std::unique_ptr MakeUnique(const char *key) { return std::unique_ptr(Factory::Make(key)); } //! Test if the class is exist static bool Has(const char *key) { return Factory::Instance()->has(key); } //! Retrieve classes in factory static std::vector Classes(void) { return Factory::Instance()->classes(); } protected: //! Constructor Factory(void) : map_() {} //! Retrieve the singleton factory static Factory *Instance(void) { static Factory factory; return (&factory); } //! Inserts a new class into map template void set(const char *key, TFunc &&func) { map_[key] = std::forward(func); } //! Produce an instance TBase *produce(const char *key) { auto iter = map_.find(key); if (iter != map_.end()) { return iter->second(); } return nullptr; } //! Test if the class is exist bool has(const char *key) { return (map_.find(key) != map_.end()); } //! Retrieve classes in factory std::vector classes(void) const { std::vector vec; for (const auto &it : map_) { vec.push_back(std::string(it.first)); } return vec; } private: //! Disable them Factory(const Factory &); Factory(Factory &&); Factory &operator=(const Factory &); /*! Key Comparer */ struct KeyComparer { bool operator()(const char *lhs, const char *rhs) const { return (std::strcmp(lhs, rhs) < 0); } }; //! Don't use variable buffer as key store. //! The key must be use a static buffer to store. std::map, KeyComparer> map_; }; //! Factory Register #define AILEGO_FACTORY_REGISTER(__NAME__, __BASE__, __IMPL__, ...) \ static ailego::Factory<__BASE__>::Register<__IMPL__> \ __ailegoFactoryRegister_##__NAME__(#__NAME__, ##__VA_ARGS__) } // namespace ailego } // namespace zvec