jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
config.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_CONFIG_HPP_
18 #define JUBATUS_CORE_COMMON_JSONCONFIG_CONFIG_HPP_
19 
20 #include <stdint.h>
21 #include <string>
22 #include <typeinfo>
23 #include <utility>
24 
25 #include "jubatus/util/text/json.h"
26 #include "jubatus/util/lang/cast.h"
27 
28 #include "exception.hpp"
29 
30 namespace jubatus {
31 namespace core {
32 namespace common {
33 namespace jsonconfig {
34 
35 class config;
36 
37 template<class T>
38 T config_cast(const config& c);
39 
40 class config {
41  public:
42  class iterator;
43 
45  : json_() {
46  }
47 
48  explicit config(const jubatus::util::text::json::json& j)
49  : json_(j) {
50  }
51 
52  config(const jubatus::util::text::json::json& j, const std::string& path)
53  : json_(j),
54  path_(path) {
55  }
56 
57  template<typename T>
58  T As() const {
59  return config_cast<T>(*this);
60  }
61 
62  config operator[](size_t index) const;
63 
64  config operator[](const std::string& key) const;
65 
66  bool contain(const std::string& key) const;
67 
68  iterator begin() const;
69  iterator end() const;
70 
71  size_t size() const;
72  const jubatus::util::text::json::json& get() const {
73  return json_;
74  }
75  const std::string& path() const {
76  return path_;
77  }
78 
79  template<class T>
80  bool is() const {
81  return jubatus::util::text::json::is<T>(json_);
82  }
83 
84  jubatus::util::text::json::json::json_type_t type() const {
85  return json_.type();
86  }
87 
88  class iterator { // const_iterator
89  public:
90  typedef jubatus::util::text::json::json::const_iterator iterator_base;
91  iterator(const iterator&);
92  iterator(
93  const config& parent,
94  const jubatus::util::text::json::json::const_iterator& it);
95 
96  const std::string& key() const;
97  config value() const;
98 
99  // InputIterator
100  bool operator==(const iterator& it) const {
101  return it_ == it.it_;
102  }
103 
104  bool operator!=(const iterator& it) const {
105  return !(*this == it);
106  }
107 
108  std::pair<const std::string, jubatus::util::text::json::json>
109  operator*() const {
110  return *it_;
111  }
112 
113  const std::pair<const std::string, jubatus::util::text::json::json>*
114  operator->() const {
115  return it_.operator->();
116  }
117  // FowrardIterator
118  const iterator& operator++() {
119  ++it_;
120  return *this;
121  }
122  const iterator operator++(int /* unused */) {
123  iterator temp(*this);
124  ++it_;
125  return temp;
126  }
127 
128  private:
129  const config& parent_;
130  jubatus::util::text::json::json::const_iterator it_;
131  };
132 
133  private:
134  jubatus::util::text::json::json json_;
135  std::string path_;
136 };
137 
138 } // namespace jsonconfig
139 } // namespace common
140 } // namespace core
141 } // namespace jubatus
142 
143 #endif // JUBATUS_CORE_COMMON_JSONCONFIG_CONFIG_HPP_
jubatus::util::text::json::json json_
Definition: config.hpp:134
bool operator==(const iterator &it) const
Definition: config.hpp:100
bool contain(const std::string &key) const
Definition: config.cpp:77
bool operator!=(const iterator &it) const
Definition: config.hpp:104
const std::pair< const std::string, jubatus::util::text::json::json > * operator->() const
Definition: config.hpp:114
config operator[](size_t index) const
Definition: config.cpp:49
std::pair< const std::string, jubatus::util::text::json::json > operator*() const
Definition: config.hpp:109
jubatus::util::text::json::json::json_type_t type() const
Definition: config.hpp:84
jubatus::util::text::json::json::const_iterator it_
Definition: config.hpp:130
jubatus::util::text::json::json::const_iterator iterator_base
Definition: config.hpp:90
config(const jubatus::util::text::json::json &j)
Definition: config.hpp:48
T config_cast(const config &c)
Definition: cast.hpp:296
config(const jubatus::util::text::json::json &j, const std::string &path)
Definition: config.hpp:52
const std::string & path() const
Definition: config.hpp:75