Blame view

ios/cocos2d/external/flatbuffers/idl_gen_fbs.cpp 3.55 KB
520389e3   xiaoyu   接入cocos源码,编译未通过,继续修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  /*
   * Copyright 2014 Google Inc. All rights reserved.
   *
   * 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.
   */
  
  // independent from idl_parser, since this code is not needed for most clients
  
  #include "flatbuffers/flatbuffers.h"
  #include "flatbuffers/idl.h"
  #include "flatbuffers/util.h"
  
  namespace flatbuffers {
  
  static std::string GenType(const Type &type) {
    switch (type.base_type) {
      case BASE_TYPE_STRUCT: return type.struct_def->name;
      case BASE_TYPE_UNION:  return type.enum_def->name;
      case BASE_TYPE_VECTOR: return "[" + GenType(type.VectorType()) + "]";
      default: return kTypeNames[type.base_type];
    }
  }
  
  // Generate a flatbuffer schema from the Parser's internal representation.
  std::string GenerateFBS(const Parser &parser, const std::string &file_name,
                          const GeneratorOptions &opts) {
    std::string schema;
    schema += "// Generated from " + file_name + ".proto\n\n";
    if (opts.include_dependence_headers) {
      int num_includes = 0;
      for (auto it = parser.included_files_.begin();
           it != parser.included_files_.end(); ++it) {
        auto basename = flatbuffers::StripPath(
                          flatbuffers::StripExtension(it->first));
        if (basename != file_name) {
          schema += "include \"" + basename + ".fbs\";\n";
          num_includes++;
        }
      }
      if (num_includes) schema += "\n";
    }
    schema += "namespace ";
    auto name_space = parser.namespaces_.back();
    for (auto it = name_space->components.begin();
             it != name_space->components.end(); ++it) {
      if (it != name_space->components.begin()) schema += ".";
      schema += *it;
    }
    schema += ";\n\n";
    // Generate code for all the enum declarations.
    for (auto it = parser.enums_.vec.begin();
             it != parser.enums_.vec.end(); ++it) {
      EnumDef &enum_def = **it;
      schema += "enum " + enum_def.name + " : ";
      schema += GenType(enum_def.underlying_type) + " {\n";
      for (auto it2 = enum_def.vals.vec.begin();
           it2 != enum_def.vals.vec.end(); ++it2) {
        auto &ev = **it2;
        schema += "  " + ev.name + " = " + NumToString(ev.value) + ",\n";
      }
      schema += "}\n\n";
    }
    // Generate code for all structs/tables.
    for (auto it = parser.structs_.vec.begin();
             it != parser.structs_.vec.end(); ++it) {
      StructDef &struct_def = **it;
      schema += "table " + struct_def.name + " {\n";
      for (auto it2 = struct_def.fields.vec.begin();
               it2 != struct_def.fields.vec.end(); ++it2) {
        auto &field = **it2;
        schema += "  " + field.name + ":" + GenType(field.value.type);
        if (field.value.constant != "0") schema += " = " + field.value.constant;
        if (field.required) schema += " (required)";
        schema += ";\n";
      }
      schema += "}\n\n";
    }
    return schema;
  }
  
  bool GenerateFBS(const Parser &parser,
                   const std::string &path,
                   const std::string &file_name,
                   const GeneratorOptions &opts) {
    return SaveFile((path + file_name + ".fbs").c_str(),
                    GenerateFBS(parser, file_name, opts), false);
  }
  
  }  // namespace flatbuffers