11

如何为包含 Plain Old 的用户定义的 C++ 类(与非数组 POD/UD 类型提供相同的方式)提供所有三个函数msgpack_packmsgpack_unpackmsgpack_object(还有,它们的含义是什么?)MSGPACK_DEFINE数据数组(例如dobule[]char[]),所以我的课程将与更高级别的课程很好地配合,在地图或矢量中包含此类?

有没有为您自己的类或至少 msgpack C++ api 文档实现它们的示例?

我发现的唯一可能的 api 参考链接是http://redmine.msgpack.org/projects/msgpack/wiki;但它现在已经死了。

说,我有一个像

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

它的 msgpack_* 成员函数是什么?

4

1 回答 1

13

感谢 -1 提出我的问题的人,我感到委屈并探索了 msgpack 的实际未记录代码库。这是前面提到的函数的示例,其中有一些解释,在我(由于缺少文档而非常不完整)的理解中:

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 { 

  }
};
于 2013-05-11T18:56:44.163 回答