google :: protobuf :: reflection :: GeneratedProtocolMessageType :: Class GeneratedProtocolMessageType
[hide private]
[frames] | no frames]

Type GeneratedProtocolMessageType

object --+    
         |    
      type --+
             |
            GeneratedProtocolMessageType

Metaclass for protocol message classes created at runtime from Descriptors.

We add implementations for all methods described in the Message class.  We
also create properties to allow getting/setting all fields in the protocol
message.  Finally, we create slots to prevent users from accidentally
"setting" nonexistent fields in the protocol message, which then wouldn't get
serialized / deserialized properly.

The protocol compiler currently uses this metaclass to create protocol
message classes at runtime.  Clients can also manually create their own
classes at runtime, as in this example:

mydescriptor = Descriptor(.....)
class MyProtoClass(Message):
  __metaclass__ = GeneratedProtocolMessageType
  DESCRIPTOR = mydescriptor
myproto_instance = MyProtoClass()
myproto.foo_field = 23
...

Instance Methods [hide private]
the object's type
__init__(cls, name, bases, dictionary)
Here we perform the majority of our work on the class.

Inherited from type: __call__, __delattr__, __eq__, __ge__, __getattribute__, __gt__, __hash__, __le__, __lt__, __ne__, __repr__, __setattr__, __subclasses__, mro

Inherited from object: __format__, __reduce__, __reduce_ex__, __sizeof__, __str__, __subclasshook__

Static Methods [hide private]
a new object with type S, a subtype of T
__new__(cls, name, bases, dictionary)
Custom allocation for runtime-generated class types.
Class Variables [hide private]
  _DESCRIPTOR_KEY = 'DESCRIPTOR'
Properties [hide private]

Inherited from type: __abstractmethods__, __base__, __bases__, __basicsize__, __dictoffset__, __flags__, __instancecheck__, __itemsize__, __mro__, __name__, __subclasscheck__, __weakrefoffset__

Inherited from object: __class__

Method Details [hide private]

__init__(cls, name, bases, dictionary)
(Constructor)

 
Here we perform the majority of our work on the class.
We add enum getters, an __init__ method, implementations
of all Message methods, and properties for all fields
in the protocol type.

Args:
  name: Name of the class (ignored, but required by the
    metaclass protocol).
  bases: Base classes of the class we're constructing.
    (Should be message.Message).  We ignore this field, but
    it's required by the metaclass protocol
  dictionary: The class dictionary of the class we're
    constructing.  dictionary[_DESCRIPTOR_KEY] must contain
    a Descriptor object describing this protocol message
    type.

Returns: the object's type
Overrides: object.__init__

__new__(cls, name, bases, dictionary)
Static Method

 
Custom allocation for runtime-generated class types.

We override __new__ because this is apparently the only place
where we can meaningfully set __slots__ on the class we're creating(?).
(The interplay between metaclasses and slots is not very well-documented).

Args:
  name: Name of the class (ignored, but required by the
    metaclass protocol).
  bases: Base classes of the class we're constructing.
    (Should be message.Message).  We ignore this field, but
    it's required by the metaclass protocol
  dictionary: The class dictionary of the class we're
    constructing.  dictionary[_DESCRIPTOR_KEY] must contain
    a Descriptor object describing this protocol message
    type.

Returns:
  Newly-allocated class.

Returns: a new object with type S, a subtype of T
Overrides: object.__new__