Home | History | Annotate | Download | only in cpp
      1 // Protocol Buffers - Google's data interchange format
      2 // Copyright 2008 Google Inc.  All rights reserved.
      3 // http://code.google.com/p/protobuf/
      4 //
      5 // Redistribution and use in source and binary forms, with or without
      6 // modification, are permitted provided that the following conditions are
      7 // met:
      8 //
      9 //     * Redistributions of source code must retain the above copyright
     10 // notice, this list of conditions and the following disclaimer.
     11 //     * Redistributions in binary form must reproduce the above
     12 // copyright notice, this list of conditions and the following disclaimer
     13 // in the documentation and/or other materials provided with the
     14 // distribution.
     15 //     * Neither the name of Google Inc. nor the names of its
     16 // contributors may be used to endorse or promote products derived from
     17 // this software without specific prior written permission.
     18 //
     19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30 
     31 // Author: kenton (at) google.com (Kenton Varda)
     32 //  Based on original Protocol Buffers design by
     33 //  Sanjay Ghemawat, Jeff Dean, and others.
     34 
     35 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
     36 #define GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
     37 
     38 #include <map>
     39 #include <string>
     40 
     41 #include <google/protobuf/stubs/common.h>
     42 #include <google/protobuf/descriptor.h>
     43 
     44 namespace google {
     45 namespace protobuf {
     46   namespace io {
     47     class Printer;             // printer.h
     48   }
     49 }
     50 
     51 namespace protobuf {
     52 namespace compiler {
     53 namespace cpp {
     54 
     55 // Helper function: set variables in the map that are the same for all
     56 // field code generators.
     57 // ['name', 'index', 'number', 'classname', 'declared_type', 'tag_size',
     58 // 'deprecation'].
     59 void SetCommonFieldVariables(const FieldDescriptor* descriptor,
     60                              map<string, string>* variables);
     61 
     62 class FieldGenerator {
     63  public:
     64   FieldGenerator() {}
     65   virtual ~FieldGenerator();
     66 
     67   // Generate lines of code declaring members fields of the message class
     68   // needed to represent this field.  These are placed inside the message
     69   // class.
     70   virtual void GeneratePrivateMembers(io::Printer* printer) const = 0;
     71 
     72   // Generate prototypes for all of the accessor functions related to this
     73   // field.  These are placed inside the class definition.
     74   virtual void GenerateAccessorDeclarations(io::Printer* printer) const = 0;
     75 
     76   // Generate inline definitions of accessor functions for this field.
     77   // These are placed inside the header after all class definitions.
     78   virtual void GenerateInlineAccessorDefinitions(
     79     io::Printer* printer) const = 0;
     80 
     81   // Generate definitions of accessors that aren't inlined.  These are
     82   // placed somewhere in the .cc file.
     83   // Most field types don't need this, so the default implementation is empty.
     84   virtual void GenerateNonInlineAccessorDefinitions(
     85     io::Printer* printer) const {}
     86 
     87   // Generate lines of code (statements, not declarations) which clear the
     88   // field.  This is used to define the clear_$name$() method as well as
     89   // the Clear() method for the whole message.
     90   virtual void GenerateClearingCode(io::Printer* printer) const = 0;
     91 
     92   // Generate lines of code (statements, not declarations) which merges the
     93   // contents of the field from the current message to the target message,
     94   // which is stored in the generated code variable "from".
     95   // This is used to fill in the MergeFrom method for the whole message.
     96   // Details of this usage can be found in message.cc under the
     97   // GenerateMergeFrom method.
     98   virtual void GenerateMergingCode(io::Printer* printer) const = 0;
     99 
    100   // Generate lines of code (statements, not declarations) which swaps
    101   // this field and the corresponding field of another message, which
    102   // is stored in the generated code variable "other". This is used to
    103   // define the Swap method. Details of usage can be found in
    104   // message.cc under the GenerateSwap method.
    105   virtual void GenerateSwappingCode(io::Printer* printer) const = 0;
    106 
    107   // Generate initialization code for private members declared by
    108   // GeneratePrivateMembers(). These go into the message class's SharedCtor()
    109   // method, invoked by each of the generated constructors.
    110   virtual void GenerateConstructorCode(io::Printer* printer) const = 0;
    111 
    112   // Generate any code that needs to go in the class's SharedDtor() method,
    113   // invoked by the destructor.
    114   // Most field types don't need this, so the default implementation is empty.
    115   virtual void GenerateDestructorCode(io::Printer* printer) const {}
    116 
    117   // Generate lines to decode this field, which will be placed inside the
    118   // message's MergeFromCodedStream() method.
    119   virtual void GenerateMergeFromCodedStream(io::Printer* printer) const = 0;
    120 
    121   // Generate lines to decode this field from a packed value, which will be
    122   // placed inside the message's MergeFromCodedStream() method.
    123   virtual void GenerateMergeFromCodedStreamWithPacking(io::Printer* printer)
    124       const;
    125 
    126   // Generate lines to serialize this field, which are placed within the
    127   // message's SerializeWithCachedSizes() method.
    128   virtual void GenerateSerializeWithCachedSizes(io::Printer* printer) const = 0;
    129 
    130   // Generate lines to serialize this field directly to the array "target",
    131   // which are placed within the message's SerializeWithCachedSizesToArray()
    132   // method. This must also advance "target" past the written bytes.
    133   virtual void GenerateSerializeWithCachedSizesToArray(
    134       io::Printer* printer) const = 0;
    135 
    136   // Generate lines to compute the serialized size of this field, which
    137   // are placed in the message's ByteSize() method.
    138   virtual void GenerateByteSize(io::Printer* printer) const = 0;
    139 
    140  private:
    141   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator);
    142 };
    143 
    144 // Convenience class which constructs FieldGenerators for a Descriptor.
    145 class FieldGeneratorMap {
    146  public:
    147   explicit FieldGeneratorMap(const Descriptor* descriptor);
    148   ~FieldGeneratorMap();
    149 
    150   const FieldGenerator& get(const FieldDescriptor* field) const;
    151 
    152  private:
    153   const Descriptor* descriptor_;
    154   scoped_array<scoped_ptr<FieldGenerator> > field_generators_;
    155 
    156   static FieldGenerator* MakeGenerator(const FieldDescriptor* field);
    157 
    158   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap);
    159 };
    160 
    161 
    162 }  // namespace cpp
    163 }  // namespace compiler
    164 }  // namespace protobuf
    165 
    166 }  // namespace google
    167 #endif  // GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
    168