-
Notifications
You must be signed in to change notification settings - Fork 644
Expand file tree
/
Copy patht_generator.cc
More file actions
91 lines (80 loc) · 2.83 KB
/
Copy patht_generator.cc
File metadata and controls
91 lines (80 loc) · 2.83 KB
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* 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.
*/
#include <thrift/compiler/generate/t_generator.h>
#include <stdexcept>
#include <utility>
#include <fmt/format.h>
namespace apache::thrift::compiler {
namespace {
/**
* Add consistent indentation and line breaks to the generator documentation
* passed to `THRIFT_REGISTER_GENERATOR`.
*/
std::string normalize_documentation(std::string_view doc) {
static constexpr auto kIndent = " ";
std::ostringstream out;
while (!doc.empty()) {
std::string_view line = doc.substr(0, doc.find_first_of('\n'));
doc.remove_prefix(std::min(doc.size(), line.size() + 1));
if (!line.empty()) {
out << kIndent << line;
}
out << '\n';
}
return out.str();
}
} // namespace
void t_generator::process_options(
const std::map<std::string, std::string>& options,
std::string out_path,
bool add_gen_dir) {
std::filesystem::path path = {out_path};
if (!out_path.empty() && out_path.back() != '/' && out_path.back() != '\\') {
path += std::filesystem::path::preferred_separator;
}
out_path_ = path.make_preferred().string();
add_gen_dir_ = add_gen_dir;
process_options(options);
}
generator_factory::generator_factory(
std::string name, std::string long_name, std::string_view documentation)
: name_(std::move(name)),
long_name_(std::move(long_name)),
documentation_(normalize_documentation(documentation)) {
generator_registry::register_generator(name_, this);
}
void generator_registry::register_generator(
const std::string& name, generator_factory* factory) {
if (!get_generators().insert({name, factory}).second) {
throw std::logic_error(fmt::format("duplicate generator \"{}\"", name));
}
}
std::unique_ptr<t_generator> generator_registry::make_generator(
const std::string& name,
t_program& p,
t_program_bundle& pb,
diagnostics_engine& diags) {
generator_map& map = get_generators();
auto iter = map.find(name);
return iter != map.end() ? iter->second->make_generator(p, pb, diags)
: nullptr;
}
generator_registry::generator_map& generator_registry::get_generators() {
// http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12
static generator_map* map = new generator_map();
return *map;
}
} // namespace apache::thrift::compiler