Skip to content

Commit bf6096a

Browse files
committed
feat: add smart pointers.
Signed-off-by: ZipoChan <[email protected]>
1 parent 5eab53e commit bf6096a

File tree

9 files changed

+143
-140
lines changed

9 files changed

+143
-140
lines changed

casbin/enforcer.cpp

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ bool Enforcer :: enforce(string matcher, Scope scope) {
162162
/**
163163
* Enforcer is the default constructor.
164164
*/
165-
Enforcer* Enforcer :: NewEnforcer() {
166-
Enforcer* e = new Enforcer;
165+
shared_ptr<Enforcer> Enforcer :: NewEnforcer() {
166+
shared_ptr<Enforcer> e = shared_ptr<Enforcer>(new Enforcer());
167167
return e;
168168
}
169169

@@ -173,8 +173,8 @@ Enforcer* Enforcer :: NewEnforcer() {
173173
* @param model_path the path of the model file.
174174
* @param policyFile the path of the policy file.
175175
*/
176-
Enforcer* Enforcer :: NewEnforcer(string model_path, string policyFile) {
177-
return NewEnforcer(model_path, FileAdapter :: NewAdapter(policyFile));
176+
shared_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, string policyFile) {
177+
return NewEnforcer(model_path, shared_ptr<FileAdapter>(FileAdapter :: NewAdapter(policyFile)));
178178
}
179179

180180
/**
@@ -183,8 +183,8 @@ Enforcer* Enforcer :: NewEnforcer(string model_path, string policyFile) {
183183
* @param model_path the path of the model file.
184184
* @param adapter the adapter.
185185
*/
186-
Enforcer* Enforcer :: NewEnforcer(string model_path, Adapter* adapter) {
187-
Enforcer* e = NewEnforcer(Model :: NewModelFromFile(model_path), adapter);
186+
shared_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, shared_ptr<Adapter> adapter) {
187+
shared_ptr<Enforcer> e = NewEnforcer(shared_ptr<Model>(Model :: NewModelFromFile(model_path)), adapter);
188188
e->model_path = model_path;
189189
return e;
190190
}
@@ -195,8 +195,8 @@ Enforcer* Enforcer :: NewEnforcer(string model_path, Adapter* adapter) {
195195
* @param m the model.
196196
* @param adapter the adapter.
197197
*/
198-
Enforcer* Enforcer :: NewEnforcer(Model* m, Adapter* adapter) {
199-
Enforcer* e = new Enforcer;
198+
shared_ptr<Enforcer> Enforcer :: NewEnforcer(shared_ptr<Model> m, shared_ptr<Adapter> adapter) {
199+
shared_ptr<Enforcer> e = shared_ptr<Enforcer>(new Enforcer());
200200
e->adapter = adapter;
201201
e->watcher = NULL;
202202

@@ -217,7 +217,7 @@ Enforcer* Enforcer :: NewEnforcer(Model* m, Adapter* adapter) {
217217
*
218218
* @param m the model.
219219
*/
220-
Enforcer* Enforcer :: NewEnforcer(Model* m) {
220+
shared_ptr<Enforcer> Enforcer :: NewEnforcer(shared_ptr<Model> m) {
221221
return NewEnforcer(m, NULL);
222222
}
223223

@@ -226,7 +226,7 @@ Enforcer* Enforcer :: NewEnforcer(Model* m) {
226226
*
227227
* @param model_path the path of the model file.
228228
*/
229-
Enforcer* Enforcer :: NewEnforcer(string model_path) {
229+
shared_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path) {
230230
return NewEnforcer(model_path, "");
231231
}
232232

@@ -237,30 +237,30 @@ Enforcer* Enforcer :: NewEnforcer(string model_path) {
237237
* @param policyFile the path of the policy file.
238238
* @param enableLog whether to enable Casbin's log.
239239
*/
240-
Enforcer* Enforcer :: NewEnforcer(string model_path, string policyFile, bool enableLog) {
241-
Enforcer* e = NewEnforcer(model_path, FileAdapter :: NewAdapter(policyFile));
240+
shared_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, string policyFile, bool enableLog) {
241+
shared_ptr<Enforcer> e = NewEnforcer(model_path, shared_ptr<FileAdapter>(FileAdapter :: NewAdapter(policyFile)));
242242
// e.EnableLog(enableLog);
243243
return e;
244244
}
245245

246246

247247
// InitWithFile initializes an enforcer with a model file and a policy file.
248248
void Enforcer :: InitWithFile(string model_path, string policyPath) {
249-
Adapter* a = FileAdapter::NewAdapter(policyPath);
249+
shared_ptr<Adapter> a = shared_ptr<FileAdapter>(FileAdapter::NewAdapter(policyPath));
250250
this->InitWithAdapter(model_path, a);
251251
}
252252

253253
// InitWithAdapter initializes an enforcer with a database adapter.
254-
void Enforcer :: InitWithAdapter(string model_path, Adapter* adapter) {
255-
Model* m = Model :: NewModelFromFile(model_path);
254+
void Enforcer :: InitWithAdapter(string model_path, shared_ptr<Adapter> adapter) {
255+
shared_ptr<Model> m =shared_ptr<Model>(Model :: NewModelFromFile(model_path));
256256

257257
this->InitWithModelAndAdapter(m, adapter);
258258

259259
this->model_path = model_path;
260260
}
261261

262262
// InitWithModelAndAdapter initializes an enforcer with a model and a database adapter.
263-
void Enforcer :: InitWithModelAndAdapter(Model* m, Adapter* adapter) {
263+
void Enforcer :: InitWithModelAndAdapter(shared_ptr<Model> m, shared_ptr<Adapter> adapter) {
264264
this->adapter = adapter;
265265

266266
this->model = m;
@@ -275,8 +275,8 @@ void Enforcer :: InitWithModelAndAdapter(Model* m, Adapter* adapter) {
275275
}
276276

277277
void Enforcer :: Initialize() {
278-
this->rm = DefaultRoleManager :: NewRoleManager(10);
279-
this->eft = DefaultEffector :: NewDefaultEffector();
278+
this->rm = shared_ptr<DefaultRoleManager>(DefaultRoleManager :: NewRoleManager(10));
279+
this->eft = shared_ptr<DefaultEffector>(DefaultEffector :: NewDefaultEffector());
280280
this->watcher = NULL;
281281

282282
this->enabled = true;
@@ -288,7 +288,7 @@ void Enforcer :: Initialize() {
288288
// LoadModel reloads the model from the model CONF file.
289289
// Because the policy is attached to a model, so the policy is invalidated and needs to be reloaded by calling LoadPolicy().
290290
void Enforcer :: LoadModel() {
291-
this->model = Model :: NewModelFromFile(this->model_path);
291+
this->model = shared_ptr<Model>(Model ::NewModelFromFile(this->model_path));
292292

293293
this->model->PrintModel();
294294
this->func_map.LoadFunctionMap();
@@ -297,30 +297,30 @@ void Enforcer :: LoadModel() {
297297
}
298298

299299
// GetModel gets the current model.
300-
Model* Enforcer :: GetModel() {
300+
shared_ptr<Model> Enforcer :: GetModel() {
301301
return this->model;
302302
}
303303

304304
// SetModel sets the current model.
305-
void Enforcer :: SetModel(Model* m) {
305+
void Enforcer :: SetModel(shared_ptr<Model> m) {
306306
this->model = m;
307307
this->func_map.LoadFunctionMap();
308308

309309
this->Initialize();
310310
}
311311

312312
// GetAdapter gets the current adapter.
313-
Adapter* Enforcer :: GetAdapter() {
313+
shared_ptr<Adapter> Enforcer::GetAdapter() {
314314
return this->adapter;
315315
}
316316

317317
// SetAdapter sets the current adapter.
318-
void Enforcer :: SetAdapter(Adapter* adapter) {
318+
void Enforcer::SetAdapter(shared_ptr<Adapter> adapter) {
319319
this->adapter = adapter;
320320
}
321321

322322
// SetWatcher sets the current watcher.
323-
void Enforcer :: SetWatcher(Watcher* watcher) {
323+
void Enforcer :: SetWatcher(shared_ptr<Watcher> watcher) {
324324
this->watcher = watcher;
325325
auto func = [&, this](string str) {
326326
this->LoadPolicy();
@@ -329,17 +329,17 @@ void Enforcer :: SetWatcher(Watcher* watcher) {
329329
}
330330

331331
// GetRoleManager gets the current role manager.
332-
RoleManager* Enforcer :: GetRoleManager() {
332+
shared_ptr<RoleManager> Enforcer ::GetRoleManager() {
333333
return this->rm;
334334
}
335335

336336
// SetRoleManager sets the current role manager.
337-
void Enforcer :: SetRoleManager(RoleManager* rm) {
337+
void Enforcer :: SetRoleManager(shared_ptr<RoleManager> rm) {
338338
this->rm = rm;
339339
}
340340

341341
// SetEffector sets the current effector.
342-
void Enforcer :: SetEffector(Effector* eft) {
342+
void Enforcer :: SetEffector(shared_ptr<Effector> eft) {
343343
this->eft = eft;
344344
}
345345

@@ -351,7 +351,7 @@ void Enforcer :: ClearPolicy() {
351351
// LoadPolicy reloads the policy from file/database.
352352
void Enforcer :: LoadPolicy() {
353353
this->model->ClearPolicy();
354-
this->adapter->LoadPolicy(this->model);
354+
this->adapter->LoadPolicy(this->model.get());
355355
this->model->PrintPolicy();
356356

357357
if(this->auto_build_role_links) {
@@ -367,7 +367,7 @@ void Enforcer :: LoadFilteredPolicy(Filter filter) {
367367
FilteredAdapter* filteredAdapter;
368368

369369
if (this->adapter->IsFiltered()) {
370-
void* adapter = this->adapter;
370+
void* adapter = this->adapter.get();
371371
filteredAdapter = (FilteredAdapter*)adapter;
372372
}
373373
else
@@ -390,12 +390,12 @@ void Enforcer :: SavePolicy() {
390390
if(this->IsFiltered())
391391
throw CasbinEnforcerException("cannot save a filtered policy");
392392

393-
this->adapter->SavePolicy(this->model);
393+
this->adapter->SavePolicy(this->model.get());
394394

395395
if(this->watcher != NULL){
396-
if (IsInstanceOf<WatcherEx>(this->watcher)) {
397-
void* watcher = this->watcher;
398-
((WatcherEx*)watcher)->UpdateForSavePolicy(this->model);
396+
if (IsInstanceOf<WatcherEx>(this->watcher.get())) {
397+
void* watcher = this->watcher.get();
398+
((WatcherEx*)watcher)->UpdateForSavePolicy(this->model.get());
399399
}
400400
else
401401
return this->watcher->Update();
@@ -431,12 +431,12 @@ void Enforcer :: EnableAutoBuildRoleLinks(bool auto_build_role_links) {
431431
void Enforcer :: BuildRoleLinks() {
432432
this->rm->Clear();
433433

434-
this->model->BuildRoleLinks(this->rm);
434+
this->model->BuildRoleLinks(this->rm.get());
435435
}
436436

437437
// BuildIncrementalRoleLinks provides incremental build the role inheritance relations.
438438
void Enforcer :: BuildIncrementalRoleLinks(policy_op op, string p_type, vector<vector<string>> rules) {
439-
return this->model->BuildIncrementalRoleLinks(this->rm, op, "g", p_type, rules);
439+
return this->model->BuildIncrementalRoleLinks(this->rm.get(), op, "g", p_type, rules);
440440
}
441441

442442
// Enforce decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).

casbin/enforcer.h

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef CASBIN_CPP_ENFORCER
1818
#define CASBIN_CPP_ENFORCER
1919

20+
#include<memory>
2021
#include "./rbac/role_manager.h"
2122
#include "./model/function.h"
2223
#include "./enforcer_interface.h"
@@ -27,12 +28,12 @@ class Enforcer : public IEnforcer{
2728
private:
2829

2930
string model_path;
30-
Model* model;
31+
shared_ptr<Model> model;
3132
FunctionMap func_map;
32-
Effector* eft;
33+
shared_ptr<Effector> eft;
3334

34-
Adapter* adapter;
35-
Watcher* watcher;
35+
shared_ptr<Adapter> adapter;
36+
shared_ptr<Watcher> watcher;
3637

3738
bool enabled;
3839
bool auto_save;
@@ -44,79 +45,79 @@ class Enforcer : public IEnforcer{
4445

4546
public:
4647

47-
RoleManager* rm;
48+
shared_ptr<RoleManager> rm;
4849

4950
/**
5051
* Enforcer is the default constructor.
5152
*/
52-
static Enforcer* NewEnforcer();
53+
static shared_ptr<Enforcer> NewEnforcer();
5354
/**
5455
* Enforcer initializes an enforcer with a model file and a policy file.
5556
*
5657
* @param model_path the path of the model file.
5758
* @param policyFile the path of the policy file.
5859
*/
59-
static Enforcer* NewEnforcer(string model_path, string policyFile);
60+
static shared_ptr<Enforcer> NewEnforcer(string model_path, string policyFile);
6061
/**
6162
* Enforcer initializes an enforcer with a database adapter.
6263
*
6364
* @param model_path the path of the model file.
6465
* @param adapter the adapter.
6566
*/
66-
static Enforcer* NewEnforcer(string model_path, Adapter* adapter);
67+
static shared_ptr<Enforcer> NewEnforcer(string model_path, shared_ptr<Adapter> adapter);
6768
/**
6869
* Enforcer initializes an enforcer with a model and a database adapter.
6970
*
7071
* @param m the model.
7172
* @param adapter the adapter.
7273
*/
73-
static Enforcer* NewEnforcer(Model* m, Adapter* adapter);
74+
static shared_ptr<Enforcer> NewEnforcer(shared_ptr<Model> m, shared_ptr<Adapter> adapter);
7475
/**
7576
* Enforcer initializes an enforcer with a model.
7677
*
7778
* @param m the model.
7879
*/
79-
static Enforcer* NewEnforcer(Model* m);
80+
static shared_ptr<Enforcer> NewEnforcer(shared_ptr<Model> m);
8081
/**
8182
* Enforcer initializes an enforcer with a model file.
8283
*
8384
* @param model_path the path of the model file.
8485
*/
85-
static Enforcer* NewEnforcer(string model_path);
86+
static shared_ptr<Enforcer> NewEnforcer(string model_path);
8687
/**
8788
* Enforcer initializes an enforcer with a model file, a policy file and an enable log flag.
8889
*
8990
* @param model_path the path of the model file.
9091
* @param policyFile the path of the policy file.
9192
* @param enableLog whether to enable Casbin's log.
9293
*/
93-
static Enforcer* NewEnforcer(string model_path, string policyFile, bool enableLog);
94+
static shared_ptr<Enforcer> NewEnforcer(string model_path, string policyFile, bool enableLog);
9495
// InitWithFile initializes an enforcer with a model file and a policy file.
9596
void InitWithFile(string model_path, string policyPath);
9697
// InitWithAdapter initializes an enforcer with a database adapter.
97-
void InitWithAdapter(string model_path, Adapter* adapter);
98+
void InitWithAdapter(string model_path, shared_ptr<Adapter> adapter);
9899
// InitWithModelAndAdapter initializes an enforcer with a model and a database adapter.
99-
void InitWithModelAndAdapter(Model* m, Adapter* adapter);
100+
void InitWithModelAndAdapter(shared_ptr<Model> m, shared_ptr<Adapter> adapter);
100101
void Initialize();
101102
// LoadModel reloads the model from the model CONF file.
102103
// Because the policy is attached to a model, so the policy is invalidated and needs to be reloaded by calling LoadPolicy().
103104
void LoadModel();
104105
// GetModel gets the current model.
105-
Model* GetModel();
106+
shared_ptr<Model> GetModel();
106107
// SetModel sets the current model.
107-
void SetModel(Model* m);
108+
void SetModel(shared_ptr<Model> m);
108109
// GetAdapter gets the current adapter.
109-
Adapter* GetAdapter();
110+
shared_ptr<Adapter> GetAdapter();
110111
// SetAdapter sets the current adapter.
111-
void SetAdapter(Adapter* adapter);
112+
void SetAdapter(shared_ptr<Adapter> adapter);
112113
// SetWatcher sets the current watcher.
113-
void SetWatcher(Watcher* watcher);
114+
void SetWatcher(shared_ptr<Watcher> watcher);
114115
// GetRoleManager gets the current role manager.
115-
RoleManager* GetRoleManager();
116+
shared_ptr<RoleManager> GetRoleManager();
116117
// SetRoleManager sets the current role manager.
117-
void SetRoleManager(RoleManager* rm);
118+
void SetRoleManager(shared_ptr <RoleManager> rm);
118119
// SetEffector sets the current effector.
119-
void SetEffector(Effector* eft);
120+
void SetEffector(shared_ptr<Effector> eft);
120121
// ClearPolicy clears all policy.
121122
void ClearPolicy();
122123
// LoadPolicy reloads the policy from file/database.

casbin/enforcer_interface.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ class IEnforcer {
2929

3030
/* Enforcer API */
3131
virtual void InitWithFile(string modelPath, string policyPath) = 0;
32-
virtual void InitWithAdapter(string modelPath, Adapter* adapter) = 0;
33-
virtual void InitWithModelAndAdapter(Model* m, Adapter* adapter) = 0;
32+
virtual void InitWithAdapter(string modelPath, shared_ptr<Adapter> adapter) = 0;
33+
virtual void InitWithModelAndAdapter(shared_ptr<Model> m, shared_ptr<Adapter> adapter) = 0;
3434
virtual void Initialize() = 0;
3535
virtual void LoadModel() = 0;
36-
virtual Model* GetModel() = 0;
37-
virtual void SetModel(Model* m) = 0;
38-
virtual Adapter* GetAdapter() = 0;
39-
virtual void SetAdapter(Adapter* adapter) = 0;
40-
virtual void SetWatcher(Watcher* watcher) = 0;
41-
virtual RoleManager* GetRoleManager() = 0;
42-
virtual void SetRoleManager(RoleManager* rm) = 0;
43-
virtual void SetEffector(Effector* eft) = 0;
36+
virtual shared_ptr<Model> GetModel() = 0;
37+
virtual void SetModel(shared_ptr<Model> m) = 0;
38+
virtual shared_ptr<Adapter> GetAdapter() = 0;
39+
virtual void SetAdapter(shared_ptr<Adapter> adapter) = 0;
40+
virtual void SetWatcher(shared_ptr<Watcher> watcher) = 0;
41+
virtual shared_ptr<RoleManager> GetRoleManager() = 0;
42+
virtual void SetRoleManager(shared_ptr<RoleManager> rm) = 0;
43+
virtual void SetEffector(shared_ptr<Effector> eft) = 0;
4444
virtual void ClearPolicy() = 0;
4545
virtual void LoadPolicy() = 0;
4646

0 commit comments

Comments
 (0)