简体   繁体   English

如何使用POD阵列msgpack一个用户定义的C ++类?

[英]How to msgpack a user-defined C++ class with POD-arrays?

How it is possible to provide all three functions: msgpack_pack , msgpack_unpack and msgpack_object (also, what are meanings of them, exactly?) for a user-defined C++ class (in the same way MSGPACK_DEFINE does it for non-array POD/UD types) containing Plain Old Data arrays (such as dobule[] or char[] ), so my class will play nicely with higher-level classes, containg this class in map or a vector? 如何为用户定义的C ++类提供所有三个函数: msgpack_packmsgpack_unpackmsgpack_object (以及它们的含义,确切地说是什么?)(对于非数组POD / UD类型, MSGPACK_DEFINE也是如此) )包含Plain Old Data数组(例如dobule[]char[] ),所以我的类可以很好地使用更高级的类,在map或vector中包含这个类?

Is there any examples of implementing them for your own class or at least msgpack C++ api documentation? 是否有任何为您自己的类或至少msgpack C ++ api文档实现它们的示例?

The only link to possible api reference i've found was http://redmine.msgpack.org/projects/msgpack/wiki ; 我发现可能的API参考的唯一链接是http://redmine.msgpack.org/projects/msgpack/wiki ; but it is dead now. 但它现在已经死了。

Say, i have a struct like 说,我有一个类似的结构

struct entity {
  const char name[256];
  double mat[16];
};

What would be a msgpack_* member functions for it? 什么是msgpack_ *成员函数呢?

Thanks to guy who -1'd my question, i felt grievance and explored the actual undocumented codebase of msgpack. 感谢那个对我提问的人,我感到不满,并探索了msgpack的实际未记录的代码库。 Here is the example of mentioned earlier functions with sort of explanation, in amount of my (vastly incomplete due to missing docs) understanding: 以下是前面提到的函数的示例,其中有一些解释,我的数量(由于缺少文档而非常不完整)理解:

struct entity {
  char name[256];
  double mat[16];

  // this function is appears to be a mere serializer
  template <typename Packer>
  void msgpack_pack(Packer& pk) const {
    // make array of two elements, by the number of class fields
    pk.pack_array(2); 

    // pack the first field, strightforward
    pk.pack_raw(sizeof(name));
    pk.pack_raw_body(name, sizeof(name));

    // since it is array of doubles, we can't use direct conversion or copying
    // memory because it would be a machine-dependent representation of floats
    // instead, we converting this POD array to some msgpack array, like this:
    pk.pack_array(16);
    for (int i = 0; i < 16; i++) {
      pk.pack_double(mat[i]);
    }
  }

  // this function is looks like de-serializer, taking an msgpack object
  // and extracting data from it to the current class fields
  void msgpack_unpack(msgpack::object o) {
    // check if received structure is an array
    if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }

    const size_t size = o.via.array.size;

    // sanity check
    if(size <= 0) return;
    // extract value of first array entry to a class field
    memcpy(name, o.via.array.ptr[0].via.raw.ptr, o.via.array.ptr[0].via.raw.size);

    // sanity check
    if(size <= 1) return;
    // extract value of second array entry which is array itself:
    for (int i = 0; i < 16 ; i++) {
      mat[i] = o.via.array.ptr[1].via.array.ptr[i].via.dec;
    }
  }

  // destination of this function is unknown - i've never ran into scenary
  // what it was called. some explaination/documentation needed.
  template <typename MSGPACK_OBJECT>
  void msgpack_object(MSGPACK_OBJECT* o, msgpack::zone* z) const { 

  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM