Home | History | Annotate | Download | only in chromeos-dbus-bindings
      1 // Copyright 2014 The Chromium OS Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chromeos-dbus-bindings/name_parser.h"
      6 
      7 #include <string>
      8 
      9 #include <base/strings/string_util.h>
     10 #include <base/strings/stringprintf.h>
     11 #include <brillo/strings/string_utils.h>
     12 
     13 #include "chromeos-dbus-bindings/indented_text.h"
     14 
     15 namespace chromeos_dbus_bindings {
     16 
     17 namespace {
     18 
     19 void AddOpenNamespace(IndentedText *text, const std::string& name) {
     20   text->AddLine(base::StringPrintf("namespace %s {", name.c_str()));
     21 }
     22 
     23 void AddCloseNamespace(IndentedText *text, const std::string& name) {
     24   text->AddLine(base::StringPrintf("}  // namespace %s", name.c_str()));
     25 }
     26 
     27 }  // anonymous namespace
     28 
     29 NameParser::NameParser(const std::string& name)
     30     : namespaces{brillo::string_utils::Split(name, ".")} {
     31   CHECK(!namespaces.empty()) << "Empty name specified";
     32   type_name = namespaces.back();
     33   namespaces.pop_back();
     34 }
     35 
     36 std::string NameParser::MakeFullyQualified(const std::string& name) const {
     37   std::vector<std::string> parts = namespaces;
     38   parts.push_back(name);
     39   return brillo::string_utils::Join("::", parts);
     40 }
     41 
     42 std::string NameParser::MakeFullCppName() const {
     43   return MakeFullyQualified(type_name);
     44 }
     45 
     46 std::string NameParser::MakeVariableName() const {
     47   // Convert CamelCase name to google_style variable name.
     48   std::string result;
     49   bool last_upper = true;
     50   for (char c : type_name) {
     51     bool is_upper = isupper(c);
     52     if (is_upper) {
     53       if (!last_upper)
     54         result += '_';
     55       c = base::ToLowerASCII(c);
     56     }
     57     last_upper = is_upper;
     58     result.push_back(c);
     59   }
     60   return result;
     61 }
     62 
     63 std::string NameParser::MakeInterfaceName(bool fully_qualified) const {
     64   std::string interface_name = type_name + "Interface";
     65   return fully_qualified ? MakeFullyQualified(interface_name) : interface_name;
     66 }
     67 
     68 std::string NameParser::MakeProxyName(bool fully_qualified) const {
     69   std::string proxy_name = type_name + "Proxy";
     70   return fully_qualified ? MakeFullyQualified(proxy_name) : proxy_name;
     71 }
     72 
     73 std::string NameParser::MakeAdaptorName(bool fully_qualified) const {
     74   std::string adaptor_name = type_name + "Adaptor";
     75   return fully_qualified ? MakeFullyQualified(adaptor_name) : adaptor_name;
     76 }
     77 
     78 void NameParser::AddOpenNamespaces(IndentedText *text,
     79                                    bool add_main_type) const {
     80   for (const auto& ns : namespaces) {
     81     AddOpenNamespace(text, ns);
     82   }
     83 
     84   if (add_main_type)
     85     AddOpenNamespace(text, type_name);
     86 }
     87 
     88 void NameParser::AddCloseNamespaces(IndentedText *text,
     89                                     bool add_main_type) const {
     90   if (add_main_type)
     91     AddCloseNamespace(text, type_name);
     92 
     93   for (auto it = namespaces.rbegin(); it != namespaces.rend(); ++it) {
     94     AddCloseNamespace(text, *it);
     95   }
     96 }
     97 
     98 }  // namespace chromeos_dbus_bindings
     99