diff --git a/src/compiler/cpp_generator.cc b/src/compiler/cpp_generator.cc index 94e56d73a6e..29bd3560f3e 100644 --- a/src/compiler/cpp_generator.cc +++ b/src/compiler/cpp_generator.cc @@ -31,6 +31,9 @@ * */ +#include +#include + #include "src/compiler/cpp_generator.h" #include "src/compiler/cpp_generator_helpers.h" @@ -92,8 +95,8 @@ bool HasBidiStreaming(const google::protobuf::FileDescriptor* file) { } } // namespace -string GetHeaderIncludes(const google::protobuf::FileDescriptor* file) { - string temp = +std::string GetHeaderIncludes(const google::protobuf::FileDescriptor* file) { + std::string temp = "#include \"grpc++/impl/internal_stub.h\"\n" "#include \"grpc++/status.h\"\n" "\n" @@ -121,7 +124,7 @@ string GetHeaderIncludes(const google::protobuf::FileDescriptor* file) { return temp; } -string GetSourceIncludes() { +std::string GetSourceIncludes() { return "#include \"grpc++/channel_interface.h\"\n" "#include \"grpc++/impl/rpc_method.h\"\n" "#include \"grpc++/impl/rpc_service_method.h\"\n" @@ -130,7 +133,7 @@ string GetSourceIncludes() { void PrintHeaderClientMethod(google::protobuf::io::Printer* printer, const google::protobuf::MethodDescriptor* method, - map* vars) { + std::map* vars) { (*vars)["Method"] = method->name(); (*vars)["Request"] = grpc_cpp_generator::ClassName(method->input_type(), true); @@ -159,7 +162,7 @@ void PrintHeaderClientMethod(google::protobuf::io::Printer* printer, void PrintHeaderServerMethod(google::protobuf::io::Printer* printer, const google::protobuf::MethodDescriptor* method, - map* vars) { + std::map* vars) { (*vars)["Method"] = method->name(); (*vars)["Request"] = grpc_cpp_generator::ClassName(method->input_type(), true); @@ -193,7 +196,7 @@ void PrintHeaderServerMethod(google::protobuf::io::Printer* printer, void PrintHeaderService(google::protobuf::io::Printer* printer, const google::protobuf::ServiceDescriptor* service, - map* vars) { + std::map* vars) { (*vars)["Service"] = service->name(); printer->Print(*vars, @@ -238,11 +241,11 @@ void PrintHeaderService(google::protobuf::io::Printer* printer, printer->Print("};\n"); } -string GetHeaderServices(const google::protobuf::FileDescriptor* file) { - string output; +std::string GetHeaderServices(const google::protobuf::FileDescriptor* file) { + std::string output; google::protobuf::io::StringOutputStream output_stream(&output); google::protobuf::io::Printer printer(&output_stream, '$'); - map vars; + std::map vars; for (int i = 0; i < file->service_count(); ++i) { PrintHeaderService(&printer, file->service(i), &vars); @@ -253,7 +256,7 @@ string GetHeaderServices(const google::protobuf::FileDescriptor* file) { void PrintSourceClientMethod(google::protobuf::io::Printer* printer, const google::protobuf::MethodDescriptor* method, - map* vars) { + std::map* vars) { (*vars)["Method"] = method->name(); (*vars)["Request"] = grpc_cpp_generator::ClassName(method->input_type(), true); @@ -311,7 +314,7 @@ void PrintSourceClientMethod(google::protobuf::io::Printer* printer, void PrintSourceServerMethod(google::protobuf::io::Printer* printer, const google::protobuf::MethodDescriptor* method, - map* vars) { + std::map* vars) { (*vars)["Method"] = method->name(); (*vars)["Request"] = grpc_cpp_generator::ClassName(method->input_type(), true); @@ -361,7 +364,7 @@ void PrintSourceServerMethod(google::protobuf::io::Printer* printer, void PrintSourceService(google::protobuf::io::Printer* printer, const google::protobuf::ServiceDescriptor* service, - map* vars) { + std::map* vars) { (*vars)["Service"] = service->name(); printer->Print( *vars, @@ -455,11 +458,11 @@ void PrintSourceService(google::protobuf::io::Printer* printer, printer->Print("}\n\n"); } -string GetSourceServices(const google::protobuf::FileDescriptor* file) { - string output; +std::string GetSourceServices(const google::protobuf::FileDescriptor* file) { + std::string output; google::protobuf::io::StringOutputStream output_stream(&output); google::protobuf::io::Printer printer(&output_stream, '$'); - map vars; + std::map vars; // Package string is empty or ends with a dot. It is used to fully qualify // method names. vars["Package"] = file->package(); diff --git a/src/compiler/cpp_generator.h b/src/compiler/cpp_generator.h index 8bd785f13e8..1a74211b715 100644 --- a/src/compiler/cpp_generator.h +++ b/src/compiler/cpp_generator.h @@ -42,21 +42,19 @@ class FileDescriptor; } // namespace protobuf } // namespace google -using namespace std; - namespace grpc_cpp_generator { // Return the includes needed for generated header file. -string GetHeaderIncludes(const google::protobuf::FileDescriptor* file); +std::string GetHeaderIncludes(const google::protobuf::FileDescriptor* file); // Return the includes needed for generated source file. -string GetSourceIncludes(); +std::string GetSourceIncludes(); // Return the services for generated header file. -string GetHeaderServices(const google::protobuf::FileDescriptor* file); +std::string GetHeaderServices(const google::protobuf::FileDescriptor* file); // Return the services for generated source file. -string GetSourceServices(const google::protobuf::FileDescriptor* file); +std::string GetSourceServices(const google::protobuf::FileDescriptor* file); } // namespace grpc_cpp_generator diff --git a/src/compiler/cpp_generator_helpers.h b/src/compiler/cpp_generator_helpers.h index ba251faf29a..1ad3cb2db3e 100644 --- a/src/compiler/cpp_generator_helpers.h +++ b/src/compiler/cpp_generator_helpers.h @@ -39,14 +39,12 @@ #include #include -using namespace std; - namespace grpc_cpp_generator { -inline bool StripSuffix(string* filename, const string& suffix) { +inline bool StripSuffix(std::string* filename, const std::string& suffix) { if (filename->length() >= suffix.length()) { size_t suffix_pos = filename->length() - suffix.length(); - if (filename->compare(suffix_pos, string::npos, suffix) == 0) { + if (filename->compare(suffix_pos, std::string::npos, suffix) == 0) { filename->resize(filename->size() - suffix.size()); return true; } @@ -55,19 +53,20 @@ inline bool StripSuffix(string* filename, const string& suffix) { return false; } -inline string StripProto(string filename) { +inline std::string StripProto(std::string filename) { if (!StripSuffix(&filename, ".protodevel")) { StripSuffix(&filename, ".proto"); } return filename; } -inline string StringReplace(string str, const string& from, const string& to) { +inline std::string StringReplace(std::string str, const std::string& from, + const std::string& to) { size_t pos = 0; for (;;) { pos = str.find(from, pos); - if (pos == string::npos) { + if (pos == std::string::npos) { break; } str.replace(pos, from.length(), to); @@ -77,23 +76,23 @@ inline string StringReplace(string str, const string& from, const string& to) { return str; } -inline string DotsToColons(const string& name) { +inline std::string DotsToColons(const std::string& name) { return StringReplace(name, ".", "::"); } -inline string DotsToUnderscores(const string& name) { +inline std::string DotsToUnderscores(const std::string& name) { return StringReplace(name, ".", "_"); } -inline string ClassName(const google::protobuf::Descriptor* descriptor, - bool qualified) { +inline std::string ClassName(const google::protobuf::Descriptor* descriptor, + bool qualified) { // Find "outer", the descriptor of the top-level message in which // "descriptor" is embedded. const google::protobuf::Descriptor* outer = descriptor; while (outer->containing_type() != NULL) outer = outer->containing_type(); - const string& outer_name = outer->full_name(); - string inner_name = descriptor->full_name().substr(outer_name.size()); + const std::string& outer_name = outer->full_name(); + std::string inner_name = descriptor->full_name().substr(outer_name.size()); if (qualified) { return "::" + DotsToColons(outer_name) + DotsToUnderscores(inner_name); diff --git a/src/compiler/cpp_plugin.cc b/src/compiler/cpp_plugin.cc index 17836b16125..e43278529d6 100644 --- a/src/compiler/cpp_plugin.cc +++ b/src/compiler/cpp_plugin.cc @@ -35,6 +35,8 @@ // #include +#include + #include "src/compiler/cpp_generator.h" #include "src/compiler/cpp_generator_helpers.h" #include @@ -50,9 +52,9 @@ class CppGrpcGenerator : public google::protobuf::compiler::CodeGenerator { virtual ~CppGrpcGenerator() {} virtual bool Generate(const google::protobuf::FileDescriptor* file, - const string& parameter, + const std::string& parameter, google::protobuf::compiler::GeneratorContext* context, - string* error) const { + std::string* error) const { if (file->options().cc_generic_services()) { *error = "cpp grpc proto compiler plugin does not work with generic " @@ -61,7 +63,7 @@ class CppGrpcGenerator : public google::protobuf::compiler::CodeGenerator { return false; } - string file_name = grpc_cpp_generator::StripProto(file->name()); + std::string file_name = grpc_cpp_generator::StripProto(file->name()); // Generate .pb.h Insert(context, file_name + ".pb.h", "includes", @@ -80,8 +82,8 @@ class CppGrpcGenerator : public google::protobuf::compiler::CodeGenerator { private: // Insert the given code into the given file at the given insertion point. void Insert(google::protobuf::compiler::GeneratorContext* context, - const string& filename, const string& insertion_point, - const string& code) const { + const std::string& filename, const std::string& insertion_point, + const std::string& code) const { std::unique_ptr output( context->OpenForInsert(filename, insertion_point)); google::protobuf::io::CodedOutputStream coded_out(output.get()); diff --git a/src/compiler/ruby_generator.cc b/src/compiler/ruby_generator.cc index fae8858ff72..393f8f3b593 100644 --- a/src/compiler/ruby_generator.cc +++ b/src/compiler/ruby_generator.cc @@ -32,6 +32,7 @@ */ #include +#include #include #include @@ -56,17 +57,17 @@ namespace grpc_ruby_generator { namespace { // Prints out the method using the ruby gRPC DSL. -void PrintMethod(const MethodDescriptor* method, const string& package, +void PrintMethod(const MethodDescriptor* method, const std::string& package, Printer* out) { - string input_type = RubyTypeOf(method->input_type()->name(), package); + std::string input_type = RubyTypeOf(method->input_type()->name(), package); if (method->client_streaming()) { input_type = "stream(" + input_type + ")"; } - string output_type = RubyTypeOf(method->output_type()->name(), package); + std::string output_type = RubyTypeOf(method->output_type()->name(), package); if (method->server_streaming()) { output_type = "stream(" + output_type + ")"; } - map method_vars = ListToDict({ + std::map method_vars = ListToDict({ "mth.name", method->name(), "input.type", input_type, "output.type", output_type, }); @@ -74,22 +75,22 @@ void PrintMethod(const MethodDescriptor* method, const string& package, } // Prints out the service using the ruby gRPC DSL. -void PrintService(const ServiceDescriptor* service, const string& package, +void PrintService(const ServiceDescriptor* service, const std::string& package, Printer* out) { if (service->method_count() == 0) { return; } // Begin the service module - map module_vars = ListToDict({ + std::map module_vars = ListToDict({ "module.name", CapitalizeFirst(service->name()), }); out->Print(module_vars, "module $module.name$\n"); out->Indent(); // TODO(temiola): add documentation - string doc = "TODO: add proto service documentation here"; - map template_vars = ListToDict({ + std::string doc = "TODO: add proto service documentation here"; + std::map template_vars = ListToDict({ "Documentation", doc, }); out->Print("\n"); @@ -103,7 +104,7 @@ void PrintService(const ServiceDescriptor* service, const string& package, out->Print("\n"); out->Print("self.marshal_class_method = :encode\n"); out->Print("self.unmarshal_class_method = :decode\n"); - map pkg_vars = ListToDict({ + std::map pkg_vars = ListToDict({ "service.name", service->name(), "pkg.name", package, }); out->Print(pkg_vars, "self.service_name = '$pkg.name$.$service.name$'\n"); @@ -124,8 +125,8 @@ void PrintService(const ServiceDescriptor* service, const string& package, } // namespace -string GetServices(const FileDescriptor* file) { - string output; +std::string GetServices(const FileDescriptor* file) { + std::string output; StringOutputStream output_stream(&output); Printer out(&output_stream, '$'); @@ -136,7 +137,7 @@ string GetServices(const FileDescriptor* file) { } // Write out a file header. - map header_comment_vars = ListToDict({ + std::map header_comment_vars = ListToDict({ "file.name", file->name(), "file.package", file->package(), }); out.Print("# Generated by the protocol buffer compiler. DO NOT EDIT!\n"); @@ -148,16 +149,16 @@ string GetServices(const FileDescriptor* file) { // Write out require statemment to import the separately generated file // that defines the messages used by the service. This is generated by the // main ruby plugin. - map dep_vars = ListToDict({ + std::map dep_vars = ListToDict({ "dep.name", MessagesRequireName(file), }); out.Print(dep_vars, "require '$dep.name$'\n"); // Write out services within the modules out.Print("\n"); - vector modules = Split(file->package(), '.'); + std::vector modules = Split(file->package(), '.'); for (size_t i = 0; i < modules.size(); ++i) { - map module_vars = ListToDict({ + std::map module_vars = ListToDict({ "module.name", CapitalizeFirst(modules[i]), }); out.Print(module_vars, "module $module.name$\n"); diff --git a/src/compiler/ruby_generator.h b/src/compiler/ruby_generator.h index 4f13f1a33b0..0306536d08a 100644 --- a/src/compiler/ruby_generator.h +++ b/src/compiler/ruby_generator.h @@ -36,8 +36,6 @@ #include -using namespace std; - namespace google { namespace protobuf { class FileDescriptor; @@ -46,7 +44,7 @@ class FileDescriptor; namespace grpc_ruby_generator { -string GetServices(const google::protobuf::FileDescriptor* file); +std::string GetServices(const google::protobuf::FileDescriptor* file); } // namespace grpc_ruby_generator diff --git a/src/compiler/ruby_generator_helpers-inl.h b/src/compiler/ruby_generator_helpers-inl.h index 8263e353b24..7b973ed9ebe 100644 --- a/src/compiler/ruby_generator_helpers-inl.h +++ b/src/compiler/ruby_generator_helpers-inl.h @@ -42,7 +42,7 @@ namespace grpc_ruby_generator { inline bool ServicesFilename(const google::protobuf::FileDescriptor* file, - string* file_name_or_error) { + std::string* file_name_or_error) { // Get output file name. static const unsigned proto_suffix_length = 6; // length of ".proto" if (file->name().size() > proto_suffix_length && @@ -57,7 +57,7 @@ inline bool ServicesFilename(const google::protobuf::FileDescriptor* file, } } -inline string MessagesRequireName( +inline std::string MessagesRequireName( const google::protobuf::FileDescriptor* file) { return Replace(file->name(), ".proto", ""); } diff --git a/src/compiler/ruby_generator_map-inl.h b/src/compiler/ruby_generator_map-inl.h index fa86fbb9569..a49650a2f7b 100644 --- a/src/compiler/ruby_generator_map-inl.h +++ b/src/compiler/ruby_generator_map-inl.h @@ -48,17 +48,18 @@ namespace grpc_ruby_generator { // Converts an initializer list of the form { key0, value0, key1, value1, ... } // into a map of key* to value*. Is merely a readability helper for later code. -inline map ListToDict(const initializer_list& values) { +inline std::map ListToDict( + const initializer_list& values) { if (values.size() % 2 != 0) { // MOE: insert std::cerr << "Not every 'key' has a value in `values`." // << std::endl; } - map value_map; + std::map value_map; auto value_iter = values.begin(); for (unsigned i = 0; i < values.size() / 2; ++i) { - string key = *value_iter; + std::string key = *value_iter; ++value_iter; - string value = *value_iter; + std::string value = *value_iter; value_map[key] = value; ++value_iter; } diff --git a/src/compiler/ruby_generator_string-inl.h b/src/compiler/ruby_generator_string-inl.h index 44e17a202a7..a253f78d49a 100644 --- a/src/compiler/ruby_generator_string-inl.h +++ b/src/compiler/ruby_generator_string-inl.h @@ -45,10 +45,10 @@ using std::transform; namespace grpc_ruby_generator { // Split splits a string using char into elems. -inline vector& Split(const string& s, char delim, - vector* elems) { - stringstream ss(s); - string item; +inline std::vector& Split(const std::string& s, char delim, + std::vector* elems) { + std::stringstream ss(s); + std::string item; while (getline(ss, item, delim)) { elems->push_back(item); } @@ -56,16 +56,17 @@ inline vector& Split(const string& s, char delim, } // Split splits a string using char, returning the result in a vector. -inline vector Split(const string& s, char delim) { - vector elems; +inline std::vector Split(const std::string& s, char delim) { + std::vector elems; Split(s, delim, &elems); return elems; } // Replace replaces from with to in s. -inline string Replace(string s, const string& from, const string& to) { +inline std::string Replace(std::string s, const std::string& from, + const std::string& to) { size_t start_pos = s.find(from); - if (start_pos == string::npos) { + if (start_pos == std::string::npos) { return s; } s.replace(start_pos, from.length(), to); @@ -73,10 +74,10 @@ inline string Replace(string s, const string& from, const string& to) { } // ReplaceAll replaces all instances of search with replace in s. -inline string ReplaceAll(string s, const string& search, - const string& replace) { +inline std::string ReplaceAll(std::string s, const std::string& search, + const std::string& replace) { size_t pos = 0; - while ((pos = s.find(search, pos)) != string::npos) { + while ((pos = s.find(search, pos)) != std::string::npos) { s.replace(pos, search.length(), replace); pos += replace.length(); } @@ -84,9 +85,10 @@ inline string ReplaceAll(string s, const string& search, } // ReplacePrefix replaces from with to in s if search is a prefix of s. -inline bool ReplacePrefix(string* s, const string& from, const string& to) { +inline bool ReplacePrefix(std::string* s, const std::string& from, + const std::string& to) { size_t start_pos = s->find(from); - if (start_pos == string::npos || start_pos != 0) { + if (start_pos == std::string::npos || start_pos != 0) { return false; } s->replace(start_pos, from.length(), to); @@ -94,7 +96,7 @@ inline bool ReplacePrefix(string* s, const string& from, const string& to) { } // CapitalizeFirst capitalizes the first char in a string. -inline string CapitalizeFirst(string s) { +inline std::string CapitalizeFirst(std::string s) { if (s.empty()) { return s; } @@ -103,14 +105,15 @@ inline string CapitalizeFirst(string s) { } // RubyTypeOf updates a proto type to the required ruby equivalent. -inline string RubyTypeOf(const string& a_type, const string& package) { - string res(a_type); +inline std::string RubyTypeOf(const std::string& a_type, + const std::string& package) { + std::string res(a_type); ReplacePrefix(&res, package, ""); // remove the leading package if present ReplacePrefix(&res, ".", ""); // remove the leading . (no package) - if (res.find('.') == string::npos) { + if (res.find('.') == std::string::npos) { return res; } else { - vector prefixes_and_type = Split(res, '.'); + std::vector prefixes_and_type = Split(res, '.'); for (unsigned int i = 0; i < prefixes_and_type.size(); ++i) { if (i != 0) { res += "::"; // switch '.' to the ruby module delim diff --git a/src/compiler/ruby_plugin.cc b/src/compiler/ruby_plugin.cc index c1748cfaeb0..86c0e11e1c1 100644 --- a/src/compiler/ruby_plugin.cc +++ b/src/compiler/ruby_plugin.cc @@ -37,6 +37,7 @@ // and net/proto2/compiler/public/plugin.h for more information on plugins. #include +#include #include "src/compiler/ruby_generator.h" #include "src/compiler/ruby_generator_helpers-inl.h" @@ -52,16 +53,16 @@ class RubyGrpcGenerator : public google::protobuf::compiler::CodeGenerator { ~RubyGrpcGenerator() override {} bool Generate(const google::protobuf::FileDescriptor* file, - const string& parameter, + const std::string& parameter, google::protobuf::compiler::GeneratorContext* context, - string* error) const override { - string code = grpc_ruby_generator::GetServices(file); + std::string* error) const override { + std::string code = grpc_ruby_generator::GetServices(file); if (code.size() == 0) { return true; // don't generate a file if there are no services } // Get output file name. - string file_name; + std::string file_name; if (!grpc_ruby_generator::ServicesFilename(file, &file_name)) { return false; }