cpp_message.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc. All rights reserved.
  3. #
  4. # Use of this source code is governed by a BSD-style
  5. # license that can be found in the LICENSE file or at
  6. # https://developers.google.com/open-source/licenses/bsd
  7. """Protocol message implementation hooks for C++ implementation.
  8. Contains helper functions used to create protocol message classes from
  9. Descriptor objects at runtime backed by the protocol buffer C++ API.
  10. """
  11. __author__ = 'tibell@google.com (Johan Tibell)'
  12. from google.protobuf.internal import api_implementation
  13. # pylint: disable=protected-access
  14. _message = api_implementation._c_module
  15. # TODO: Remove this import after fix api_implementation
  16. if _message is None:
  17. from google.protobuf.pyext import _message
  18. class GeneratedProtocolMessageType(_message.MessageMeta):
  19. """Metaclass for protocol message classes created at runtime from Descriptors.
  20. The protocol compiler currently uses this metaclass to create protocol
  21. message classes at runtime. Clients can also manually create their own
  22. classes at runtime, as in this example:
  23. mydescriptor = Descriptor(.....)
  24. factory = symbol_database.Default()
  25. factory.pool.AddDescriptor(mydescriptor)
  26. MyProtoClass = message_factory.GetMessageClass(mydescriptor)
  27. myproto_instance = MyProtoClass()
  28. myproto.foo_field = 23
  29. ...
  30. The above example will not work for nested types. If you wish to include them,
  31. use reflection.MakeClass() instead of manually instantiating the class in
  32. order to create the appropriate class structure.
  33. """
  34. # Must be consistent with the protocol-compiler code in
  35. # proto2/compiler/internal/generator.*.
  36. _DESCRIPTOR_KEY = 'DESCRIPTOR'