Turi Create  4.0
toolkit_util.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_UTIL_HPP
7 
8 #define TURI_UNITY_TOOLKIT_UTIL_HPP
9 #include <vector>
10 #include <utility>
11 #include <model_server/lib/variant.hpp>
12 
13 /*
14  * This contains a collection of useful utility function for toolkit
15  * developement
16  */
17 namespace turi {
18 
19 template <typename T>
20 T safe_varmap_get(const variant_map_type& kv, std::string key) {
21  if (kv.count(key) == 0) {
22  log_and_throw("Required Key " + key + " not found");
23  } else {
24  return variant_get_value<T>(kv.at(key));
25  }
26  __builtin_unreachable();
27 }
28 
29 /**
30  * Extract all flexible_type values from the varmap into a std::map<std::String, flexible_type>.
31  * All other value types will be ignored.
32  */
33 inline std::map<std::string, flexible_type> varmap_to_flexmap(const variant_map_type& map) {
34  std::map<std::string, flexible_type> ret;
35  for (const auto& kv : map) {
36  if (kv.second.type() == typeid(flexible_type)) {
37  ret[kv.first] = boost::get<flexible_type>(kv.second);
38  }
39  }
40  return ret;
41 }
42 
43 /**
44  * Cast each flexible type to variant type.
45  */
46 inline std::map<std::string, variant_type> flexmap_to_varmap(const std::map<std::string, flexible_type>& map) {
47  std::map<std::string, variant_type> ret;
48  for (const auto& kv : map) {
49  ret[kv.first] = (variant_type) kv.second;
50  }
51  return ret;
52 }
53 
54 
55 } // namespace turi
56 #endif
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
std::map< std::string, variant_type > flexmap_to_varmap(const std::map< std::string, flexible_type > &map)
std::map< std::string, flexible_type > varmap_to_flexmap(const variant_map_type &map)