Turi Create  4.0
toolkit_class_registry.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_UNITY_TOOLKIT_CLASS_REGISTRY_HPP
7 #define TURI_UNITY_TOOLKIT_CLASS_REGISTRY_HPP
8 #include <model_server/lib/toolkit_class_specification.hpp>
9 
10 namespace turi {
11 class model_base;
12 /**
13  * \ingroup unity
14  * Defines a collection of models. Has the ability to add/register new
15  * toolkits, and get information about the model.
16  */
18  public:
19  /**
20  * Register a model (name, constructor) pair.
21  *
22  * Returns false if the model name already exists.
23  *
24  * The optional "description" argument describing the model.
25  * The following keys are recognized.
26  * - "functions": A dictionary with key: function name, and value,
27  * a list of input parameters.
28  * - "get_properties": The list of all readable properties of the model
29  * - "set_properties": The list of all writable properties of the model
30  */
31  typedef std::map<std::string, flexible_type> toolkit_class_description_type;
32 
33  /**
34  * Registers a toolkit class.
35  */
36  bool register_toolkit_class(const std::string& class_name,
37  std::function<model_base*()> constructor,
38  toolkit_class_description_type description = toolkit_class_description_type());
39 
40  /**
41  * Registers a toolkit class.
42  */
43  bool register_toolkit_class(std::vector<toolkit_class_specification> classes,
44  std::string prefix = "");
45 
46  /**
47  * Creates a new model object with the given model_name.
48  *
49  * Throws an exception if the model_name was not registered.
50  */
51  std::shared_ptr<model_base> get_toolkit_class(const std::string& class_name);
52 
53  /**
54  * Returns the description associated with the model.
55  *
56  * Throws an exception if the model_name was not registered.
57  */
58  std::map<std::string, flexible_type> get_toolkit_class_description(const std::string& toolkit_class_name);
59  /**
60  * Returns a list of names of all registered models.
61  *
62  * \returns A vector of string where each string is a model name.
63  */
64  std::vector<std::string> available_toolkit_classes();
65 
66  private:
67  std::map<std::string, std::function<model_base*()> > registry;
68  std::map<std::string, std::map<std::string, flexible_type> > descriptions;
69 };
70 
71 } // turicreate
72 
73 #endif // TURI_UNITY_toolkit_class_registry
bool register_toolkit_class(const std::string &class_name, std::function< model_base *()> constructor, toolkit_class_description_type description=toolkit_class_description_type())
std::shared_ptr< model_base > get_toolkit_class(const std::string &class_name)
std::map< std::string, flexible_type > toolkit_class_description_type
std::map< std::string, flexible_type > get_toolkit_class_description(const std::string &toolkit_class_name)
std::vector< std::string > available_toolkit_classes()