From: Bryan Duxbury Date: Fri, 20 Aug 2010 16:42:04 +0000 (+0000) Subject: compiler: make a standard way for language generators to accept sub-namespaces X-Git-Tag: 0.5.0~160 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=681f5eae6dee4e317248145be07be0b6ded5c1e8;p=common%2Fthrift.git 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 2635bf5d..0a4dace5 100644 --- a/compiler/cpp/src/generate/t_generator.h +++ b/compiler/cpp/src/generate/t_generator.h @@ -65,6 +65,16 @@ class t_generator { const std::string& contents, 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. */ diff --git a/compiler/cpp/src/generate/t_generator_registry.h b/compiler/cpp/src/generate/t_generator_registry.h index 8cadfae5..98a484d8 100644 --- a/compiler/cpp/src/generate/t_generator_registry.h +++ b/compiler/cpp/src/generate/t_generator_registry.h @@ -46,6 +46,8 @@ class t_generator_factory { 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 @@ class t_generator_factory_impl : public t_generator_factory { 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 2ed76a4c..42c8f933 100644 --- a/compiler/cpp/src/generate/t_st_generator.cc +++ b/compiler/cpp/src/generate/t_st_generator.cc @@ -181,6 +181,10 @@ string t_st_generator::class_name() { 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 68697c6e..f7556434 100644 --- a/compiler/cpp/src/parse/t_program.h +++ b/compiler/cpp/src/parse/t_program.h @@ -160,16 +160,26 @@ class t_program : public t_doc { // 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; }