Turi Create  4.0
simple_model.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_SIMPLE_MODEL_HPP
7 #define TURI_UNITY_SIMPLE_MODEL_HPP
8 
9 #include <model_server/lib/variant.hpp>
10 #include <model_server/lib/extensions/model_base.hpp>
11 
12 namespace turi {
13 /**
14  * \ingroup unity
15  * The simple_model is the simplest implementation of the \ref model_base
16  * object, containing just a map from string to variant and permitting
17  * query operations on the map.
18  */
19 class simple_model: public model_base {
20 
21  public:
22 
23  static constexpr size_t SIMPLE_MODEL_VERSION = 0;
24 
25  /// Default constructor
27 
28  /**
29  * Constructs a simple_model from a variant map.
30  * A copy of the map is taken and stored.
31  */
32  explicit simple_model(const variant_map_type& params) : params(params) {}
33 
34 
35  /// Lists all the keys stored in the variant map
36  std::vector<std::string> list_fields();
37 
38  /**
39  * Gets the value of a key in the variant map. Throws an error if the key
40  * is not found. opts is ignored.
41  */
42  variant_type get_value(std::string key, variant_map_type& opts);
43 
44  /**
45  * Returns the current model version
46  */
47  size_t get_version() const override;
48 
49  /**
50  * Serializes the model. Must save the model to the file format version
51  * matching that of get_version()
52  */
53  void save_impl(oarchive& oarc) const override;
54 
55  /**
56  * Loads a model previously saved at a particular version number.
57  * Should raise an exception on failure.
58  */
59  void load_version(iarchive& iarc, size_t version) override;
60 
61  /// Destructor
62  ~simple_model();
63 
64  /// Internal map
65  variant_map_type params;
66 
67  BEGIN_CLASS_MEMBER_REGISTRATION("simple_model")
69  REGISTER_NAMED_CLASS_MEMBER_FUNCTION("get", simple_model::get_value, "key")
71 };
72 
73 }
74 
75 #endif // TURI_UNITY_SIMPLE_MODEL
#define BEGIN_CLASS_MEMBER_REGISTRATION(python_facing_classname)
#define REGISTER_CLASS_MEMBER_FUNCTION(function,...)
The serialization input archive object which, provided with a reference to an istream, will read from the istream, providing deserialization capabilities.
Definition: iarchive.hpp:60
std::vector< std::string > list_fields()
Lists all the keys stored in the variant map.
variant_map_type params
Internal map.
boost::make_recursive_variant< flexible_type, std::shared_ptr< unity_sgraph_base >, dataframe_t, std::shared_ptr< model_base >, std::shared_ptr< unity_sframe_base >, std::shared_ptr< unity_sarray_base >, std::map< std::string, boost::recursive_variant_ >, std::vector< boost::recursive_variant_ >, boost::recursive_wrapper< function_closure_info > >::type variant_type
Definition: variant.hpp:24
#define END_CLASS_MEMBER_REGISTRATION
simple_model()
Default constructor.
~simple_model()
Destructor.
size_t get_version() const override
simple_model(const variant_map_type &params)
#define REGISTER_NAMED_CLASS_MEMBER_FUNCTION(name, function,...)
void save_impl(oarchive &oarc) const override
void load_version(iarchive &iarc, size_t version) override
variant_type get_value(std::string key, variant_map_type &opts)
The serialization output archive object which, provided with a reference to an ostream, will write to the ostream, providing serialization capabilities.
Definition: oarchive.hpp:80