summaryrefslogtreecommitdiff
path: root/chromeos-dbus-bindings/name_parser.cc
blob: f920c04dc657077d9d47f14eaa50fc35e406b566 (plain)
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
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos-dbus-bindings/name_parser.h"

#include <string>

#include <base/strings/string_util.h>
#include <base/strings/stringprintf.h>
#include <chromeos/strings/string_utils.h>

#include "chromeos-dbus-bindings/indented_text.h"

namespace chromeos_dbus_bindings {

namespace {

void AddOpenNamespace(IndentedText *text, const std::string& name) {
  text->AddLine(base::StringPrintf("namespace %s {", name.c_str()));
}

void AddCloseNamespace(IndentedText *text, const std::string& name) {
  text->AddLine(base::StringPrintf("}  // namespace %s", name.c_str()));
}

}  // anonymous namespace

NameParser::NameParser(const std::string& name)
    : namespaces{chromeos::string_utils::Split(name, '.')} {
  CHECK(!namespaces.empty()) << "Empty name specified";
  type_name = namespaces.back();
  namespaces.pop_back();
}

std::string NameParser::MakeFullyQualified(const std::string& name) const {
  std::vector<std::string> parts = namespaces;
  parts.push_back(name);
  return chromeos::string_utils::Join("::", parts);
}

std::string NameParser::MakeFullCppName() const {
  return MakeFullyQualified(type_name);
}

std::string NameParser::MakeVariableName() const {
  // Convert CamelCase name to google_style variable name.
  std::string result;
  bool last_upper = true;
  for (char c : type_name) {
    bool is_upper = isupper(c);
    if (is_upper) {
      if (!last_upper)
        result += '_';
      c = base::ToLowerASCII(c);
    }
    last_upper = is_upper;
    result.push_back(c);
  }
  return result;
}

std::string NameParser::MakeInterfaceName(bool fully_qualified) const {
  std::string interface_name = type_name + "Interface";
  return fully_qualified ? MakeFullyQualified(interface_name) : interface_name;
}

std::string NameParser::MakeProxyName(bool fully_qualified) const {
  std::string proxy_name = type_name + "Proxy";
  return fully_qualified ? MakeFullyQualified(proxy_name) : proxy_name;
}

std::string NameParser::MakeAdaptorName(bool fully_qualified) const {
  std::string adaptor_name = type_name + "Adaptor";
  return fully_qualified ? MakeFullyQualified(adaptor_name) : adaptor_name;
}

void NameParser::AddOpenNamespaces(IndentedText *text,
                                   bool add_main_type) const {
  for (const auto& ns : namespaces) {
    AddOpenNamespace(text, ns);
  }

  if (add_main_type)
    AddOpenNamespace(text, type_name);
}

void NameParser::AddCloseNamespaces(IndentedText *text,
                                    bool add_main_type) const {
  if (add_main_type)
    AddCloseNamespace(text, type_name);

  for (auto it = namespaces.rbegin(); it != namespaces.rend(); ++it) {
    AddCloseNamespace(text, *it);
  }
}

}  // namespace chromeos_dbus_bindings