From: Jake Farrell Date: Fri, 22 Jun 2012 03:22:53 +0000 (+0000) Subject: Thrift-1567:Thrift/cpp: Allow alternate classes to be used for strings X-Git-Tag: 0.9.1~338 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=f42ae012aa57d476a3ca44542ccfd7801cac3587;p=common%2Fthrift.git Thrift-1567:Thrift/cpp: Allow alternate classes to be used for strings Client: cpp Patch: dreiss The goal of this diff is to allow Thrift strings to be used without depending on std::string, since it looks like we're starting to move away from std::string instead of moving to a better implementation. git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1352765 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/compiler/cpp/src/generate/t_cpp_generator.cc b/compiler/cpp/src/generate/t_cpp_generator.cc index 20b991de..f4136e1f 100644 --- a/compiler/cpp/src/generate/t_cpp_generator.cc +++ b/compiler/cpp/src/generate/t_cpp_generator.cc @@ -69,6 +69,9 @@ class t_cpp_generator : public t_oop_generator { iter = parsed_options.find("templates"); gen_templates_ = (iter != parsed_options.end()); + gen_templates_only_ = + (iter != parsed_options.end() && iter->second == "only"); + out_dir_base_ = "gen-cpp"; } @@ -253,7 +256,13 @@ class t_cpp_generator : public t_oop_generator { bool gen_templates_; /** - * True if we should use a path prefix in our #include statements for other + * True iff we should generate process function pointers for only templatized + * reader/writer methods. + */ + bool gen_templates_only_; + + /** + * True iff we should use a path prefix in our #include statements for other * thrift-generated header files. */ bool use_include_prefix_; @@ -2896,11 +2905,15 @@ void ProcessorGenerator::generate_class_definition() { f_header_ << indent() << "processMap_[\"" << (*f_iter)->get_name() << "\"] = "; if (generator_->gen_templates_) { - f_header_ << "ProcessFunctions(" << endl << - indent() << " &" << class_name_ << "::process_" << - (*f_iter)->get_name() << "," << endl << - indent() << " &" << class_name_ << "::process_" << - (*f_iter)->get_name() << ")"; + f_header_ << "ProcessFunctions(" << endl; + if (generator_->gen_templates_only_) { + indent(f_header_) << " NULL," << endl; + } else { + indent(f_header_) << " &" << class_name_ << "::process_" << + (*f_iter)->get_name() << "," << endl; + } + indent(f_header_) << " &" << class_name_ << "::process_" << + (*f_iter)->get_name() << ")"; } else { f_header_ << "&" << class_name_ << "::process_" << (*f_iter)->get_name(); } @@ -2988,7 +3001,12 @@ void ProcessorGenerator::generate_dispatch_call(bool template_protocol) { f_out_ << indent() << "(this->*(pfn->second.specialized))"; } else { - if (generator_->gen_templates_) { + if (generator_->gen_templates_only_) { + // TODO: This is a null pointer, so nothing good will come from calling + // it. Throw an exception instead. + f_out_ << + indent() << "(this->*(pfn->second.generic))"; + } else if (generator_->gen_templates_) { f_out_ << indent() << "(this->*(pfn->second.generic))"; } else { @@ -4246,6 +4264,11 @@ string t_cpp_generator::namespace_close(string ns) { string t_cpp_generator::type_name(t_type* ttype, bool in_typedef, bool arg) { if (ttype->is_base_type()) { string bname = base_type_name(((t_base_type*)ttype)->get_base()); + std::map::iterator it = ttype->annotations_.find("cpp.type"); + if (it != ttype->annotations_.end()) { + bname = it->second; + } + if (!arg) { return bname; } diff --git a/lib/cpp/src/thrift/protocol/TBinaryProtocol.h b/lib/cpp/src/thrift/protocol/TBinaryProtocol.h index 37d33f48..b762ad4c 100644 --- a/lib/cpp/src/thrift/protocol/TBinaryProtocol.h +++ b/lib/cpp/src/thrift/protocol/TBinaryProtocol.h @@ -134,7 +134,8 @@ class TBinaryProtocolT inline uint32_t writeDouble(const double dub); - inline uint32_t writeString(const std::string& str); + template + inline uint32_t writeString(const StrType& str); inline uint32_t writeBinary(const std::string& str); @@ -187,12 +188,14 @@ class TBinaryProtocolT inline uint32_t readDouble(double& dub); - inline uint32_t readString(std::string& str); + template + inline uint32_t readString(StrType& str); inline uint32_t readBinary(std::string& str); protected: - uint32_t readStringBody(std::string& str, int32_t sz); + template + uint32_t readStringBody(StrType& str, int32_t sz); Transport_* trans_; diff --git a/lib/cpp/src/thrift/protocol/TBinaryProtocol.tcc b/lib/cpp/src/thrift/protocol/TBinaryProtocol.tcc index a95fdba2..f3f38f70 100644 --- a/lib/cpp/src/thrift/protocol/TBinaryProtocol.tcc +++ b/lib/cpp/src/thrift/protocol/TBinaryProtocol.tcc @@ -176,7 +176,8 @@ uint32_t TBinaryProtocolT::writeDouble(const double dub) { template -uint32_t TBinaryProtocolT::writeString(const std::string& str) { +template +uint32_t TBinaryProtocolT::writeString(const StrType& str) { uint32_t size = str.size(); uint32_t result = writeI32((int32_t)size); if (size > 0) { @@ -401,7 +402,8 @@ uint32_t TBinaryProtocolT::readDouble(double& dub) { } template -uint32_t TBinaryProtocolT::readString(std::string& str) { +template +uint32_t TBinaryProtocolT::readString(StrType& str) { uint32_t result; int32_t size; result = readI32(size); @@ -414,7 +416,8 @@ uint32_t TBinaryProtocolT::readBinary(std::string& str) { } template -uint32_t TBinaryProtocolT::readStringBody(std::string& str, +template +uint32_t TBinaryProtocolT::readStringBody(StrType& str, int32_t size) { uint32_t result = 0;