Turi Create  4.0
toolkit_function_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_TOOLKIT_FUNCTION_REGISTRY_HPP
7 #define TURI_TOOLKIT_FUNCTION_REGISTRY_HPP
8 #include <map>
9 #include <string>
10 #include <model_server/lib/toolkit_function_specification.hpp>
11 #include <model_server/lib/api/function_closure_info.hpp>
12 namespace turi {
13 
14 /**
15  * \ingroup unity
16  * Defines a collection of toolkits. Has the ability to add/register new
17  * toolkits, and get information about the toolkits.
18  */
20  private:
21  std::map<std::string, toolkit_function_specification> registry;
22 
23  public:
24 
25  // default constructor. Does nothing
27 
28  /**
29  * Registers a toolkit specification. See \ref toolkit_function_specification
30  * for details. After registration, information about the toolkit will be
31  * queryable via the other toolkit_function_registry function. \ref unregister_toolkit_function
32  * will remove the toolkit from the registry.
33  *
34  * \returns Returns true on success. Returns false if some other toolkit with
35  * the same name has already been registered.
36  */
38  std::string prefix = "");
39 
40  /**
41  * Registers a collection of toolkit specifications. See \ref
42  * toolkit_function_specification for details. After registration, information about
43  * the toolkits will be queryable via the other toolkit_function_registry function.
44  * \ref unregister_toolkit_function will remove the toolkit from the registry.
45  *
46  * \returns Returns true on success. Returns false if some other toolkit with
47  * the same name has already been registered, in which case, none of the
48  * toolkits listed in spec will be registered.
49  */
50  bool register_toolkit_function(std::vector<toolkit_function_specification> spec,
51  std::string prefix = "");
52 
53 
54  /**
55  * Unregisters a previously registered toolkit.
56  *
57  * \returns Returns true on success. Returns false if a toolkit with
58  * the specified name has not been registered.
59  */
60  bool unregister_toolkit_function(std::string name);
61 
62  /**
63  * Gets the complete specification infomation about a toolkit.
64  *
65  * \returns Returns a pointer to the toolkit_function_specification on success.
66  * Returns NULL if a toolkit with the specified name has not been registered.
67  *
68  * \note Registering, or unregistering toolkits invalidates this pointer.
69  */
70  const toolkit_function_specification* get_toolkit_function_info(std::string toolkit_fn_name);
71 
72  /**
73  * Returns the natively callable version of a toolkit function if available.
74  * Raises an exception otherwise.
75  */
76  std::function<variant_type(const std::vector<variant_type>&)>
77  get_native_function(std::string name);
78 
79 
80  /**
81  * Returns the natively callable version of a toolkit function with
82  * closure information associated.
83  * Raises an exception otherwise.
84  */
85  std::function<variant_type(const std::vector<variant_type>&)>
87 
88  /**
89  * Returns a list of names of all registered toolkits.
90  *
91  * \returns A vector of string where each string is a toolkit name.
92  */
93  std::vector<std::string> available_toolkit_functions();
94 };
95 
96 }
97 
98 #endif
bool unregister_toolkit_function(std::string name)
const toolkit_function_specification * get_toolkit_function_info(std::string toolkit_fn_name)
bool register_toolkit_function(toolkit_function_specification spec, std::string prefix="")
std::function< variant_type(const std::vector< variant_type > &)> get_native_function(std::string name)
std::vector< std::string > available_toolkit_functions()