Generate a null implementation of thrift C++ class for easy subclassing
authorMark Slee <mcslee@apache.org>
Fri, 8 Dec 2006 19:15:35 +0000 (19:15 +0000)
committerMark Slee <mcslee@apache.org>
Fri, 8 Dec 2006 19:15:35 +0000 (19:15 +0000)
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

compiler/cpp/src/generate/t_cpp_generator.cc
compiler/cpp/src/generate/t_cpp_generator.h

index 9188955..645e085 100644 (file)
@@ -684,6 +684,7 @@ void t_cpp_generator::generate_service(t_service* tservice) {
 
   // Generate all the components
   generate_service_interface(tservice);
+  generate_service_null(tservice);
   generate_service_helpers(tservice);
   generate_service_client(tservice);
   generate_service_processor(tservice);
@@ -750,6 +751,48 @@ void t_cpp_generator::generate_service_interface(t_service* tservice) {
     "}; " << endl << endl;
 }
 
+/**
+ * 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
index 435b0c6..fdab3ee 100644 (file)
@@ -52,6 +52,7 @@ class t_cpp_generator : public t_oop_generator {
    */
 
   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);