VL
DMBCore.h
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <string>
5#include "vl.h"
6#include "JSONDefs.h"
8#include "TypeResolver.h"
9
10namespace dmb
11{
12 class Model;
13
14 // Util class for working with types as vl objects
16 {
17 friend class Model;
18
19 public:
20 vl::Object& CreateType(const std::string&);
21 vl::Var& RegisterType(const std::string&, vl::Var& type);
22 vl::Object& GetType(const std::string&);
23 bool ForeachType(const std::function<bool(const std::string&, vl::Object&)>& pred);
24 bool ForeachType(const std::function<bool(const std::string&, const vl::Object&)>& pred) const;
25 bool RemoveType(const std::string& typeName);
26 bool RenameType(const std::string& typeName, const std::string& newName);
27 bool HasType(const std::string& typeName);
28 // TODO: think how to keep int under the hood
29 vl::Object& GetData();
30 const vl::Object& GetData() const;
31 void Clear(bool recursive = false);
32
33 protected:
34 void Init(const vl::Object& data);
35
36 protected:
37 vl::Object mData;
38 };
39
40 class Model;
41
42 // Util class for working with content as a set of vl variables
43 // Content can be stored separately of its model
44 class Content
45 {
46 friend class Model;
47
48 public:
49 // Create an object of type proto and put it into the content
50 vl::Object& Add(const std::string&, vl::Object& proto);
51 // Put any value as it is. Objects and lists are copied
52 vl::Var& Add(const std::string&, vl::Var& value);
53 int ItemCount() const;
54 vl::Var& Get(const std::string& entityName);
55 const vl::Var& Get(const std::string& entityName) const;
56 bool Has(const std::string& entityName) const;
57 bool Remove(const std::string& entityName);
58 bool Rename(const std::string& entityName, const std::string& newName);
59 bool ForeachItem(const std::function<bool(const std::string&, vl::Object&)>& pred);
60 bool ForeachItem(const std::function<bool(const std::string&, const vl::Object&)>& pred) const;
61 bool Store(const std::string& filePath, const vl::CnvParams& params = vl::CnvParams());
62 std::string JSONStr(const vl::CnvParams& params = vl::CnvParams());
63 // TODO: think how to keep it under the hood
64 vl::Object& GetData();
65 const vl::Object& GetData() const;
66 void Clear(bool recursive = false);
67
68 protected:
69 void Init(const vl::Object& data, const TypeResolver& typeResolver);
70
71 protected:
72 vl::Object mData;
73 TypeResolver mTypeResolver;
74 };
75
76 // Util class for working with data model as a vl object
77 // with types and content fields
78 // You can store or load a model from JSON
79 class Model
80 {
81 public:
82 Model();
83 ~Model();
87 vl::Object& GetType(const std::string& typeName);
88 bool Load(const std::string& fileName);
89 bool IsLoaded() const;
90 bool Store(const std::string& fileName, const vl::CnvParams& params = vl::CnvParams());
91 std::string JSONStr(const vl::CnvParams& params = vl::CnvParams());
92 std::string GetTypeId(const vl::Object& obj) const;
93 const vl::Object& GetData();
94 void Clear(bool recursive = false);
95 std::string DataStr(bool pretty = true) const;
97 return mVarNodeRegistry;
98 }
99 protected:
100 void Init();
101
102 protected:
103 vl::Object mData;
108 TypeResolver mTypeResolver;
109 bool mIsLoaded = false;
110 };
111}
Definition: DMBCore.h:45
vl::Object mData
Definition: DMBCore.h:72
TypeResolver mTypeResolver
Definition: DMBCore.h:73
vl::Object & Add(const std::string &, vl::Object &proto)
Definition: DMBCore.cpp:83
int ItemCount() const
Definition: DMBCore.cpp:104
bool Remove(const std::string &entityName)
Definition: DMBCore.cpp:119
bool ForeachItem(const std::function< bool(const std::string &, vl::Object &)> &pred)
Definition: DMBCore.cpp:134
void Init(const vl::Object &data, const TypeResolver &typeResolver)
Definition: DMBCore.cpp:177
bool Store(const std::string &filePath, const vl::CnvParams &params=vl::CnvParams())
Definition: DMBCore.cpp:150
std::string JSONStr(const vl::CnvParams &params=vl::CnvParams())
Definition: DMBCore.cpp:156
bool Rename(const std::string &entityName, const std::string &newName)
Definition: DMBCore.cpp:129
bool Has(const std::string &entityName) const
Definition: DMBCore.cpp:114
vl::Var & Get(const std::string &entityName)
Definition: DMBCore.cpp:124
void Clear(bool recursive=false)
Definition: DMBCore.cpp:172
vl::Object & GetData()
Definition: DMBCore.cpp:162
Definition: DMBCore.h:80
bool Store(const std::string &fileName, const vl::CnvParams &params=vl::CnvParams())
Definition: DMBCore.cpp:230
const vl::VarNodeRegistry & GetVarNodeRegistry() const
Definition: DMBCore.h:96
bool IsLoaded() const
Definition: DMBCore.cpp:225
vl::VarNodeRegistry mVarNodeRegistry
Definition: DMBCore.h:107
bool Load(const std::string &fileName)
Definition: DMBCore.cpp:212
Registry mRegistry
Definition: DMBCore.h:104
vl::Object & GetType(const std::string &typeName)
Definition: DMBCore.cpp:207
void Init()
Definition: DMBCore.cpp:287
const vl::Object & GetData()
Definition: DMBCore.cpp:270
Content & GetContent()
Definition: DMBCore.h:86
Registry & GetPrivateScope()
Definition: DMBCore.h:85
TypeResolver mTypeResolver
Definition: DMBCore.h:108
Registry mPrivate
Definition: DMBCore.h:105
std::string GetTypeId(const vl::Object &obj) const
Definition: DMBCore.cpp:242
Registry & GetRegistry()
Definition: DMBCore.h:84
void Clear(bool recursive=false)
Definition: DMBCore.cpp:275
Content mContent
Definition: DMBCore.h:106
~Model()
Definition: DMBCore.cpp:183
std::string JSONStr(const vl::CnvParams &params=vl::CnvParams())
Definition: DMBCore.cpp:236
std::string DataStr(bool pretty=true) const
Definition: DMBCore.cpp:280
Model()
Definition: DMBCore.cpp:188
vl::Object mData
Definition: DMBCore.h:103
bool mIsLoaded
Definition: DMBCore.h:109
Definition: DMBCore.h:16
vl::Object & GetData()
Definition: DMBCore.cpp:63
bool HasType(const std::string &typeName)
Definition: DMBCore.cpp:58
bool RenameType(const std::string &typeName, const std::string &newName)
Definition: DMBCore.cpp:53
vl::Var & RegisterType(const std::string &, vl::Var &type)
Definition: DMBCore.cpp:22
vl::Object mData
Definition: DMBCore.h:37
void Clear(bool recursive=false)
Definition: DMBCore.cpp:73
vl::Object & GetType(const std::string &)
Definition: DMBCore.cpp:27
bool ForeachType(const std::function< bool(const std::string &, vl::Object &)> &pred)
Definition: DMBCore.cpp:32
void Init(const vl::Object &data)
Definition: DMBCore.cpp:78
bool RemoveType(const std::string &typeName)
Definition: DMBCore.cpp:48
vl::Object & CreateType(const std::string &)
Definition: DMBCore.cpp:17
Definition: VLBackwardTraversable.h:128
Definition: DMBCore.h:11
Definition: JSONDefs.h:8