/************************************************************************* * Copyright (C) [2019] by Cambricon, Inc. All rights reserved * * 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 * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. *************************************************************************/ #ifndef MODULES_INFERENCE_INCLUDE_REFLEX_OBJECT_H_ #define MODULES_INFERENCE_INCLUDE_REFLEX_OBJECT_H_ #include #include #include #include #define DECLARE_REFLEX_OBJECT(Class) \ public: \ static cnstream::ClassInfo sclass_info; \ \ protected: \ const cnstream::ClassInfo& class_info() const; #define IMPLEMENT_REFLEX_OBJECT(Class) \ cnstream::ClassInfo Class::sclass_info(std::string(#Class), \ cnstream::ObjectConstructor([]() { \ return reinterpret_cast(new Class); \ }), \ true); \ const cnstream::ClassInfo& Class::class_info() const { return sclass_info; } #define DECLARE_REFLEX_OBJECT_EX(Class, BaseType) \ public: \ static cnstream::ClassInfo sclass_info; \ \ protected: \ const cnstream::ClassInfo& class_info() const; #define IMPLEMENT_REFLEX_OBJECT_EX(Class, BaseType) \ cnstream::ClassInfo Class::sclass_info( \ std::string(#Class), \ cnstream::ObjectConstructor([]() { return reinterpret_cast(new Class); }), true); \ const cnstream::ClassInfo& Class::class_info() const { return sclass_info; } namespace cnstream { /***************************************** * [T]: The return type for reflection object. *****************************************/ template using ObjectConstructor = std::function; template class ClassInfo { public: ClassInfo(const std::string& name, const ObjectConstructor& constructor, bool regist = false); T* CreateObject() const; std::string name() const; bool Register() const; const ObjectConstructor& constructor() const; private: std::string name_; ObjectConstructor constructor_; }; // class classinfo class ReflexObject { public: static ReflexObject* CreateObject(const std::string& name); static bool Register(const ClassInfo& info); virtual ~ReflexObject() = 0; #ifdef UNIT_TEST static void Remove(const std::string& name); #endif }; // class reflexobject template class ReflexObjectEx : public ReflexObject { public: static T* CreateObject(const std::string& name); static bool Register(const ClassInfo& info); virtual ~ReflexObjectEx() = 0; }; // class reflectobject template ClassInfo::ClassInfo(const std::string& name, const ObjectConstructor& constructor, bool regist) : name_(name), constructor_(constructor) { if (regist) { Register(); } } template inline std::string ClassInfo::name() const { return name_; } template inline const ObjectConstructor& ClassInfo::constructor() const { return constructor_; } template inline bool ClassInfo::Register() const { return ReflexObjectEx::Register(*this); } template T* ClassInfo::CreateObject() const { if (NULL != constructor_) { return constructor_(); } return nullptr; } template T* ReflexObjectEx::CreateObject(const std::string& name) { auto ptr = ReflexObject::CreateObject(name); if (nullptr == ptr) return nullptr; T* ret = dynamic_cast(ptr); if (nullptr == ret) { delete ptr; return nullptr; } return ret; } template bool ReflexObjectEx::Register(const ClassInfo& info) { // build base ClassInfo(ClassInfo) ObjectConstructor base_constructor = NULL; if (info.constructor() != NULL) { base_constructor = [info]() { return static_cast(info.constructor()()); }; } ClassInfo base_info(info.name(), base_constructor); return ReflexObject::Register(base_info); } template ReflexObjectEx::~ReflexObjectEx() {} } // namespace cnstream #endif // MODULES_INFERENCE_INCLUDE_REFLEX_OBJECT_HPP_