<bdo id='SM37H'></bdo><ul id='SM37H'></ul>

      <tfoot id='SM37H'></tfoot>

    1. <i id='SM37H'><tr id='SM37H'><dt id='SM37H'><q id='SM37H'><span id='SM37H'><b id='SM37H'><form id='SM37H'><ins id='SM37H'></ins><ul id='SM37H'></ul><sub id='SM37H'></sub></form><legend id='SM37H'></legend><bdo id='SM37H'><pre id='SM37H'><center id='SM37H'></center></pre></bdo></b><th id='SM37H'></th></span></q></dt></tr></i><div id='SM37H'><tfoot id='SM37H'></tfoot><dl id='SM37H'><fieldset id='SM37H'></fieldset></dl></div>

      <small id='SM37H'></small><noframes id='SM37H'>

    2. <legend id='SM37H'><style id='SM37H'><dir id='SM37H'><q id='SM37H'></q></dir></style></legend>

      1. C++中如何实现序列化

        时间:2023-06-04
        <i id='fOAL3'><tr id='fOAL3'><dt id='fOAL3'><q id='fOAL3'><span id='fOAL3'><b id='fOAL3'><form id='fOAL3'><ins id='fOAL3'></ins><ul id='fOAL3'></ul><sub id='fOAL3'></sub></form><legend id='fOAL3'></legend><bdo id='fOAL3'><pre id='fOAL3'><center id='fOAL3'></center></pre></bdo></b><th id='fOAL3'></th></span></q></dt></tr></i><div id='fOAL3'><tfoot id='fOAL3'></tfoot><dl id='fOAL3'><fieldset id='fOAL3'></fieldset></dl></div>
          <legend id='fOAL3'><style id='fOAL3'><dir id='fOAL3'><q id='fOAL3'></q></dir></style></legend>
            <tbody id='fOAL3'></tbody>
          <tfoot id='fOAL3'></tfoot>

          <small id='fOAL3'></small><noframes id='fOAL3'>

            • <bdo id='fOAL3'></bdo><ul id='fOAL3'></ul>

                  本文介绍了C++中如何实现序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  每当我发现自己需要在 C++ 程序中序列化对象时,我就会回到这种模式:

                  Whenever I find myself needing to serialize objects in a C++ program, I fall back to this kind of pattern:

                  class Serializable {
                    public:
                      static Serializable *deserialize(istream &is) {
                          int id;
                          is >> id;
                          switch(id) {
                            case EXAMPLE_ID:
                              return new ExampleClass(is);
                            //...
                          }
                      }
                  
                      void serialize(ostream &os) {
                          os << getClassID();
                          serializeMe(os);
                      }
                  
                    protected:
                      int getClassID()=0;
                      void serializeMe(ostream &os)=0;
                  };
                  

                  以上在实践中效果很好.但是,我听说这种切换类 ID 是邪恶的,是一种反模式;在 C++ 中处理序列化的标准 OO 方式是什么?

                  The above works pretty well in practice. However, I've heard that this kind of switching over class IDs is evil and an antipattern; what's the standard, OO-way of handling serialization in C++?

                  推荐答案

                  使用类似 Boost Serialization 虽然绝不是标准,但(在大多数情况下)是一个编写得非常好的库,可以为您完成繁重的工作.

                  Using something like Boost Serialization, while by no means a standard, is a (for the most part) very well written library that does the grunt work for you.

                  上次我必须手动解析带有清晰继承树的预定义记录结构时,我最终使用了 带有可注册类的工厂模式(即使用键映射到(模板)创建者函数而不是大量开关函数)来尝试避免您遇到的问题.

                  The last time I had to manually parse a predefined record structure with a clear inheritance tree, I ended up using the factory pattern with registrable classes (i.e. Using a map of key to a (template) creator function rather than a lot of switch functions) to try and avoid the issue you were having.

                  编辑
                  上一段提到的对象工厂的基本 C++ 实现.

                  EDIT
                  A basic C++ implementation of a object factory mentioned in the above paragraph.

                  /**
                  * A class for creating objects, with the type of object created based on a key
                  * 
                  * @param K the key
                  * @param T the super class that all created classes derive from
                  */
                  template<typename K, typename T>
                  class Factory { 
                  private: 
                      typedef T *(*CreateObjectFunc)();
                  
                      /**
                      * A map keys (K) to functions (CreateObjectFunc)
                      * When creating a new type, we simply call the function with the required key
                      */
                      std::map<K, CreateObjectFunc> mObjectCreator;
                  
                      /**
                      * Pointers to this function are inserted into the map and called when creating objects
                      *
                      * @param S the type of class to create
                      * @return a object with the type of S
                      */
                      template<typename S> 
                      static T* createObject(){ 
                          return new S(); 
                      }
                  public:
                  
                      /**
                      * Registers a class to that it can be created via createObject()
                      *
                      * @param S the class to register, this must ve a subclass of T
                      * @param id the id to associate with the class. This ID must be unique
                      */ 
                      template<typename S> 
                      void registerClass(K id){ 
                          if (mObjectCreator.find(id) != mObjectCreator.end()){ 
                              //your error handling here
                          }
                          mObjectCreator.insert( std::make_pair<K,CreateObjectFunc>(id, &createObject<S> ) ); 
                      }
                  
                      /**
                      * Returns true if a given key exists
                      *
                      * @param id the id to check exists
                      * @return true if the id exists
                      */
                      bool hasClass(K id){
                          return mObjectCreator.find(id) != mObjectCreator.end();
                      } 
                  
                      /**
                      * Creates an object based on an id. It will return null if the key doesn't exist
                      *
                      * @param id the id of the object to create
                      * @return the new object or null if the object id doesn't exist
                      */
                      T* createObject(K id){
                          //Don't use hasClass here as doing so would involve two lookups
                          typename std::map<K, CreateObjectFunc>::iterator iter = mObjectCreator.find(id); 
                          if (iter == mObjectCreator.end()){ 
                              return NULL;
                          }
                          //calls the required createObject() function
                          return ((*iter).second)();
                      }
                  };
                  

                  这篇关于C++中如何实现序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:序列化包含 std::string 的类 下一篇:提升 C++ 序列化开销

                  相关文章

                    <bdo id='bTu90'></bdo><ul id='bTu90'></ul>

                  1. <tfoot id='bTu90'></tfoot>
                  2. <legend id='bTu90'><style id='bTu90'><dir id='bTu90'><q id='bTu90'></q></dir></style></legend>
                  3. <i id='bTu90'><tr id='bTu90'><dt id='bTu90'><q id='bTu90'><span id='bTu90'><b id='bTu90'><form id='bTu90'><ins id='bTu90'></ins><ul id='bTu90'></ul><sub id='bTu90'></sub></form><legend id='bTu90'></legend><bdo id='bTu90'><pre id='bTu90'><center id='bTu90'></center></pre></bdo></b><th id='bTu90'></th></span></q></dt></tr></i><div id='bTu90'><tfoot id='bTu90'></tfoot><dl id='bTu90'><fieldset id='bTu90'></fieldset></dl></div>

                    <small id='bTu90'></small><noframes id='bTu90'>