HoverRace  2.0
InspectMapNode.h
Go to the documentation of this file.
1 
2 // InspectMapNode.h
3 //
4 // Copyright (c) 2010, 2015 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #include <boost/lexical_cast.hpp>
25 
26 #include "InspectNode.h"
27 
28 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
29 # ifdef MR_ENGINE
30 # define MR_DllDeclare __declspec( dllexport )
31 # else
32 # define MR_DllDeclare __declspec( dllimport )
33 # endif
34 #else
35 # define MR_DllDeclare
36 #endif
37 
38 namespace HoverRace {
39 namespace Util {
40 
41 class Inspectable;
42 
44 {
45  using SUPER = InspectNode;
46 
47 public:
48  InspectScalarNode(const std::string &value) :
49  SUPER(), value(value) { }
50  virtual ~InspectScalarNode() { }
51 
52 public:
53  void RenderToYaml(yaml::Emitter &emitter) override;
54 
55 private:
56  std::string value;
57 };
58 
60 {
61  using SUPER = InspectNode;
62 
63 public:
64  InspectSeqNode(size_t sz) : SUPER()
65  {
66  fields.reserve(sz);
67  }
68  virtual ~InspectSeqNode() { }
69 
70 public:
71  void Add(std::shared_ptr<InspectNode> s)
72  {
73  fields.emplace_back(std::move(s));
74  }
75 
76 public:
77  void RenderToYaml(yaml::Emitter &emitter) override;
78 
79 private:
80  std::vector<std::shared_ptr<InspectNode>> fields;
81 };
82 
89 {
90  using SUPER = InspectNode;
91 
92 public:
94  virtual ~InspectMapNode() { }
95 
96 protected:
97  void AddStringField(const std::string &name, const std::string &value);
98 
99 public:
100  void RenderToStream(std::ostream &os);
101  void RenderToString(std::string &s);
102 
103 public:
104  void RenderToYaml(yaml::Emitter &emitter) override;
105 
106 public:
107  template<typename T>
108  InspectMapNode &AddField(const std::string &name, const T &value)
109  {
110  AddStringField(name, boost::lexical_cast<std::string>(value));
111  return *this;
112  }
113 
114  InspectMapNode &AddField(const std::string &name, const char *value)
115  {
116  AddStringField(name, value);
117  return *this;
118  }
119 
120  InspectMapNode &AddField(const std::string &name, const std::string &value)
121  {
122  AddStringField(name, value);
123  return *this;
124  }
125 
126  InspectMapNode &AddField(const std::string &name, bool value)
127  {
128  AddStringField(name, value ? "true" : "false");
129  return *this;
130  }
131 
132  template<typename T>
133  InspectMapNode &AddArray(const std::string &name, T *elems,
134  size_t startIndex, size_t size)
135  {
136  auto node = std::make_shared<InspectSeqNode>(size);
137  for (size_t i = startIndex; i < size; ++i)
138  node->Add(std::make_shared<InspectScalarNode>(
139  boost::lexical_cast<std::string>(elems[i])));
140  fields.emplace_back(name, node);
141  return *this;
142  }
143 
144  InspectMapNode &AddSubobject(const std::string &name, const Inspectable *obj);
145 
146 private:
147  std::vector<std::pair<std::string, std::shared_ptr<InspectNode>>> fields;
148 };
149 
150 } // namespace Util
151 } // namespace HoverRace
152 
153 #undef MR_DllDeclare
InspectMapNode & AddField(const std::string &name, bool value)
Definition: InspectMapNode.h:126
virtual ~InspectScalarNode()
Definition: InspectMapNode.h:50
InspectNode()
Definition: InspectNode.h:50
std::string value
Definition: InspectMapNode.h:56
An inspection node which maps field names to values (either strings or inspectable subobjects)...
Definition: InspectMapNode.h:88
Definition: InspectMapNode.h:43
InspectMapNode & AddField(const std::string &name, const T &value)
Definition: InspectMapNode.h:108
Definition: Inspectable.h:41
InspectSeqNode(size_t sz)
Definition: InspectMapNode.h:64
virtual ~InspectSeqNode()
Definition: InspectMapNode.h:68
std::vector< std::pair< std::string, std::shared_ptr< InspectNode > > > fields
Definition: InspectMapNode.h:147
InspectMapNode & AddArray(const std::string &name, T *elems, size_t startIndex, size_t size)
Definition: InspectMapNode.h:133
InspectScalarNode(const std::string &value)
Definition: InspectMapNode.h:48
Definition: InspectNode.h:47
#define MR_DllDeclare
Definition: InspectMapNode.h:35
Definition: InspectMapNode.h:59
Definition: Announcement.h:24
virtual ~InspectMapNode()
Definition: InspectMapNode.h:94
InspectMapNode()
Definition: InspectMapNode.h:93
Wrapper for the LibYAML emitter.
Definition: Emitter.h:48
std::vector< std::shared_ptr< InspectNode > > fields
Definition: InspectMapNode.h:80
InspectMapNode & AddField(const std::string &name, const char *value)
Definition: InspectMapNode.h:114
void Add(std::shared_ptr< InspectNode > s)
Definition: InspectMapNode.h:71
InspectMapNode & AddField(const std::string &name, const std::string &value)
Definition: InspectMapNode.h:120
void RenderToYaml(yaml::Emitter &emitter) override
Definition: InspectMapNode.cpp:102