Merge pull request #72 from nicolasnoble/no-std

Removing "using namespace std" everywhere.
pull/77/merge
Yang Gao 10 years ago
commit 56ac94f2fe
  1. 33
      src/compiler/cpp_generator.cc
  2. 10
      src/compiler/cpp_generator.h
  3. 25
      src/compiler/cpp_generator_helpers.h
  4. 12
      src/compiler/cpp_plugin.cc
  5. 31
      src/compiler/ruby_generator.cc
  6. 4
      src/compiler/ruby_generator.h
  7. 4
      src/compiler/ruby_generator_helpers-inl.h
  8. 9
      src/compiler/ruby_generator_map-inl.h
  9. 39
      src/compiler/ruby_generator_string-inl.h
  10. 9
      src/compiler/ruby_plugin.cc

@ -31,6 +31,9 @@
*
*/
#include <string>
#include <map>
#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<string, string>* vars) {
std::map<std::string, std::string>* 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<string, string>* vars) {
std::map<std::string, std::string>* 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<string, string>* vars) {
std::map<std::string, std::string>* 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<string, string> vars;
std::map<std::string, std::string> 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<string, string>* vars) {
std::map<std::string, std::string>* 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<string, string>* vars) {
std::map<std::string, std::string>* 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<string, string>* vars) {
std::map<std::string, std::string>* 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<string, string> vars;
std::map<std::string, std::string> vars;
// Package string is empty or ends with a dot. It is used to fully qualify
// method names.
vars["Package"] = file->package();

@ -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

@ -39,14 +39,12 @@
#include <google/protobuf/descriptor.h>
#include <google/protobuf/descriptor.pb.h>
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);

@ -35,6 +35,8 @@
//
#include <memory>
#include <string>
#include "src/compiler/cpp_generator.h"
#include "src/compiler/cpp_generator_helpers.h"
#include <google/protobuf/descriptor.h>
@ -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<google::protobuf::io::ZeroCopyOutputStream> output(
context->OpenForInsert(filename, insertion_point));
google::protobuf::io::CodedOutputStream coded_out(output.get());

@ -32,6 +32,7 @@
*/
#include <cctype>
#include <string>
#include <map>
#include <vector>
@ -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<string, string> method_vars = ListToDict({
std::map<std::string, std::string> 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<string, string> module_vars = ListToDict({
std::map<std::string, std::string> 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<string, string> template_vars = ListToDict({
std::string doc = "TODO: add proto service documentation here";
std::map<std::string, std::string> 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<string, string> pkg_vars = ListToDict({
std::map<std::string, std::string> 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<string, string> header_comment_vars = ListToDict({
std::map<std::string, std::string> 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<string, string> dep_vars = ListToDict({
std::map<std::string, std::string> 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<string> modules = Split(file->package(), '.');
std::vector<std::string> modules = Split(file->package(), '.');
for (size_t i = 0; i < modules.size(); ++i) {
map<string, string> module_vars = ListToDict({
std::map<std::string, std::string> module_vars = ListToDict({
"module.name", CapitalizeFirst(modules[i]),
});
out.Print(module_vars, "module $module.name$\n");

@ -36,8 +36,6 @@
#include <string>
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

@ -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", "");
}

@ -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<string, string> ListToDict(const initializer_list<string>& values) {
inline std::map<std::string, std::string> ListToDict(
const initializer_list<std::string>& values) {
if (values.size() % 2 != 0) {
// MOE: insert std::cerr << "Not every 'key' has a value in `values`."
// << std::endl;
}
map<string, string> value_map;
std::map<std::string, std::string> 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;
}

@ -45,10 +45,10 @@ using std::transform;
namespace grpc_ruby_generator {
// Split splits a string using char into elems.
inline vector<string>& Split(const string& s, char delim,
vector<string>* elems) {
stringstream ss(s);
string item;
inline std::vector<std::string>& Split(const std::string& s, char delim,
std::vector<std::string>* elems) {
std::stringstream ss(s);
std::string item;
while (getline(ss, item, delim)) {
elems->push_back(item);
}
@ -56,16 +56,17 @@ inline vector<string>& Split(const string& s, char delim,
}
// Split splits a string using char, returning the result in a vector.
inline vector<string> Split(const string& s, char delim) {
vector<string> elems;
inline std::vector<std::string> Split(const std::string& s, char delim) {
std::vector<std::string> 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<string> prefixes_and_type = Split(res, '.');
std::vector<std::string> 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

@ -37,6 +37,7 @@
// and net/proto2/compiler/public/plugin.h for more information on plugins.
#include <memory>
#include <string>
#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;
}

Loading…
Cancel
Save