Open Lighting Architecture  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JsonPatch.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * JsonPatch.h
17  * Implementation of RFC 6902.
18  * Copyright (C) 2014 Simon Newton
19  */
20 
29 #ifndef INCLUDE_OLA_WEB_JSONPATCH_H_
30 #define INCLUDE_OLA_WEB_JSONPATCH_H_
31 
32 #include <ola/base/Macro.h>
33 #include <ola/web/Json.h>
34 #include <ola/web/JsonPointer.h>
35 #include <memory>
36 #include <string>
37 #include <vector>
38 
39 namespace ola {
40 namespace web {
41 
47 class JsonPatchSet;
48 
52 class JsonPatchOp {
53  public:
54  virtual ~JsonPatchOp() {}
55 
62  virtual bool Apply(JsonValue **value) const = 0;
63 };
64 
68 class JsonPatchAddOp : public JsonPatchOp {
69  public:
75  JsonPatchAddOp(const JsonPointer &path, const JsonValue *value)
76  : m_pointer(path),
77  m_value(value) {
78  }
79 
80  bool Apply(JsonValue **value) const;
81 
82  private:
83  JsonPointer m_pointer;
84  std::auto_ptr<const JsonValue> m_value;
85 
86  DISALLOW_COPY_AND_ASSIGN(JsonPatchAddOp);
87 };
88 
93  public:
98  explicit JsonPatchRemoveOp(const JsonPointer &path)
99  : m_pointer(path) {
100  }
101 
102  bool Apply(JsonValue **value) const;
103 
104  private:
105  const JsonPointer m_pointer;
106 
107  DISALLOW_COPY_AND_ASSIGN(JsonPatchRemoveOp);
108 };
109 
114  public:
120  JsonPatchReplaceOp(const JsonPointer &path, const JsonValue *value)
121  : m_pointer(path),
122  m_value(value) {
123  }
124 
125  bool Apply(JsonValue **value) const;
126 
127  private:
128  const JsonPointer m_pointer;
129  std::auto_ptr<const JsonValue> m_value;
130 
131  DISALLOW_COPY_AND_ASSIGN(JsonPatchReplaceOp);
132 };
133 
137 class JsonPatchMoveOp : public JsonPatchOp {
138  public:
144  JsonPatchMoveOp(const JsonPointer &from, const JsonPointer &to)
145  : m_from(from),
146  m_to(to) {
147  }
148 
149  bool Apply(JsonValue **value) const;
150 
151  private:
152  JsonPointer m_from;
153  JsonPointer m_to;
154 
155  DISALLOW_COPY_AND_ASSIGN(JsonPatchMoveOp);
156 };
157 
161 class JsonPatchCopyOp : public JsonPatchOp {
162  public:
168  JsonPatchCopyOp(const JsonPointer &from, const JsonPointer &to)
169  : m_from(from),
170  m_to(to) {
171  }
172 
173  bool Apply(JsonValue **value) const;
174 
175  private:
176  JsonPointer m_from;
177  JsonPointer m_to;
178 
179  DISALLOW_COPY_AND_ASSIGN(JsonPatchCopyOp);
180 };
181 
185 class JsonPatchTestOp : public JsonPatchOp {
186  public:
187  JsonPatchTestOp(const JsonPointer &path, const JsonValue *value)
188  : m_pointer(path),
189  m_value(value) {
190  }
191 
192  bool Apply(JsonValue **value) const;
193 
194  private:
195  JsonPointer m_pointer;
196  std::auto_ptr<const JsonValue> m_value;
197 
198  DISALLOW_COPY_AND_ASSIGN(JsonPatchTestOp);
199 };
200 
201 
206  public:
207  JsonPatchSet() {}
208  ~JsonPatchSet();
209 
214  void AddOp(JsonPatchOp *op);
215 
221  bool Apply(JsonValue **value) const;
222 
223  bool Empty() const { return m_patch_ops.empty(); }
224 
225  private:
226  typedef std::vector<JsonPatchOp*> PatchOps;
227 
228  PatchOps m_patch_ops;
229 };
231 } // namespace web
232 } // namespace ola
233 #endif // INCLUDE_OLA_WEB_JSONPATCH_H_