Generate a null implementation of thrift C++ class for easy subclassing
Summary: Sometimes you just want a subclass that only implements one method... annoying to fill in nulls for the others, so use the generated null class
Reviewed By: tbr-aditya
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664888 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 9188955..645e085 100644
--- a/compiler/cpp/src/generate/t_cpp_generator.cc
+++ b/compiler/cpp/src/generate/t_cpp_generator.cc
@@ -684,6 +684,7 @@
// Generate all the components
generate_service_interface(tservice);
+ generate_service_null(tservice);
generate_service_helpers(tservice);
generate_service_client(tservice);
generate_service_processor(tservice);
@@ -751,6 +752,48 @@
}
/**
+ * Generates a null implementation of the service.
+ *
+ * @param tservice The service to generate a header definition for
+ */
+void t_cpp_generator::generate_service_null(t_service* tservice) {
+ string extends = "";
+ if (tservice->get_extends() != NULL) {
+ extends = " : virtual public " + type_name(tservice->get_extends()) + "Null";
+ }
+ f_header_ <<
+ "class " << service_name_ << "Null : virtual public " << service_name_ << "If" << extends << " {" << endl <<
+ " public: " << endl;
+ indent_up();
+ f_header_ <<
+ indent() << "virtual ~" << service_name_ << "If() {}" << endl;
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::iterator f_iter;
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ f_header_ <<
+ indent() << function_signature(*f_iter) << " {" << endl;
+ indent_up();
+ t_type* returntype = (*f_iter)->get_returntype();
+ if (returntype->is_void()) {
+ f_header_ <<
+ indent() << "return;" << endl;
+ } else {
+ t_field returnfield(returntype, "rval");
+ f_header_ <<
+ indent() << declare_field(&returnfield, true) << endl <<
+ indent() << "return rval;" << endl;
+ }
+ indent_down();
+ f_header_ <<
+ indent() << "}" << endl;
+ }
+ indent_down();
+ f_header_ <<
+ "}; " << endl << endl;
+}
+
+
+/**
* Generates a multiface, which is a single server that just takes a set
* of objects implementing the interface and calls them all, returning the
* value of the last one to be called.
diff --git a/compiler/cpp/src/generate/t_cpp_generator.h b/compiler/cpp/src/generate/t_cpp_generator.h
index 435b0c6..fdab3ee 100644
--- a/compiler/cpp/src/generate/t_cpp_generator.h
+++ b/compiler/cpp/src/generate/t_cpp_generator.h
@@ -52,6 +52,7 @@
*/
void generate_service_interface (t_service* tservice);
+ void generate_service_null (t_service* tservice);
void generate_service_multiface (t_service* tservice);
void generate_service_helpers (t_service* tservice);
void generate_service_client (t_service* tservice);