Lightmetrica  0.0.1.50dbee3 (yosakoi)
 All Classes Functions Variables Typedefs Enumerations Enumerator
confignode.h
1 /*
2  Lightmetrica : A research-oriented renderer
3 
4  Copyright (c) 2014 Hisanari Otsu
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 #ifndef LIB_LIGHTMETRICA_CONFIG_NODE_H
22 #define LIB_LIGHTMETRICA_CONFIG_NODE_H
23 
24 #include "object.h"
25 #include "logger.h"
26 #include "math.types.h"
27 #include <vector>
28 
29 LM_NAMESPACE_BEGIN
30 
31 class Config;
32 
37 class LM_PUBLIC_API ConfigNode : public Object
38 {
39 public:
40 
41  ConfigNode();
42  ~ConfigNode();
43 
44 public:
45 
46  ConfigNode(const ConfigNode& config);
47  void operator=(const ConfigNode& config);
48  ConfigNode(ConfigNode&& config);
49  void operator=(ConfigNode&& config);
50 
51 public:
52 
59  ConfigNode(void* node, const Config* config);
60 
61 public:
62 
67  const Config* GetConfig() const;
68 
74  bool Empty() const;
75 
82  ConfigNode Child(const std::string& name) const;
83 
88  ConfigNode FirstChild() const;
89 
94  ConfigNode NextChild() const;
95 
101  ConfigNode NextChild(const std::string& name) const;
102 
107  std::string Name() const;
108 
113  std::string Value() const;
114 
120  template <typename T>
121  T Value() const;
122 
128  std::string AttributeValue(const std::string& name) const;
129 
139  template <typename T>
140  bool ChildValueOrDefault(const std::string& name, const T& defaultValue, T& value) const
141  {
142  const auto child = Child(name);
143  if (child.Empty())
144  {
145  LM_LOG_WARN("Missing '" + name + "' element. Using default value.");
146  value = defaultValue;
147  return false;
148  }
149 
150  value = child.Value<T>();
151  return true;
152  }
153 
163  template <typename T>
164  bool ChildValue(const std::string& name, T& value) const
165  {
166  const auto child = Child(name);
167  if (child.Empty())
168  {
169  LM_LOG_ERROR("Missing '" + name + "' element");
170  return false;
171  }
172 
173  value = child.Value<T>();
174  return true;
175  }
176 
177 private:
178 
179  class Impl;
180  Impl* p;
181 
182 };
183 
184 #ifndef LM_EXPORTS
185 extern template LM_PUBLIC_API std::string ConfigNode::Value<std::string>() const;
186 extern template LM_PUBLIC_API int ConfigNode::Value<int>() const;
187 extern template LM_PUBLIC_API long long ConfigNode::Value<long long>() const;
188 extern template LM_PUBLIC_API bool ConfigNode::Value<bool>() const;
189 extern template LM_PUBLIC_API Math::Float ConfigNode::Value<Math::Float>() const;
190 extern template LM_PUBLIC_API Math::Vec3 ConfigNode::Value<Math::Vec3>() const;
191 extern template LM_PUBLIC_API Math::Mat4 ConfigNode::Value<Math::Mat4>() const;
192 extern template LM_PUBLIC_API std::vector<Math::Float> ConfigNode::Value<std::vector<Math::Float>>() const;
193 extern template LM_PUBLIC_API std::vector<unsigned int> ConfigNode::Value<std::vector<unsigned int>>() const;
194 #endif
195 
196 LM_NAMESPACE_END
197 
198 #endif // LIB_LIGHTMETRICA_CONFIG_NODE_H
Definition: config.h:36
bool ChildValue(const std::string &name, T &value) const
Definition: confignode.h:164
Definition: object.h:40
Definition: confignode.h:37
bool ChildValueOrDefault(const std::string &name, const T &defaultValue, T &value) const
Definition: confignode.h:140