PageRenderTime 61ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/model/ProtoBuf.h

https://code.google.com/p/crack-language/
C Header | 54 lines | 22 code | 8 blank | 24 comment | 3 complexity | 591f2c4b182e79814a05d44ed30038b5 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0, LGPL-3.0, Apache-2.0
  1. // Copyright 2013 Google Inc.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. //
  7. #ifndef _model_ProtoBuf_h_
  8. #define _model_ProtoBuf_h_
  9. namespace model {
  10. class ProtoBuf {
  11. public:
  12. enum { varInt = 0, bits64 = 1, string = 2, ref = 3, bits32 = 5 };
  13. };
  14. } // namespace model
  15. // These macros try to take away the boilerplate aspects of dealing with
  16. // nested protobuf files.
  17. // Use them like this:
  18. // CRACK_PB_BEGIN(deserializer, 256, optional)
  19. // CRACK_PB_FIELD(1, varInt)
  20. // foo = optionalDeser.readUInt("foo");
  21. // break;
  22. // CRACK_PB_FIELD(2, string)
  23. // bar = optionalDeser.readString(16, "bar");
  24. // break;
  25. // CRACK_PB_END
  26. //
  27. // Note that:
  28. // - the break statements are very important.
  29. // - CRACK_PB_BEGIN() defines a set of variables whose names begin with its
  30. // third argument (which is also the name of the field that it will read).
  31. // The most important of them is "*Deser" (optionalDeser in the example).
  32. // This is a nested deserializer.
  33. #define CRACK_PB_KEY(id, type) ((id << 3) | (model::ProtoBuf::type))
  34. #define CRACK_PB_BEGIN(deser, maxSize, sectionName) \
  35. { \
  36. model::NestedDeserializer sectionName##Deser(deser, maxSize, #sectionName); \
  37. bool sectionName##EOF; \
  38. int sectionName##Header; \
  39. while ((sectionName##Header = \
  40. sectionName##Deser.readUInt(#sectionName ".header", \
  41. &sectionName##EOF)) || \
  42. !sectionName##EOF) { \
  43. switch (sectionName##Header) {
  44. #define CRACK_PB_FIELD(id, type) case CRACK_PB_KEY(id, type):
  45. #define CRACK_PB_END }}}
  46. #endif