Skip to content

Commit 546c5ac

Browse files
authored
Merge pull request #57 from ZipoChan/master
fix: Using smart pointers instead of raw pointer.
2 parents 5eab53e + 012f357 commit 546c5ac

File tree

9 files changed

+151
-176
lines changed

9 files changed

+151
-176
lines changed

casbin/enforcer.cpp

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

170170
/**
@@ -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+
unique_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, string policyFile) {
177+
return move(NewEnforcer(model_path, shared_ptr<FileAdapter>(FileAdapter :: NewAdapter(policyFile))));
178178
}
179179

180180
/**
@@ -183,10 +183,10 @@ 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+
unique_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, shared_ptr<Adapter> adapter) {
187+
unique_ptr<Enforcer> e = NewEnforcer(shared_ptr<Model>(Model :: NewModelFromFile(model_path)), adapter);
188188
e->model_path = model_path;
189-
return e;
189+
return move(e);
190190
}
191191

192192
/**
@@ -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+
unique_ptr<Enforcer> Enforcer :: NewEnforcer(shared_ptr<Model> m, shared_ptr<Adapter> adapter) {
199+
unique_ptr<Enforcer> e = unique_ptr<Enforcer>(new Enforcer());
200200
e->adapter = adapter;
201201
e->watcher = NULL;
202202

@@ -209,25 +209,25 @@ Enforcer* Enforcer :: NewEnforcer(Model* m, Adapter* adapter) {
209209
if (e->adapter->file_path != "") {
210210
e->LoadPolicy();
211211
}
212-
return e;
212+
return move(e);
213213
}
214214

215215
/**
216216
* Enforcer initializes an enforcer with a model.
217217
*
218218
* @param m the model.
219219
*/
220-
Enforcer* Enforcer :: NewEnforcer(Model* m) {
221-
return NewEnforcer(m, NULL);
220+
unique_ptr<Enforcer> Enforcer ::NewEnforcer(shared_ptr<Model> m) {
221+
return move(NewEnforcer(m, NULL));
222222
}
223223

224224
/**
225225
* Enforcer initializes an enforcer with a model file.
226226
*
227227
* @param model_path the path of the model file.
228228
*/
229-
Enforcer* Enforcer :: NewEnforcer(string model_path) {
230-
return NewEnforcer(model_path, "");
229+
unique_ptr<Enforcer> Enforcer ::NewEnforcer(string model_path) {
230+
return move(NewEnforcer(model_path, ""));
231231
}
232232

233233
/**
@@ -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+
unique_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, string policyFile, bool enableLog) {
241+
unique_ptr<Enforcer> e = NewEnforcer(model_path, shared_ptr<FileAdapter>(FileAdapter :: NewAdapter(policyFile)));
242242
// e.EnableLog(enableLog);
243-
return e;
243+
return move(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,29 +431,19 @@ 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).
443443
bool Enforcer :: Enforce(Scope scope) {
444444
return this->EnforceWithMatcher("", scope);
445445
}
446446

447-
// Enforce with three params, decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
448-
bool Enforcer::Enforce(string sub, string obj, string act) {
449-
return EnforceWithMatcher("", sub, obj, act);
450-
}
451-
452-
// Enforce with four params, decides whether a "subject" can access a "object" with the operation "action" in the domain "dom", input parameters are usually: (sub, dom, obj,act).
453-
bool Enforcer::Enforce(string sub, string dom, string obj, string act) {
454-
return EnforceWithMatcher("", sub, dom, obj, act);
455-
}
456-
457447
// Enforce with a vector param,decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
458448
bool Enforcer::Enforce(vector<string> params) {
459449
return this->EnforceWithMatcher("", params);
@@ -469,16 +459,6 @@ bool Enforcer :: EnforceWithMatcher(string matcher, Scope scope) {
469459
return this->enforce(matcher, scope);
470460
}
471461

472-
// Enforce with three params, decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
473-
bool Enforcer::EnforceWithMatcher(string matcher, string sub, string obj, string act) {
474-
return this->EnforceWithMatcher(matcher, { sub,obj,act });
475-
}
476-
477-
// Enforce with four params, decides whether a "subject" can access a "object" with the operation "action" in the domain "dom", input parameters are usually: (sub, dom, obj,act).
478-
bool Enforcer::EnforceWithMatcher(string matcher, string sub, string dom, string obj, string act) {
479-
return this->EnforceWithMatcher(matcher, { sub,dom,obj,act });
480-
}
481-
482462
// EnforceWithMatcher use a custom matcher to decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is "".
483463
bool Enforcer::EnforceWithMatcher(string matcher, vector<string> params) {
484464
vector <string> r_tokens = this->model->m["r"].assertion_map["r"]->tokens;

0 commit comments

Comments
 (0)