Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright 2016 Google Inc. All rights reserved.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "flatbuffers/code_generators.h"
     18 #include <assert.h>
     19 #include "flatbuffers/base.h"
     20 #include "flatbuffers/util.h"
     21 
     22 #if defined(_MSC_VER)
     23 #pragma warning(push)
     24 #pragma warning(disable: 4127) // C4127: conditional expression is constant
     25 #endif
     26 
     27 namespace flatbuffers {
     28 
     29 void CodeWriter::operator+=(std::string text) {
     30 
     31   while (true) {
     32     auto begin = text.find("{{");
     33     if (begin == std::string::npos) {
     34       break;
     35     }
     36 
     37     auto end = text.find("}}");
     38     if (end == std::string::npos || end < begin) {
     39       break;
     40     }
     41 
     42     // Write all the text before the first {{ into the stream.
     43     stream_.write(text.c_str(), begin);
     44 
     45     // The key is between the {{ and }}.
     46     const std::string key = text.substr(begin + 2, end - begin - 2);
     47 
     48     // Find the value associated with the key.  If it exists, write the
     49     // value into the stream, otherwise write the key itself into the stream.
     50     auto iter = value_map_.find(key);
     51     if (iter != value_map_.end()) {
     52       const std::string &value = iter->second;
     53       stream_ << value;
     54     } else {
     55       assert(false && "could not find key");
     56       stream_ << key;
     57     }
     58 
     59     // Update the text to everything after the }}.
     60     text = text.substr(end + 2);
     61   }
     62   if (!text.empty() && string_back(text) == '\\') {
     63     text.pop_back();
     64     stream_ << text;
     65   } else {
     66     stream_ << text << std::endl;
     67   }
     68 }
     69 
     70 const char *BaseGenerator::FlatBuffersGeneratedWarning() {
     71   return "automatically generated by the FlatBuffers compiler,"
     72          " do not modify";
     73 }
     74 
     75 std::string BaseGenerator::NamespaceDir(const Parser &parser,
     76                                         const std::string &path,
     77                                         const Namespace &ns) {
     78   EnsureDirExists(path.c_str());
     79   if (parser.opts.one_file) return path;
     80   std::string namespace_dir = path;  // Either empty or ends in separator.
     81   auto &namespaces = ns.components;
     82   for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
     83     namespace_dir += *it + kPathSeparator;
     84     EnsureDirExists(namespace_dir.c_str());
     85   }
     86   return namespace_dir;
     87 }
     88 
     89 std::string BaseGenerator::NamespaceDir(const Namespace &ns) const {
     90   return BaseGenerator::NamespaceDir(parser_, path_, ns);
     91 }
     92 
     93 std::string BaseGenerator::FullNamespace(const char *separator,
     94                                          const Namespace &ns) {
     95   std::string namespace_name;
     96   auto &namespaces = ns.components;
     97   for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
     98     if (namespace_name.length()) namespace_name += separator;
     99     namespace_name += *it;
    100   }
    101   return namespace_name;
    102 }
    103 
    104 std::string BaseGenerator::LastNamespacePart(const Namespace &ns) {
    105   if (!ns.components.empty())
    106     return ns.components.back();
    107   else
    108     return std::string("");
    109 }
    110 
    111 // Ensure that a type is prefixed with its namespace whenever it is used
    112 // outside of its namespace.
    113 std::string BaseGenerator::WrapInNameSpace(const Namespace *ns,
    114                                            const std::string &name) const {
    115   if (CurrentNameSpace() == ns) return name;
    116   std::string qualified_name = qualifying_start_;
    117   for (auto it = ns->components.begin(); it != ns->components.end(); ++it)
    118     qualified_name += *it + qualifying_separator_;
    119   return qualified_name + name;
    120 }
    121 
    122 
    123 std::string BaseGenerator::WrapInNameSpace(const Definition &def) const {
    124   return WrapInNameSpace(def.defined_namespace, def.name);
    125 }
    126 
    127 std::string BaseGenerator::GetNameSpace(const Definition &def) const {
    128   const Namespace *ns = def.defined_namespace;
    129   if (CurrentNameSpace() == ns) return "";
    130   std::string qualified_name = qualifying_start_;
    131   for (auto it = ns->components.begin(); it != ns->components.end(); ++it) {
    132     qualified_name += *it;
    133     if ((it + 1) != ns->components.end()) {
    134       qualified_name += qualifying_separator_;
    135     }
    136   }
    137 
    138   return qualified_name;
    139 }
    140 
    141 // Generate a documentation comment, if available.
    142 void GenComment(const std::vector<std::string> &dc, std::string *code_ptr,
    143                 const CommentConfig *config, const char *prefix) {
    144   if (dc.begin() == dc.end()) {
    145     // Don't output empty comment blocks with 0 lines of comment content.
    146     return;
    147   }
    148 
    149   std::string &code = *code_ptr;
    150   if (config != nullptr && config->first_line != nullptr) {
    151     code += std::string(prefix) + std::string(config->first_line) + "\n";
    152   }
    153   std::string line_prefix = std::string(prefix) +
    154       ((config != nullptr && config->content_line_prefix != nullptr) ?
    155        config->content_line_prefix : "///");
    156   for (auto it = dc.begin();
    157        it != dc.end();
    158        ++it) {
    159     code += line_prefix + *it + "\n";
    160   }
    161   if (config != nullptr && config->last_line != nullptr) {
    162     code += std::string(prefix) + std::string(config->last_line) + "\n";
    163   }
    164 }
    165 
    166 }  // namespace flatbuffers
    167 
    168 #if defined(_MSC_VER)
    169 #pragma warning(pop)
    170 #endif
    171