jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
cast.hpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2012 Preferred Networks and Nippon Telegraph and Telephone Corporation.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License version 2.1 as published by the Free Software Foundation.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 #ifndef JUBATUS_CORE_COMMON_JSONCONFIG_CAST_HPP_
18 #define JUBATUS_CORE_COMMON_JSONCONFIG_CAST_HPP_
19 
20 #include <stdint.h>
21 #include <map>
22 #include <set>
23 #include <string>
24 #include <vector>
25 #include "jubatus/util/data/unordered_map.h"
26 #include "jubatus/util/lang/shared_ptr.h"
27 #include "jubatus/util/lang/demangle.h"
28 
29 #include "config.hpp"
30 
31 namespace jubatus {
32 namespace core {
33 namespace common {
34 namespace jsonconfig {
35 
36 typedef std::vector<jubatus::util::lang::shared_ptr<config_error> >
38 
40  public:
41  void append(const std::string& name) {
42  members_.push_back(name);
43  }
44 
45  const std::vector<std::string>& get_members() const {
46  return members_;
47  }
48 
49  private:
50  std::vector<std::string> members_;
51 };
52 
53 template <typename T>
54 inline void serialize(
55  member_collector& mem,
56  jubatus::util::data::serialization::named_value<T>& v) {
57  mem.append(v.name);
58 }
59 
61  public:
62  explicit json_config_iarchive_cast(const config& js)
63  : js_(js),
64  errors_(NULL) {
65  }
67  : js_(js),
68  errors_(errors) {
69  }
70 
71  const config& get_config() const {
72  return js_;
73  }
74  const jubatus::util::text::json::json& get() const {
75  return js_.get();
76  }
77 
78  bool trace_error() const {
79  return errors_;
80  }
81 
82  template<class T>
83  void push_error(const T& e) {
84  if (errors_) {
85  errors_->push_back(
86  jubatus::util::lang::shared_ptr<config_error>(new T(e)));
87  }
88  }
89 
91  return errors_;
92  }
93 
94  private:
95  const config& js_;
97 };
98 
99 template <typename T>
100 void json_from_config(const config& conf, T& v);
101 
102 template <typename T>
103 void json_from_config(const config& conf, T& v, json_config_iarchive_cast& js);
104 
105 template <typename T>
106 inline void serialize(json_config_iarchive_cast& js, T& v) {
107  if (js.get().type() == jubatus::util::text::json::json::Object) {
108  member_collector collector;
110  std::set<std::string> members(collector.get_members().begin(),
111  collector.get_members().end());
112  for (jubatus::util::text::json::json::const_iterator it = js.get().begin();
113  it != js.get().end(); ++it) {
114  const std::string& key = it->first;
115  if (members.count(key) == 0) {
116  redundant_key e(js.get_config().path(), key);
117  if (js.trace_error()) {
118  js.push_error(e);
119  } else {
120  throw JUBATUS_EXCEPTION(e);
121  }
122  }
123  }
124  }
125 
127 }
128 
129 namespace detail {
130 inline bool check_json_type(
132  jubatus::util::text::json::json::json_type_t t) {
133  if (js.get().type() != t) {
134  type_error e(js.get_config().path(), t, js.get().type());
135  if (js.trace_error()) {
136  js.push_error(e);
137  } else {
138  throw JUBATUS_EXCEPTION(e);
139  }
140  return false;
141  }
142  return true;
143 }
144 
146  if (js.get().type() != jubatus::util::text::json::json::Float
147  && js.get().type() != jubatus::util::text::json::json::Integer) {
148  type_error e(
149  js.get_config().path(),
150  jubatus::util::text::json::json::Float,
151  js.get().type());
152  if (js.trace_error()) {
153  js.push_error(e);
154  } else {
155  throw JUBATUS_EXCEPTION(e);
156  }
157  return false;
158  }
159  return true;
160 }
161 } // namespace detail
162 
163 #define GENERATE_CONFIG_SERIALIZE_DEF(typ, json_typ) \
164  template <> \
165  inline void serialize(json_config_iarchive_cast& js, typ& v) { \
166  if (detail::check_json_type( \
167  js, jubatus::util::text::json::json::json_typ)) { \
168  v = jubatus::util::text::json::json_cast<typ>(js.get()); \
169  } \
170  }
171 
173 GENERATE_CONFIG_SERIALIZE_DEF(int32_t, Integer)
174 GENERATE_CONFIG_SERIALIZE_DEF(int64_t, Integer)
175 GENERATE_CONFIG_SERIALIZE_DEF(std::string, String)
176 
177 #define GENERATE_CONFIG_SERIALIZE_FLOAT_DEF(typ) \
178  template <> \
179  inline void serialize(json_config_iarchive_cast& js, typ& v) { \
180  if (detail::check_json_float(js)) { \
181  v = jubatus::util::text::json::json_cast<typ>(js.get()); \
182  } \
183  }
184 
186 GENERATE_CONFIG_SERIALIZE_FLOAT_DEF(double) // NOLINT
187 
188 template <typename T>
189 inline void serialize(json_config_iarchive_cast& js, std::vector<T>& vs) {
190  // check errors
191  if (!detail::check_json_type(js, jubatus::util::text::json::json::Array)) {
192  return;
193  }
194 
195  size_t size = js.get_config().size();
196  std::vector<T> v(size);
197  for (size_t i = 0; i < size; ++i) {
198  json_from_config(js.get_config()[i], v[i], js.errors());
199  }
200  vs.swap(v);
201 }
202 
203 template <typename K, typename V>
204 inline void serialize(json_config_iarchive_cast& js, std::map<K, V>& m) {
205  if (!detail::check_json_type(js, jubatus::util::text::json::json::Object)) {
206  return;
207  }
208 
209  std::map<K, V> tmp;
210  typedef config::iterator iter_t;
211  for (iter_t it = js.get_config().begin(), end = js.get_config().end();
212  it != end; ++it) {
213  V value = V();
214  json_from_config(it.value(), value, js.errors());
215  tmp[it.key()] = value;
216  }
217  tmp.swap(m);
218 }
219 
220 template <typename K, typename V>
221 inline void serialize(
223  jubatus::util::data::unordered_map<K, V>& m) {
224  if (!detail::check_json_type(js, jubatus::util::text::json::json::Object)) {
225  return;
226  }
227 
228  jubatus::util::data::unordered_map<K, V> tmp;
229  typedef config::iterator iter_t;
230  for (iter_t it = js.get_config().begin(), end = js.get_config().end();
231  it != end; ++it) {
232  V value = V();
233  json_from_config(it.value(), value, js.errors());
234  tmp[it.key()] = value;
235  }
236  tmp.swap(m);
237 }
238 
239 template <typename T>
240 inline void serialize(
242  jubatus::util::data::serialization::named_value<
243  jubatus::util::data::optional<T> >& v) {
244  using jubatus::util::text::json::json;
245  if (js.get_config().contain(v.name)
246  && (js.get_config()[v.name].get().type() != json::Null)) {
247  T value = T();
248  json_from_config(js.get_config()[v.name], value, js.errors());
249  v.v = value;
250  } else {
251  // optional can be null
252  v.v = jubatus::util::data::optional<T>();
253  }
254 }
255 
256 template <typename T>
258  jubatus::util::data::serialization::named_value<T>& v) {
259  if (js.get_config().contain(v.name)) {
260  json_from_config(js.get_config()[v.name], v.v, js.errors());
261  } else {
262  not_found e(js.get_config().path(), v.name);
263  if (js.trace_error()) {
264  js.push_error(e);
265  } else {
266  throw JUBATUS_EXCEPTION(e);
267  }
268  }
269 }
270 
271 template<>
272 inline void serialize(
274  jubatus::util::text::json::json& v) {
275  v = js.get();
276 }
277 
278 template<>
280  v = config(js.get(), js.get_config().path());
281 }
282 
283 template <typename T>
284 void json_from_config(const config& conf, T& v) {
285  json_config_iarchive_cast cast(conf);
286  serialize(cast, v);
287 }
288 
289 template <typename T>
290 void json_from_config(const config& conf, T& v, config_error_list* errors) {
291  json_config_iarchive_cast cast(conf, errors);
292  serialize(cast, v);
293 }
294 
295 template <class T>
296 T config_cast(const config& c) {
297  T value = T();
298  json_from_config(c, value);
299  return value;
300 }
301 
302 template <class T>
303 T config_cast(const config& c, config_error_list& errors) {
304  T value = T();
305  json_config_iarchive_cast cast(c, &errors);
306  serialize(cast, value);
307  return value;
308 }
309 
310 template <class T>
312  config_error_list errors;
313  T value = T();
314  json_config_iarchive_cast cast(c, &errors);
315  serialize(cast, value);
316  if (!errors.empty()) {
317  throw JUBATUS_EXCEPTION(cast_check_error(errors));
318  }
319  return value;
320 }
321 
322 } // namespace jsonconfig
323 } // namespace common
324 } // namespace core
325 } // namespace jubatus
326 
327 #endif // JUBATUS_CORE_COMMON_JSONCONFIG_CAST_HPP_
T config_cast_check(const config &c)
Definition: cast.hpp:311
void serialize(member_collector &mem, jubatus::util::data::serialization::named_value< T > &v)
Definition: cast.hpp:54
bool check_json_float(json_config_iarchive_cast &js)
Definition: cast.hpp:145
bool contain(const std::string &key) const
Definition: config.cpp:77
json_config_iarchive_cast(const config &js, config_error_list *errors)
Definition: cast.hpp:66
void serialize(json_config_iarchive_cast &js, config &v)
Definition: cast.hpp:279
void append(const std::string &name)
Definition: cast.hpp:41
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
const std::vector< std::string > & get_members() const
Definition: cast.hpp:45
std::vector< T > & vs
Definition: cast.hpp:189
bool check_json_type(json_config_iarchive_cast &js, jubatus::util::text::json::json::json_type_t t)
Definition: cast.hpp:130
void json_from_config(const config &conf, T &v)
Definition: cast.hpp:284
std::vector< T > v(size)
#define GENERATE_CONFIG_SERIALIZE_DEF(typ, json_typ)
Definition: cast.hpp:163
const jubatus::util::text::json::json & get() const
Definition: config.hpp:72
T config_cast(const config &c)
Definition: cast.hpp:296
GENERATE_CONFIG_SERIALIZE_FLOAT_DEF(float) GENERATE_CONFIG_SERIALIZE_FLOAT_DEF(double) template< typename T > inline void serialize(json_config_iarchive_cast &js
const std::string & path() const
Definition: config.hpp:75
const jubatus::util::text::json::json & get() const
Definition: cast.hpp:74
std::vector< jubatus::util::lang::shared_ptr< config_error > > config_error_list
Definition: cast.hpp:37