jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
config.cpp
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 #include "config.hpp"
18 
19 #include <iostream>
20 #include <string>
21 
22 #include "exception.hpp"
23 
24 namespace jubatus {
25 namespace core {
26 namespace common {
27 namespace jsonconfig {
28 
30  : parent_(it.parent_),
31  it_(it.it_) {
32 }
33 
35  const config& parent,
36  const jubatus::util::text::json::json::const_iterator& it)
37  : parent_(parent),
38  it_(it) {
39 }
40 
41 const std::string& config::iterator::key() const {
42  return it_->first;
43 }
44 
46  return config(it_->second, parent_.path() + "." + key());
47 }
48 
49 config config::operator[](size_t index) const {
50  try {
51  if (index < json_.size()) {
52  std::ostringstream os;
53  os << path_ << "[" << index << "]";
54  return config(json_[index], os.str());
55  } else {
56  throw JUBATUS_EXCEPTION(out_of_range(path_, json_.size(), index));
57  }
58  } catch (const std::bad_cast& e) {
59  throw JUBATUS_EXCEPTION(
60  type_error(path_, jubatus::util::text::json::json::Array, type()));
61  }
62 }
63 
64 config config::operator[](const std::string& key) const {
65  try {
66  std::ostringstream os;
67  os << path_ << "." << key;
68  return config(json_[key], os.str());
69  } catch (const std::out_of_range& e) {
70  throw JUBATUS_EXCEPTION(not_found(path_, key));
71  } catch (const std::bad_cast& e) {
72  throw JUBATUS_EXCEPTION(
73  type_error(path_, jubatus::util::text::json::json::Object, type()));
74  }
75 }
76 
77 bool config::contain(const std::string& key) const {
78  if (type() != jubatus::util::text::json::json::Object) {
79  throw JUBATUS_EXCEPTION(
80  type_error(path_, jubatus::util::text::json::json::Object, type()));
81  }
82  return json_.count(key) > 0;
83 }
84 
85 size_t config::size() const {
86  try {
87  return json_.size();
88  } catch (const std::bad_cast& e) {
89  throw JUBATUS_EXCEPTION(
90  type_error(path_, jubatus::util::text::json::json::Array, type()));
91  }
92 }
93 
95  return iterator(*this, json_.begin());
96 }
97 
99  return iterator(*this, json_.end());
100 }
101 
102 } // namespace jsonconfig
103 } // namespace common
104 } // namespace core
105 } // namespace jubatus
jubatus::util::text::json::json json_
Definition: config.hpp:134
bool contain(const std::string &key) const
Definition: config.cpp:77
config operator[](size_t index) const
Definition: config.cpp:49
jubatus::util::text::json::json::json_type_t type() const
Definition: config.hpp:84
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79