blob: f3aa72e4409addc31e1507826e31cf5ff8297812 [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
temporal40ee5512008-07-10 02:12:20 +00004//
kenton@google.com24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
kenton@google.com24bf56f2008-09-24 20:31:01 +00009// * 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.
temporal40ee5512008-07-10 02:12:20 +000018//
kenton@google.com24bf56f2008-09-24 20:31:01 +000019// 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.
temporal40ee5512008-07-10 02:12:20 +000030
31// Author: kenton@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_ENUM_H__
36#define GOOGLE_PROTOBUF_COMPILER_CPP_ENUM_H__
37
Feng Xiaoeee38b02015-08-22 18:25:48 -070038#include <set>
temporal40ee5512008-07-10 02:12:20 +000039#include <string>
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000040#include <google/protobuf/compiler/cpp/cpp_options.h>
temporal40ee5512008-07-10 02:12:20 +000041#include <google/protobuf/descriptor.h>
42
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000043
temporal40ee5512008-07-10 02:12:20 +000044namespace google {
45namespace protobuf {
46 namespace io {
47 class Printer; // printer.h
48 }
49}
50
51namespace protobuf {
52namespace compiler {
53namespace cpp {
54
55class EnumGenerator {
56 public:
57 // See generator.cc for the meaning of dllexport_decl.
58 explicit EnumGenerator(const EnumDescriptor* descriptor,
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000059 const Options& options);
temporal40ee5512008-07-10 02:12:20 +000060 ~EnumGenerator();
61
62 // Header stuff.
63
Feng Xiaoeee38b02015-08-22 18:25:48 -070064 // Fills the name to use when declaring the enum. This is for use when
Bo Yang5db21732015-05-21 14:28:59 -070065 // generating other .proto.h files. This code should be placed within the
66 // enum's package namespace, but NOT within any class, even for nested
67 // enums.
Feng Xiaoeee38b02015-08-22 18:25:48 -070068 void FillForwardDeclaration(set<string>* enum_names);
Bo Yang5db21732015-05-21 14:28:59 -070069
temporal40ee5512008-07-10 02:12:20 +000070 // Generate header code defining the enum. This code should be placed
71 // within the enum's package namespace, but NOT within any class, even for
72 // nested enums.
73 void GenerateDefinition(io::Printer* printer);
74
kenton@google.com80b1d622009-07-29 01:13:20 +000075 // Generate specialization of GetEnumDescriptor<MyEnum>().
76 // Precondition: in ::google::protobuf namespace.
77 void GenerateGetEnumDescriptorSpecializations(io::Printer* printer);
78
temporal40ee5512008-07-10 02:12:20 +000079 // For enums nested within a message, generate code to import all the enum's
80 // symbols (e.g. the enum type name, all its values, etc.) into the class's
81 // namespace. This should be placed inside the class definition in the
82 // header.
83 void GenerateSymbolImports(io::Printer* printer);
84
85 // Source file stuff.
86
87 // Generate code that initializes the global variable storing the enum's
88 // descriptor.
89 void GenerateDescriptorInitializer(io::Printer* printer, int index);
90
91 // Generate non-inline methods related to the enum, such as IsValidValue().
92 // Goes in the .cc file.
93 void GenerateMethods(io::Printer* printer);
94
95 private:
96 const EnumDescriptor* descriptor_;
97 string classname_;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000098 Options options_;
jieluo@google.com4de8f552014-07-18 00:47:59 +000099 // whether to generate the *_ARRAYSIZE constant.
100 bool generate_array_size_;
temporal40ee5512008-07-10 02:12:20 +0000101
102 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumGenerator);
103};
104
105} // namespace cpp
106} // namespace compiler
107} // namespace protobuf
108
109} // namespace google
110#endif // GOOGLE_PROTOBUF_COMPILER_CPP_ENUM_H__