Skip to content

Commit 2daf343

Browse files
committed
feat: cleaned up model
Signed-off-by: Yash Pandey (YP) <[email protected]>
1 parent c30c417 commit 2daf343

22 files changed

+79
-79
lines changed

casbin/enforcer.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ bool Enforcer::m_enforce(const std::string& matcher, Scope scope) {
5454

5555
if(ok) {
5656
for (auto [assertion_name, assertion] : m_model->m["g"].assertion_map) {
57-
std::shared_ptr<RoleManager> rm = assertion->rm;
57+
std::shared_ptr<RoleManager>& rm = assertion->rm;
5858

5959
int char_count = static_cast<int>(std::count(assertion->value.begin(), assertion->value.end(), '_'));
6060
size_t index = exp_string.find(assertion_name + "(");
@@ -197,7 +197,7 @@ Enforcer ::Enforcer(const std::string& model_path, std::shared_ptr<Adapter> adap
197197
* @param m the model.
198198
* @param adapter the adapter.
199199
*/
200-
Enforcer::Enforcer(std::shared_ptr<Model> m, std::shared_ptr<Adapter> adapter)
200+
Enforcer::Enforcer(const std::shared_ptr<Model>& m, std::shared_ptr<Adapter> adapter)
201201
: m_adapter(adapter), m_watcher(nullptr), m_model(m) {
202202
m_model->PrintModel();
203203

@@ -212,7 +212,7 @@ Enforcer::Enforcer(std::shared_ptr<Model> m, std::shared_ptr<Adapter> adapter)
212212
*
213213
* @param m the model.
214214
*/
215-
Enforcer ::Enforcer(std::shared_ptr<Model> m): Enforcer(m, NULL) {
215+
Enforcer::Enforcer(const std::shared_ptr<Model>& m) : Enforcer(m, NULL) {
216216
}
217217

218218
/**
@@ -244,15 +244,15 @@ void Enforcer::InitWithFile(const std::string& model_path, const std::string& po
244244

245245
// InitWithAdapter initializes an enforcer with a database adapter.
246246
void Enforcer::InitWithAdapter(const std::string& model_path, std::shared_ptr<Adapter> adapter) {
247-
std::shared_ptr<Model> m = std::shared_ptr<Model>(Model::NewModelFromFile(model_path));
247+
std::shared_ptr<Model> m = Model::NewModelFromFile(model_path);
248248

249249
this->InitWithModelAndAdapter(m, adapter);
250250

251251
m_model_path = model_path;
252252
}
253253

254254
// InitWithModelAndAdapter initializes an enforcer with a model and a database adapter.
255-
void Enforcer::InitWithModelAndAdapter(std::shared_ptr<Model> m, std::shared_ptr<Adapter> adapter) {
255+
void Enforcer::InitWithModelAndAdapter(const std::shared_ptr<Model>& m, std::shared_ptr<Adapter> adapter) {
256256
m_adapter = adapter;
257257

258258
m_model = m;
@@ -281,7 +281,7 @@ void Enforcer::Initialize() {
281281
// Because the policy is attached to a model, so the policy is invalidated and needs
282282
// to be reloaded by calling LoadPolicy().
283283
void Enforcer::LoadModel() {
284-
m_model = std::shared_ptr<Model>(Model::NewModelFromFile(m_model_path));
284+
m_model = Model::NewModelFromFile(m_model_path);
285285

286286
m_model->PrintModel();
287287
m_func_map.LoadFunctionMap();
@@ -295,7 +295,7 @@ std::shared_ptr<Model> Enforcer::GetModel() {
295295
}
296296

297297
// SetModel sets the current model.
298-
void Enforcer::SetModel(std::shared_ptr<Model> m) {
298+
void Enforcer::SetModel(const std::shared_ptr<Model>& m) {
299299
m_model = m;
300300
m_func_map.LoadFunctionMap();
301301

@@ -327,7 +327,7 @@ std::shared_ptr<RoleManager> Enforcer ::GetRoleManager() {
327327
}
328328

329329
// SetRoleManager sets the current role manager.
330-
void Enforcer::SetRoleManager(std::shared_ptr<RoleManager> rm) {
330+
void Enforcer::SetRoleManager(std::shared_ptr<RoleManager>& rm) {
331331
this->rm = rm;
332332
}
333333

@@ -344,7 +344,7 @@ void Enforcer::ClearPolicy() {
344344
// LoadPolicy reloads the policy from file/database.
345345
void Enforcer::LoadPolicy() {
346346
this->ClearPolicy();
347-
m_adapter->LoadPolicy(m_model.get());
347+
m_adapter->LoadPolicy(m_model);
348348
m_model->PrintPolicy();
349349

350350
if(m_auto_build_role_links) {
@@ -381,12 +381,12 @@ void Enforcer::SavePolicy() {
381381
if(this->IsFiltered())
382382
throw CasbinEnforcerException("cannot save a filtered policy");
383383

384-
m_adapter->SavePolicy(m_model.get());
384+
m_adapter->SavePolicy(m_model);
385385

386386
if(m_watcher != NULL){
387387
if (IsInstanceOf<WatcherEx>(m_watcher.get())) {
388388
auto watcher = dynamic_cast<WatcherEx*>(m_watcher.get());
389-
watcher->UpdateForSavePolicy(m_model.get());
389+
watcher->UpdateForSavePolicy(m_model);
390390
}
391391
else
392392
return m_watcher->Update();

casbin/enforcer.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ class Enforcer : public IEnforcer {
7979
* @param m the model.
8080
* @param adapter the adapter.
8181
*/
82-
Enforcer(std::shared_ptr<Model> m, std::shared_ptr<Adapter> adapter);
82+
Enforcer(const std::shared_ptr<Model>& m, std::shared_ptr<Adapter> adapter);
8383
/**
8484
* Enforcer initializes an enforcer with a model.
8585
*
8686
* @param m the model.
8787
*/
88-
Enforcer(std::shared_ptr<Model> m);
88+
Enforcer(const std::shared_ptr<Model>& m);
8989
/**
9090
* Enforcer initializes an enforcer with a model file.
9191
*
@@ -105,7 +105,7 @@ class Enforcer : public IEnforcer {
105105
// InitWithAdapter initializes an enforcer with a database adapter.
106106
void InitWithAdapter(const std::string& model_path, std::shared_ptr<Adapter> adapter);
107107
// InitWithModelAndAdapter initializes an enforcer with a model and a database adapter.
108-
void InitWithModelAndAdapter(std::shared_ptr<Model> m, std::shared_ptr<Adapter> adapter);
108+
void InitWithModelAndAdapter(const std::shared_ptr<Model>& m, std::shared_ptr<Adapter> adapter);
109109
void Initialize();
110110
// LoadModel reloads the model from the model CONF file.
111111
// Because the policy is attached to a model, so the policy is invalidated and
@@ -114,7 +114,7 @@ class Enforcer : public IEnforcer {
114114
// GetModel gets the current model.
115115
std::shared_ptr<Model> GetModel();
116116
// SetModel sets the current model.
117-
void SetModel(std::shared_ptr<Model> m);
117+
void SetModel(const std::shared_ptr<Model>& m);
118118
// GetAdapter gets the current adapter.
119119
std::shared_ptr<Adapter> GetAdapter();
120120
// SetAdapter sets the current adapter.
@@ -124,7 +124,7 @@ class Enforcer : public IEnforcer {
124124
// GetRoleManager gets the current role manager.
125125
std::shared_ptr<RoleManager> GetRoleManager();
126126
// SetRoleManager sets the current role manager.
127-
void SetRoleManager(std::shared_ptr <RoleManager> rm);
127+
void SetRoleManager(std::shared_ptr<RoleManager>& rm);
128128
// SetEffector sets the current effector.
129129
void SetEffector(std::shared_ptr<Effector> eft);
130130
// ClearPolicy clears all policy.

casbin/enforcer_cached.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ CachedEnforcer ::CachedEnforcer(const std::string& model_path, std::shared_ptr<A
6666
* @param m the model.
6767
* @param adapter the adapter.
6868
*/
69-
CachedEnforcer ::CachedEnforcer(std::shared_ptr<Model> m, std::shared_ptr<Adapter> adapter)
69+
CachedEnforcer ::CachedEnforcer(const std::shared_ptr<Model>& m, std::shared_ptr<Adapter> adapter)
7070
: Enforcer(m, adapter) {
7171
this->enableCache = true;
7272
}
@@ -76,7 +76,7 @@ CachedEnforcer ::CachedEnforcer(std::shared_ptr<Model> m, std::shared_ptr<Adapte
7676
*
7777
* @param m the model.
7878
*/
79-
CachedEnforcer ::CachedEnforcer(std::shared_ptr<Model> m)
79+
CachedEnforcer ::CachedEnforcer(const std::shared_ptr<Model>& m)
8080
: Enforcer(m) {
8181
this->enableCache = true;
8282
}

casbin/enforcer_cached.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ class CachedEnforcer : public Enforcer {
6262
* @param m the model.
6363
* @param adapter the adapter.
6464
*/
65-
CachedEnforcer(std::shared_ptr<Model> m, std::shared_ptr<Adapter> adapter);
65+
CachedEnforcer(const std::shared_ptr<Model>& m, std::shared_ptr<Adapter> adapter);
6666
/**
6767
* Enforcer initializes an enforcer with a model.
6868
*
6969
* @param m the model.
7070
*/
71-
CachedEnforcer(std::shared_ptr<Model> m);
71+
CachedEnforcer(const std::shared_ptr<Model>& m);
7272
/**
7373
* Enforcer initializes an enforcer with a model file.
7474
*

casbin/enforcer_interface.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ class IEnforcer {
3333
/* Enforcer API */
3434
virtual void InitWithFile(const std::string& model_path, const std::string& policy_path) = 0;
3535
virtual void InitWithAdapter(const std::string& model_path, std::shared_ptr<Adapter> adapter) = 0;
36-
virtual void InitWithModelAndAdapter(std::shared_ptr<Model> m, std::shared_ptr<Adapter> adapter) = 0;
36+
virtual void InitWithModelAndAdapter(const std::shared_ptr<Model>& m, std::shared_ptr<Adapter> adapter) = 0;
3737
virtual void Initialize() = 0;
3838
virtual void LoadModel() = 0;
3939
virtual std::shared_ptr<Model> GetModel() = 0;
40-
virtual void SetModel(std::shared_ptr<Model> m) = 0;
40+
virtual void SetModel(const std::shared_ptr<Model>& m) = 0;
4141
virtual std::shared_ptr<Adapter> GetAdapter() = 0;
4242
virtual void SetAdapter(std::shared_ptr<Adapter> adapter) = 0;
4343
virtual void SetWatcher(std::shared_ptr<Watcher> watcher) = 0;
4444
virtual std::shared_ptr<RoleManager> GetRoleManager() = 0;
45-
virtual void SetRoleManager(std::shared_ptr<RoleManager> rm) = 0;
45+
virtual void SetRoleManager(std::shared_ptr<RoleManager>& rm) = 0;
4646
virtual void SetEffector(std::shared_ptr<Effector> eft) = 0;
4747
virtual void ClearPolicy() = 0;
4848
virtual void LoadPolicy() = 0;

casbin/enforcer_synced.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ SyncedEnforcer ::SyncedEnforcer(const std::string& model_path, std::shared_ptr<A
5959
* @param m the model.
6060
* @param adapter the adapter.
6161
*/
62-
SyncedEnforcer ::SyncedEnforcer(std::shared_ptr<Model> m, std::shared_ptr<Adapter> adapter)
62+
SyncedEnforcer ::SyncedEnforcer(const std::shared_ptr<Model>& m, std::shared_ptr<Adapter> adapter)
6363
: Enforcer(m, adapter), autoLoadRunning(false) {}
6464

6565
/**
6666
* Enforcer initializes an enforcer with a model.
6767
*
6868
* @param m the model.
6969
*/
70-
SyncedEnforcer ::SyncedEnforcer(std::shared_ptr<Model> m)
70+
SyncedEnforcer ::SyncedEnforcer(const std::shared_ptr<Model>& m)
7171
: Enforcer(m), autoLoadRunning(false) {}
7272

7373
/**

casbin/enforcer_synced.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ class SyncedEnforcer : public Enforcer {
6262
* @param m the model.
6363
* @param adapter the adapter.
6464
*/
65-
SyncedEnforcer(std::shared_ptr<Model> m, std::shared_ptr<Adapter> adapter);
65+
SyncedEnforcer(const std::shared_ptr<Model>& m, std::shared_ptr<Adapter> adapter);
6666

6767
/**
6868
* Enforcer initializes an enforcer with a model.
6969
*
7070
* @param m the model.
7171
*/
72-
SyncedEnforcer(std::shared_ptr<Model> m);
72+
SyncedEnforcer(const std::shared_ptr<Model>& m);
7373

7474
/**
7575
* Enforcer initializes an enforcer with a model file.

casbin/model/assertion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
namespace casbin {
2929

30-
void Assertion::BuildIncrementalRoleLinks(std::shared_ptr<RoleManager> rm, policy_op op, const std::vector<std::vector<std::string>>& rules) {
30+
void Assertion::BuildIncrementalRoleLinks(std::shared_ptr<RoleManager>& rm, policy_op op, const std::vector<std::vector<std::string>>& rules) {
3131
this->rm = rm;
3232
size_t char_count = count(this->value.begin(), this->value.end(), '_');
3333

@@ -53,7 +53,7 @@ void Assertion::BuildIncrementalRoleLinks(std::shared_ptr<RoleManager> rm, polic
5353
}
5454
}
5555

56-
void Assertion::BuildRoleLinks(std::shared_ptr<RoleManager> rm) {
56+
void Assertion::BuildRoleLinks(std::shared_ptr<RoleManager>& rm) {
5757
this->rm = rm;
5858
size_t char_count = count(this->value.begin(), this->value.end(), '_');
5959

casbin/model/assertion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class Assertion {
4040
std::vector<std::vector<std::string>> policy;
4141
std::shared_ptr<RoleManager> rm;
4242

43-
void BuildIncrementalRoleLinks(std::shared_ptr<RoleManager> rm, policy_op op, const std::vector<std::vector<std::string>>& rules);
43+
void BuildIncrementalRoleLinks(std::shared_ptr<RoleManager>& rm, policy_op op, const std::vector<std::vector<std::string>>& rules);
4444

45-
void BuildRoleLinks(std::shared_ptr<RoleManager> rm);
45+
void BuildRoleLinks(std::shared_ptr<RoleManager>& rm);
4646
};
4747

4848
}; // namespace casbin

casbin/model/model.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ std::vector<std::string> Model::required_sections{"r","p","e","m"};
4141

4242
void Model::LoadModelFromConfig(std::shared_ptr<ConfigInterface> cfg) {
4343
for (auto [section_name, _] : section_name_map)
44-
LoadSection(this, cfg, section_name);
44+
LoadSection(this ,cfg, section_name);
4545

4646
std::vector<std::string> ms;
4747
ms.reserve(required_sections.size());
@@ -58,10 +58,10 @@ bool Model::HasSection(const std::string& sec) {
5858
return this->m.find(sec) != this->m.end();
5959
}
6060

61-
void Model::LoadSection(Model* model, std::shared_ptr<ConfigInterface> cfg, const std::string& sec) {
61+
void Model::LoadSection(Model* raw_ptr, std::shared_ptr<ConfigInterface> cfg, const std::string& sec) {
6262
int i = 1;
6363
while(true) {
64-
if (!LoadAssertion(model, cfg, sec, sec + GetKeySuffix(i)))
64+
if (!LoadAssertion(raw_ptr, cfg, sec, sec + GetKeySuffix(i)))
6565
break;
6666
else
6767
i++;
@@ -74,9 +74,9 @@ std::string Model::GetKeySuffix(int i) {
7474
return std::to_string(i);
7575
}
7676

77-
bool Model::LoadAssertion(Model* model, std::shared_ptr<ConfigInterface> cfg, const std::string& sec, const std::string& key) {
77+
bool Model::LoadAssertion(Model* raw_ptr, std::shared_ptr<ConfigInterface> cfg, const std::string& sec, const std::string& key) {
7878
std::string value = cfg->GetString(section_name_map[sec] + "::" + key);
79-
return model->AddDef(sec, key, value);
79+
return raw_ptr->AddDef(sec, key, value);
8080
}
8181

8282
// AddDef adds an assertion to the model.
@@ -141,31 +141,31 @@ Model::Model(const std::string& path){
141141
}
142142

143143
// NewModel creates an empty model.
144-
Model* Model::NewModel() {
145-
return new Model();
144+
std::shared_ptr<Model> Model::NewModel() {
145+
return std::make_shared<Model>();
146146
}
147147

148148
// NewModel creates a model from a .CONF file.
149-
Model* Model::NewModelFromFile(const std::string& path) {
150-
Model* m = NewModel();
149+
std::shared_ptr<Model> Model::NewModelFromFile(const std::string& path) {
150+
std::shared_ptr<Model> m = NewModel();
151151
m->LoadModel(path);
152152
return m;
153153
}
154154

155155
// NewModel creates a model from a std::string which contains model text.
156-
Model* Model::NewModelFromString(const std::string& text) {
157-
Model* m = NewModel();
156+
std::shared_ptr<Model> Model::NewModelFromString(const std::string& text) {
157+
std::shared_ptr<Model> m = NewModel();
158158
m->LoadModelFromText(text);
159159
return m;
160160
}
161161

162-
void Model::BuildIncrementalRoleLinks(std::shared_ptr<RoleManager> rm, policy_op op, const std::string& sec, const std::string& p_type, const std::vector<std::vector<std::string>>& rules) {
162+
void Model::BuildIncrementalRoleLinks(std::shared_ptr<RoleManager>& rm, policy_op op, const std::string& sec, const std::string& p_type, const std::vector<std::vector<std::string>>& rules) {
163163
if (sec == "g")
164164
this->m[sec].assertion_map[p_type]->BuildIncrementalRoleLinks(rm, op, rules);
165165
}
166166

167167
// BuildRoleLinks initializes the roles in RBAC.
168-
void Model::BuildRoleLinks(std::shared_ptr<RoleManager> rm) {
168+
void Model::BuildRoleLinks(std::shared_ptr<RoleManager>& rm) {
169169
for (auto [_, assertion_ptr] : this->m["g"].assertion_map)
170170
assertion_ptr->BuildRoleLinks(rm);
171171
}

0 commit comments

Comments
 (0)