2008-08-22 20:38:05 +00:00
|
|
|
// Protocol Buffers - Google's data interchange format
|
|
|
|
// Copyright 2008 Google Inc.
|
|
|
|
// http://code.google.com/p/protobuf/
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Author: kenton@google.com (Kenton Varda)
|
|
|
|
// Based on original Protocol Buffers design by
|
|
|
|
// Sanjay Ghemawat, Jeff Dean, and others.
|
|
|
|
|
|
|
|
// Modified to implement C code by Dave Benson.
|
|
|
|
|
2013-11-16 17:11:48 -05:00
|
|
|
#include <protoc-c/c_enum_field.h>
|
|
|
|
#include <protoc-c/c_helpers.h>
|
2008-08-22 20:38:05 +00:00
|
|
|
#include <google/protobuf/io/printer.h>
|
2010-01-24 16:42:14 +00:00
|
|
|
#include <google/protobuf/wire_format.h>
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
namespace google {
|
|
|
|
namespace protobuf {
|
|
|
|
namespace compiler {
|
|
|
|
namespace c {
|
|
|
|
|
|
|
|
using internal::WireFormat;
|
|
|
|
|
|
|
|
// TODO(kenton): Factor out a "SetCommonFieldVariables()" to get rid of
|
|
|
|
// repeat code between this and the other field types.
|
|
|
|
void SetEnumVariables(const FieldDescriptor* descriptor,
|
|
|
|
map<string, string>* variables) {
|
|
|
|
|
|
|
|
(*variables)["name"] = FieldName(descriptor);
|
|
|
|
(*variables)["type"] = FullNameToC(descriptor->enum_type()->full_name());
|
2008-11-30 06:10:28 +00:00
|
|
|
if (descriptor->has_default_value()) {
|
|
|
|
const EnumValueDescriptor* default_value = descriptor->default_value_enum();
|
|
|
|
(*variables)["default"] = FullNameToUpper(default_value->type()->full_name())
|
|
|
|
+ "__" + ToUpper(default_value->name());
|
|
|
|
} else
|
|
|
|
(*variables)["default"] = "0";
|
2010-08-12 19:38:03 +00:00
|
|
|
(*variables)["deprecated"] = FieldDeprecated(descriptor);
|
2008-08-22 20:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ===================================================================
|
|
|
|
|
|
|
|
EnumFieldGenerator::
|
|
|
|
EnumFieldGenerator(const FieldDescriptor* descriptor)
|
|
|
|
: FieldGenerator(descriptor)
|
|
|
|
{
|
|
|
|
SetEnumVariables(descriptor, &variables_);
|
|
|
|
}
|
|
|
|
|
|
|
|
EnumFieldGenerator::~EnumFieldGenerator() {}
|
|
|
|
|
|
|
|
void EnumFieldGenerator::GenerateStructMembers(io::Printer* printer) const
|
|
|
|
{
|
|
|
|
switch (descriptor_->label()) {
|
|
|
|
case FieldDescriptor::LABEL_REQUIRED:
|
2010-08-12 19:38:03 +00:00
|
|
|
printer->Print(variables_, "$type$ $name$$deprecated$;\n");
|
2008-08-22 20:38:05 +00:00
|
|
|
break;
|
|
|
|
case FieldDescriptor::LABEL_OPTIONAL:
|
2010-08-12 19:38:03 +00:00
|
|
|
printer->Print(variables_, "protobuf_c_boolean has_$name$$deprecated$;\n");
|
|
|
|
printer->Print(variables_, "$type$ $name$$deprecated$;\n");
|
2008-08-22 20:38:05 +00:00
|
|
|
break;
|
|
|
|
case FieldDescriptor::LABEL_REPEATED:
|
2010-08-12 19:38:03 +00:00
|
|
|
printer->Print(variables_, "size_t n_$name$$deprecated$;\n");
|
|
|
|
printer->Print(variables_, "$type$ *$name$$deprecated$;\n");
|
2008-08-22 20:38:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-11-30 06:10:28 +00:00
|
|
|
|
|
|
|
string EnumFieldGenerator::GetDefaultValue(void) const
|
|
|
|
{
|
|
|
|
return variables_.find("default")->second;
|
|
|
|
}
|
2008-08-23 19:59:17 +00:00
|
|
|
void EnumFieldGenerator::GenerateStaticInit(io::Printer* printer) const
|
|
|
|
{
|
|
|
|
switch (descriptor_->label()) {
|
|
|
|
case FieldDescriptor::LABEL_REQUIRED:
|
|
|
|
printer->Print(variables_, "$default$");
|
|
|
|
break;
|
2008-08-24 12:38:58 +00:00
|
|
|
case FieldDescriptor::LABEL_OPTIONAL:
|
|
|
|
printer->Print(variables_, "0,$default$");
|
|
|
|
break;
|
2008-08-23 19:59:17 +00:00
|
|
|
case FieldDescriptor::LABEL_REPEATED:
|
|
|
|
// no support for default?
|
|
|
|
printer->Print("0,NULL");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
void EnumFieldGenerator::GenerateDescriptorInitializer(io::Printer* printer) const
|
|
|
|
{
|
|
|
|
string addr = "&" + FullNameToLower(descriptor_->enum_type()->full_name()) + "__descriptor";
|
|
|
|
GenerateDescriptorInitializerGeneric(printer, true, "ENUM", addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace c
|
|
|
|
} // namespace compiler
|
|
|
|
} // namespace protobuf
|
|
|
|
} // namespace google
|