compiler: make a standard way for language generators to accept sub-namespaces

This patch adds a new method to t_generator that allows the compiler to avoid special cases in checking for sub-namespaces in the Thrift IDL.

Patch: Bruce Lowekamp

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@987565 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/generate/t_generator.h b/compiler/cpp/src/generate/t_generator.h
index 2635bf5..0a4dace 100644
--- a/compiler/cpp/src/generate/t_generator.h
+++ b/compiler/cpp/src/generate/t_generator.h
@@ -66,6 +66,16 @@
                                   const std::string& comment_end);
 
   /**
+   * check whether sub-namespace declaraction is used by generator.
+   * e.g. allow 
+   * namespace py.twisted bar
+   * to specify namespace to use when -gen py:twisted is specified.
+   * Will be called with subnamespace, i.e. is_valid_namespace("twisted")
+   * will be called for the above example.
+   */
+  static bool is_valid_namespace(const std::string& sub_namespace) { return false; }
+
+  /**
    * Escape string to use one in generated sources.
    */
   virtual std::string escape_string(const std::string &in) const;
diff --git a/compiler/cpp/src/generate/t_generator_registry.h b/compiler/cpp/src/generate/t_generator_registry.h
index 8cadfae..98a484d 100644
--- a/compiler/cpp/src/generate/t_generator_registry.h
+++ b/compiler/cpp/src/generate/t_generator_registry.h
@@ -46,6 +46,8 @@
       const std::string& option_string)
     = 0;
 
+  virtual bool is_valid_namespace(const std::string& sub_namespace) = 0;
+
   std::string get_short_name() { return short_name_; }
   std::string get_long_name() { return long_name_; }
   std::string get_documentation() { return documentation_; }
@@ -71,6 +73,10 @@
       const std::string& option_string) {
     return new generator(program, parsed_options, option_string);
   }
+
+  bool is_valid_namespace(const std::string& sub_namespace){
+    return generator::is_valid_namespace(sub_namespace);
+  }
 };
 
 class t_generator_registry {
diff --git a/compiler/cpp/src/generate/t_st_generator.cc b/compiler/cpp/src/generate/t_st_generator.cc
index 2ed76a4..42c8f93 100644
--- a/compiler/cpp/src/generate/t_st_generator.cc
+++ b/compiler/cpp/src/generate/t_st_generator.cc
@@ -181,6 +181,10 @@
   return capitalize(program_name_);
 }
 
+static bool is_valid_namespace(const std::string& sub_namespace) {
+  return sub_namespace == "prefix" || sub_namespace == "category";
+}
+
 string t_st_generator::prefix(string class_name) {
   string prefix = program_->get_namespace("smalltalk.prefix");
   string name = capitalize(class_name);
diff --git a/compiler/cpp/src/parse/t_program.h b/compiler/cpp/src/parse/t_program.h
index 68697c6..f755643 100644
--- a/compiler/cpp/src/parse/t_program.h
+++ b/compiler/cpp/src/parse/t_program.h
@@ -160,16 +160,26 @@
 
   // Language neutral namespace / packaging
   void set_namespace(std::string language, std::string name_space) {
+    size_t sub_index = language.find('.');
+    std::string base_language = language.substr(0, sub_index);
+    std::string sub_namespace;
+
     t_generator_registry::gen_map_t my_copy = t_generator_registry::get_generator_map();
 
     t_generator_registry::gen_map_t::iterator it;
-    it=my_copy.find(language);
+    it=my_copy.find(base_language);
 
     if (it == my_copy.end()) {
-      if (language != "smalltalk.prefix" && language != "smalltalk.package") {
-        throw "No generator named '" + language + "' could be found!";
-      }
+      throw "No generator named '" + base_language + "' could be found!";
     }
+
+    if (sub_index != std::string::npos) {
+      std::string sub_namespace = language.substr(sub_index+1);
+      if(! it->second->is_valid_namespace(sub_namespace)) {
+        throw base_language +" generator does not accept '" + sub_namespace + "' as sub-namespace!";
+      }
+    } 
+
     namespaces_[language] = name_space;
   }