JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
RapidJsonDocument.h
Go to the documentation of this file.
1 //
2 // Created by Nico Schäfer on 3/20/17.
3 //
4 
5 #ifndef JODA_RAPIDJSONDOCUMENT_H
6 #define JODA_RAPIDJSONDOCUMENT_H
8 #include <rapidjson/allocators.h>
9 #include <rapidjson/fwd.h>
10 #include <atomic>
11 #include <memory>
12 #include "../../../../../misc/include/joda/misc/FileNameRepo.h"
13 #include "IOrigin.h"
14 #include "joda/misc/RJFwd.h"
15 
16 typedef unsigned long DOC_ID; // The type used to identify documents
17 
23  public:
30  RapidJsonDocument(unsigned long id, std::unique_ptr<RJDocument> &&json,
31  std::unique_ptr<const IOrigin> &&origin);
32 
38  RapidJsonDocument(std::unique_ptr<RJDocument> &&json,
39  std::unique_ptr<const IOrigin> &&origin);
40 
41  ~RapidJsonDocument() = default;
42 
47  RapidJsonDocument(RapidJsonDocument &&doc) noexcept = default;
48  RapidJsonDocument &operator=(RapidJsonDocument &&other) noexcept = default;
49 
50 
55  RapidJsonDocument(const RapidJsonDocument &doc) = delete;
57 
58 
67  const std::unique_ptr<RJDocument> &getJson() const;
68 
73  void setJson(std::unique_ptr<RJDocument> &&json);
74 
79  void setOrigin(std::unique_ptr<const IOrigin> &&orig);
80 
84  void removeDoc();
85 
90  static DOC_ID getNewID();
91 
96  unsigned long getId() const;
97 
102  bool isValid() const;
103 
108  bool isView() const;
109 
110  void setView(std::unique_ptr<ViewLayer> &&view);
111 
112  const std::unique_ptr<ViewLayer> &getView() const;
113 
114  RJValue const *Get(const RJPointer &ptr) const;
115 
116  const std::unique_ptr<const IOrigin>& getOrigin() const;
117 
118 
119 
120  template <typename Handler>
121  bool Accept(Handler &handler) const {
122  if (isView()) {
123  DCHECK(view != nullptr) << "View was not computed";
124  return view->Accept(handler);
125  } else {
126  DCHECK(json != nullptr) << "Json was not loaded";
127  return json->Accept(handler);
128  }
129  }
130 
131  protected:
132  unsigned long id{};
133  std::unique_ptr<const IOrigin> origin;
134  std::unique_ptr<RJDocument> json;
135  std::unique_ptr<ViewLayer> view;
136  bool isView_ = false;
137 
138  private:
139  static std::atomic_ulong currID;
140 };
141 
142 typedef std::unique_ptr<RapidJsonDocument> RJDocumentPointer;
143 typedef std::unique_ptr<RJMemoryPoolAlloc> RJMemoryPoolPointer;
144 
145 #endif // JODA_RAPIDJSONDOCUMENT_H
rapidjson::GenericPointer< RJValue, RJBaseAlloc > RJPointer
Definition: RJFwd.h:30
rapidjson::GenericValue< RJChar, RJMemoryPoolAlloc > RJValue
Definition: RJFwd.h:29
std::unique_ptr< RJMemoryPoolAlloc > RJMemoryPoolPointer
Definition: RapidJsonDocument.h:143
unsigned long DOC_ID
Definition: RapidJsonDocument.h:16
std::unique_ptr< RapidJsonDocument > RJDocumentPointer
Definition: RapidJsonDocument.h:142
Definition: RapidJsonDocument.h:22
const std::unique_ptr< RJDocument > & getJson() const
Definition: RapidJsonDocument.cpp:12
const std::unique_ptr< const IOrigin > & getOrigin() const
Definition: RapidJsonDocument.cpp:25
std::unique_ptr< RJDocument > json
Definition: RapidJsonDocument.h:134
void setOrigin(std::unique_ptr< const IOrigin > &&orig)
Definition: RapidJsonDocument.cpp:43
void setJson(std::unique_ptr< RJDocument > &&json)
Definition: RapidJsonDocument.cpp:39
RapidJsonDocument & operator=(RapidJsonDocument &&other) noexcept=default
RapidJsonDocument & operator=(const RapidJsonDocument &other)=delete
RJValue const * Get(const RJPointer &ptr) const
Definition: RapidJsonDocument.cpp:56
const std::unique_ptr< ViewLayer > & getView() const
Definition: RapidJsonDocument.cpp:65
bool isView_
Definition: RapidJsonDocument.h:136
bool Accept(Handler &handler) const
Definition: RapidJsonDocument.h:121
bool isView() const
Definition: RapidJsonDocument.cpp:49
std::unique_ptr< ViewLayer > view
Definition: RapidJsonDocument.h:135
RapidJsonDocument(RapidJsonDocument &&doc) noexcept=default
bool isValid() const
Definition: RapidJsonDocument.cpp:47
void removeDoc()
Definition: RapidJsonDocument.cpp:16
static DOC_ID getNewID()
Definition: RapidJsonDocument.cpp:22
RapidJsonDocument(const RapidJsonDocument &doc)=delete
void setView(std::unique_ptr< ViewLayer > &&view)
Definition: RapidJsonDocument.cpp:51
std::unique_ptr< const IOrigin > origin
Definition: RapidJsonDocument.h:133
unsigned long getId() const
Definition: RapidJsonDocument.cpp:20
~RapidJsonDocument()=default