OpenSDN source code
t_generator_registry.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  * Modifications Copyright (c) Juniper Networks, Inc.
20  * Copyright Contributors to the OpenSDN project
21  */
22 
23 #ifndef T_GENERATOR_REGISTRY_H
24 #define T_GENERATOR_REGISTRY_H
25 
26 class t_generator;
27 
36  public:
37  t_generator_factory(const std::string& short_name,
38  const std::string& long_name,
39  const std::string& documentation);
40 
41  virtual ~t_generator_factory() {}
42 
44  // The program to generate.
45  t_program* program,
46  // Note: parsed_options will not exist beyond the call to get_generator.
47  const std::map<std::string, std::string>& parsed_options,
48  // Note: option_string might not exist beyond the call to get_generator.
49  const std::string& option_string)
50  = 0;
51 
52  virtual bool is_valid_namespace(const std::string& sub_namespace) = 0;
53 
54  std::string get_short_name() { return short_name_; }
55  std::string get_long_name() { return long_name_; }
56  std::string get_documentation() { return documentation_; }
57 
58  private:
59  std::string short_name_;
60  std::string long_name_;
61  std::string documentation_;
62 };
63 
64 template <typename generator>
66  public:
67  t_generator_factory_impl(const std::string& short_name,
68  const std::string& long_name,
69  const std::string& documentation)
70  : t_generator_factory(short_name, long_name, documentation)
71  {}
72 
74  t_program* program,
75  const std::map<std::string, std::string>& parsed_options,
76  const std::string& option_string) {
77  return new generator(program, parsed_options, option_string);
78  }
79 
80  virtual bool is_valid_namespace(const std::string& sub_namespace) {
81  return generator::is_valid_namespace(sub_namespace);
82  }
83 };
84 
86  public:
87  static void register_generator(t_generator_factory* factory);
88 
89  static t_generator* get_generator(t_program* program,
90  const std::string& options);
91 
92  typedef std::map<std::string, t_generator_factory*> gen_map_t;
93  static gen_map_t& get_generator_map();
94 
95  private:
98 };
99 
100 #define THRIFT_REGISTER_GENERATOR(language, long_name, doc) \
101  class t_##language##_generator_factory_impl \
102  : public t_generator_factory_impl<t_##language##_generator> \
103  { \
104  public: \
105  t_##language##_generator_factory_impl() \
106  : t_generator_factory_impl<t_##language##_generator>( \
107  #language, long_name, doc) \
108  {} \
109  }; \
110  static t_##language##_generator_factory_impl _registerer;
111 
112 #endif
virtual bool is_valid_namespace(const std::string &sub_namespace)
virtual t_generator * get_generator(t_program *program, const std::map< std::string, std::string > &parsed_options, const std::string &option_string)
t_generator_factory_impl(const std::string &short_name, const std::string &long_name, const std::string &documentation)
virtual t_generator * get_generator(t_program *program, const std::map< std::string, std::string > &parsed_options, const std::string &option_string)=0
t_generator_factory(const std::string &short_name, const std::string &long_name, const std::string &documentation)
Definition: t_generator.cc:183
std::string get_documentation()
std::string get_short_name()
virtual bool is_valid_namespace(const std::string &sub_namespace)=0
static gen_map_t & get_generator_map()
Definition: t_generator.cc:177
static t_generator * get_generator(t_program *program, const std::string &options)
Definition: t_generator.cc:140
std::map< std::string, t_generator_factory * > gen_map_t
t_generator_registry(const t_generator_registry &)
static void register_generator(t_generator_factory *factory)
Definition: t_generator.cc:132