久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

c ++派生類型的自動工廠注冊

c++ automatic factory registration of derived types(c ++派生類型的自動工廠注冊)
本文介紹了c ++派生類型的自動工廠注冊的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

像我之前的許多人一樣,我正在嘗試讓我的派生類型自動注冊到我的工廠.我通讀了許多問題,并試圖專注于我在那里沒有找到的內容.

Like many before me, I'm trying so get my derived types to automatically register with my factory. I read through many question and tried to focus on what I didn't find there.

除了自動注冊之外,其他一切都運行良好.

I've got everything running nicely except the automatic registration.

我的目標:

  1. 自動注冊我的基類的任何派生類Base
  1. 只有我標記為可注冊
  2. 的課程
  3. 不僅是Base的直接子類
    • 例如: Base -> Device -> Camera -> 網絡攝像頭
    • 這將使使用 CRTP 如這個問題困難
  1. only classes I mark as registrable
  2. not only direct sub-classes of Base
    • ex: Base -> Device -> Camera -> Webcam
    • this would make using the CRTP like described in this question dificult

  • 對我想要注冊的課程進行最少的更改 - 假人證明
  • 更喜歡使用注冊器類而不是宏
    • 就像在這個問題中一樣,但我不確定這是否依賴于 CRTP
    • minimal changes to the classes I want registered - dummies proof
    • would prefer using a registrator class than macros
      • like in this question, but I'm not sure if this is dependent on CRTP
      • 我有什么:

        template <class T>
        class abstract_factory
        {
            public:
                template < typename Tsub > static void register_class();
                static T* create( const std::string& name );
            private:
                // allocator<T> is a helper class to create a pointer of correct type
                static std::map<std::string, boost::shared_ptr<allocator<T> > > s_map;
        };
        

        • 模板化抽象工廠,std::string 作為鍵類型
        • 抽象工廠擁有所有成員和方法靜態
        • 使用typeid自動恢復類名(注冊時不需要文本名稱)
        • 調用注冊:abstract_factory::register_class();
          • templated abstract factory, with std::string as key type
          • abstract factory has all members and methods static
          • class name is recovered automatically with typeid (no need for text name when registering)
          • registration by calling: abstract_factory<Base>::register_class<MyDerived>();
          • 我嘗試過的(或想嘗試但不知道如何正確操作):

            • registratorclass:在 Derived.cpp 中靜態實例化的模板化類,應在其構造函數中調用 abstract_factory::register_class()
              • 永遠不會被調用或實例化
              • 如果我在 main() 中創建一個 Derived 的實例,這有效 -> 雖然有點違背了目的
              • registrator<Derived> class: templateded class that is instantiated statically in Derived.cpp and should call abstract_factory::register_class<Derived>() in it's constructor
                • never gets called or instantiated
                • if I make an instance of Derived in main() this works -> kinda defeats the purpose though

                可以使用任何建議,無論大小,thanx.

                Could use any advice, large or small, thanx.

                推薦答案

                我用單例帶成員注冊,基本上:

                I use a singleton with a member for registration, basically:

                template< typename KeyType, typename ProductCreatorType >
                class Factory
                {
                    typedef boost::unordered_map< KeyType, ProductCreatorType > CreatorMap;
                    ...
                };
                

                使用 Loki 之后,我得到了一些類似的東西:

                Using Loki I then have something along these lines:

                 typedef Loki::SingletonHolder< Factory< StringHash, boost::function< boost::shared_ptr< SomeBase >( const SomeSource& ) > >, Loki::CreateStatic > SomeFactory;
                

                注冊通常使用宏完成,例如:

                Registration is usually done using a macro such as:

                #define REGISTER_SOME_FACTORY( type ) static bool BOOST_PP_CAT( type, __regged ) = SomeFactory::Instance().RegisterCreator( BOOST_PP_STRINGIZE( type ), boost::bind( &boost::make_shared< type >, _1 ) );
                

                這種設置有很多優點:

                • 適用于例如 boost::shared_ptr<>.
                • 不需要為所有注冊需求維護一個龐大的文件.
                • 對于創作者來說非常靈活,任何事情都非常順利.
                • 宏涵蓋了最常見的用例,同時為替代方案敞開了大門.

                調用 .cpp 文件中的宏就足以在靜態初始化期間在啟動時注冊類型.當類型注冊是靜態庫的一部分時,這很有效,在這種情況下,它不會包含在您的二進制文件中.將注冊編譯為我見過的庫的一部分的唯一解決方案是擁有一個巨大的文件,該文件將注冊明確作為某種初始化例程的一部分.相反,我現在所做的是在我的庫中創建一個客戶端文件夾,用戶將其作為二進制構建的一部分包含在內.

                Invoking the macro in the .cpp file is then enough to get the type registered at start up during static initialization. This works dandy save for when the type registration is a part of a static library, in which case it won't be included in your binary. The only solutions which compiles the registration as a part of the library which I've seen work is to have one huge file that does the registration explicitly as a part of some sort of initialization routine. Instead what I do nowadays is to have a client folder with my lib which the user includes as a part of the binary build.

                從您的要求列表中,我相信除了使用注冊器類之外,這可以滿足所有要求.

                From your list of requirements I believe this satisfies everything save for using a registrator class.

                這篇關于c ++派生類型的自動工廠注冊的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!
  • 相關文檔推薦

    Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
    Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
    How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
    Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
    Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
    Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
    主站蜘蛛池模板: 嫩草一区二区三区 | 亚洲国产高清免费 | 欧洲亚洲精品久久久久 | 久草视频在线播放 | 欧美三级久久久 | 成人高清视频在线观看 | 久久久亚洲一区 | 久久久久国产精品 | 久久久久久久久久久久久九 | 国产精品一区二区日韩 | 亚洲二区在线 | 亚洲欧美日韩在线一区二区 | 国产精品久久久久久久久久妇女 | 国产在线视频在线观看 | 九九热精品免费 | 精品一区二区三区在线观看国产 | 91在线免费视频 | 久久精品视频一区二区三区 | 日本成人福利 | 亚洲一区视频 | av超碰 | 欧美一区二区三区大片 | 播放一级黄色片 | 欧美日韩午夜精品 | 亚洲精品二区 | 一区二区视频 | 精品一区欧美 | 国产成人av电影 | 中文在线视频 | 日韩快播电影网 | 国产精品乱码一二三区的特点 | 欧美黄色小视频 | 国产精品1区2区 | av在线播放网址 | 欧美一级二级视频 | 国产精品日韩一区 | 四虎影视一区二区 | 欧美人妖网站 | 91视频网址| 午夜黄色| 亚洲一级视频在线 |