objectivec_field.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  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. #ifndef GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__
  31. #define GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__
  32. #include <map>
  33. #include <string>
  34. #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
  35. #include <google/protobuf/stubs/common.h>
  36. #include <google/protobuf/descriptor.h>
  37. namespace google {
  38. namespace protobuf {
  39. namespace io {
  40. class Printer; // printer.h
  41. } // namespace io
  42. namespace compiler {
  43. namespace objectivec {
  44. class FieldGenerator {
  45. public:
  46. static FieldGenerator* Make(const FieldDescriptor* field,
  47. const Options& options);
  48. virtual ~FieldGenerator();
  49. // Exposed for subclasses to fill in.
  50. virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const = 0;
  51. virtual void GeneratePropertyDeclaration(io::Printer* printer) const = 0;
  52. virtual void GeneratePropertyImplementation(io::Printer* printer) const = 0;
  53. // Called by GenerateFieldDescription, exposed for classes that need custom
  54. // generation.
  55. virtual void GenerateFieldDescriptionTypeSpecific(io::Printer* printer) const;
  56. // Exposed for subclasses to extend, base does nothing.
  57. virtual void GenerateCFunctionDeclarations(io::Printer* printer) const;
  58. virtual void GenerateCFunctionImplementations(io::Printer* printer) const;
  59. // Exposed for subclasses, should always call it on the parent class also.
  60. virtual void DetermineForwardDeclarations(set<string>* fwd_decls) const;
  61. // Used during generation, not intended to be extended by subclasses.
  62. void GenerateFieldDescription(io::Printer* printer) const;
  63. void GenerateFieldNumberConstant(io::Printer* printer) const;
  64. void SetOneofIndexBase(int index_base);
  65. string variable(const char* key) const {
  66. return variables_.find(key)->second;
  67. }
  68. bool needs_textformat_name_support() const {
  69. const string& field_flags = variable("fieldflags");
  70. return field_flags.find("GPBFieldTextFormatNameCustom") != string::npos;
  71. }
  72. string generated_objc_name() const { return variable("name"); }
  73. string raw_field_name() const { return variable("raw_field_name"); }
  74. protected:
  75. FieldGenerator(const FieldDescriptor* descriptor, const Options& options);
  76. virtual void FinishInitialization(void);
  77. virtual bool WantsHasProperty(void) const = 0;
  78. const FieldDescriptor* descriptor_;
  79. map<string, string> variables_;
  80. private:
  81. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator);
  82. };
  83. class SingleFieldGenerator : public FieldGenerator {
  84. public:
  85. virtual ~SingleFieldGenerator();
  86. virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const;
  87. virtual void GeneratePropertyDeclaration(io::Printer* printer) const;
  88. virtual void GeneratePropertyImplementation(io::Printer* printer) const;
  89. protected:
  90. SingleFieldGenerator(const FieldDescriptor* descriptor,
  91. const Options& options);
  92. virtual bool WantsHasProperty(void) const;
  93. private:
  94. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SingleFieldGenerator);
  95. };
  96. // Subclass with common support for when the field ends up as an ObjC Object.
  97. class ObjCObjFieldGenerator : public SingleFieldGenerator {
  98. public:
  99. virtual ~ObjCObjFieldGenerator();
  100. virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const;
  101. virtual void GeneratePropertyDeclaration(io::Printer* printer) const;
  102. protected:
  103. ObjCObjFieldGenerator(const FieldDescriptor* descriptor,
  104. const Options& options);
  105. private:
  106. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ObjCObjFieldGenerator);
  107. };
  108. class RepeatedFieldGenerator : public ObjCObjFieldGenerator {
  109. public:
  110. virtual ~RepeatedFieldGenerator();
  111. virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const;
  112. virtual void GeneratePropertyDeclaration(io::Printer* printer) const;
  113. virtual void GeneratePropertyImplementation(io::Printer* printer) const;
  114. protected:
  115. RepeatedFieldGenerator(const FieldDescriptor* descriptor,
  116. const Options& options);
  117. virtual void FinishInitialization(void);
  118. virtual bool WantsHasProperty(void) const;
  119. private:
  120. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedFieldGenerator);
  121. };
  122. // Convenience class which constructs FieldGenerators for a Descriptor.
  123. class FieldGeneratorMap {
  124. public:
  125. FieldGeneratorMap(const Descriptor* descriptor, const Options& options);
  126. ~FieldGeneratorMap();
  127. const FieldGenerator& get(const FieldDescriptor* field) const;
  128. const FieldGenerator& get_extension(int index) const;
  129. void SetOneofIndexBase(int index_base);
  130. private:
  131. const Descriptor* descriptor_;
  132. scoped_array<scoped_ptr<FieldGenerator> > field_generators_;
  133. scoped_array<scoped_ptr<FieldGenerator> > extension_generators_;
  134. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap);
  135. };
  136. } // namespace objectivec
  137. } // namespace compiler
  138. } // namespace protobuf
  139. } // namespace google
  140. #endif // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__