Skip to content

Commit

Permalink
feat: add smart pointers.
Browse files Browse the repository at this point in the history
Signed-off-by: ZipoChan <[email protected]>
  • Loading branch information
ZipoChan committed Jul 17, 2020
1 parent 5eab53e commit bf6096a
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 140 deletions.
70 changes: 35 additions & 35 deletions casbin/enforcer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ bool Enforcer :: enforce(string matcher, Scope scope) {
/**
* Enforcer is the default constructor.
*/
Enforcer* Enforcer :: NewEnforcer() {
Enforcer* e = new Enforcer;
shared_ptr<Enforcer> Enforcer :: NewEnforcer() {
shared_ptr<Enforcer> e = shared_ptr<Enforcer>(new Enforcer());
return e;
}

Expand All @@ -173,8 +173,8 @@ Enforcer* Enforcer :: NewEnforcer() {
* @param model_path the path of the model file.
* @param policyFile the path of the policy file.
*/
Enforcer* Enforcer :: NewEnforcer(string model_path, string policyFile) {
return NewEnforcer(model_path, FileAdapter :: NewAdapter(policyFile));
shared_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, string policyFile) {
return NewEnforcer(model_path, shared_ptr<FileAdapter>(FileAdapter :: NewAdapter(policyFile)));
}

/**
Expand All @@ -183,8 +183,8 @@ Enforcer* Enforcer :: NewEnforcer(string model_path, string policyFile) {
* @param model_path the path of the model file.
* @param adapter the adapter.
*/
Enforcer* Enforcer :: NewEnforcer(string model_path, Adapter* adapter) {
Enforcer* e = NewEnforcer(Model :: NewModelFromFile(model_path), adapter);
shared_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path, shared_ptr<Adapter> adapter) {
shared_ptr<Enforcer> e = NewEnforcer(shared_ptr<Model>(Model :: NewModelFromFile(model_path)), adapter);
e->model_path = model_path;
return e;
}
Expand All @@ -195,8 +195,8 @@ Enforcer* Enforcer :: NewEnforcer(string model_path, Adapter* adapter) {
* @param m the model.
* @param adapter the adapter.
*/
Enforcer* Enforcer :: NewEnforcer(Model* m, Adapter* adapter) {
Enforcer* e = new Enforcer;
shared_ptr<Enforcer> Enforcer :: NewEnforcer(shared_ptr<Model> m, shared_ptr<Adapter> adapter) {
shared_ptr<Enforcer> e = shared_ptr<Enforcer>(new Enforcer());
e->adapter = adapter;
e->watcher = NULL;

Expand All @@ -217,7 +217,7 @@ Enforcer* Enforcer :: NewEnforcer(Model* m, Adapter* adapter) {
*
* @param m the model.
*/
Enforcer* Enforcer :: NewEnforcer(Model* m) {
shared_ptr<Enforcer> Enforcer :: NewEnforcer(shared_ptr<Model> m) {
return NewEnforcer(m, NULL);
}

Expand All @@ -226,7 +226,7 @@ Enforcer* Enforcer :: NewEnforcer(Model* m) {
*
* @param model_path the path of the model file.
*/
Enforcer* Enforcer :: NewEnforcer(string model_path) {
shared_ptr<Enforcer> Enforcer :: NewEnforcer(string model_path) {
return NewEnforcer(model_path, "");
}

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


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

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

this->InitWithModelAndAdapter(m, adapter);

this->model_path = model_path;
}

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

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

void Enforcer :: Initialize() {
this->rm = DefaultRoleManager :: NewRoleManager(10);
this->eft = DefaultEffector :: NewDefaultEffector();
this->rm = shared_ptr<DefaultRoleManager>(DefaultRoleManager :: NewRoleManager(10));
this->eft = shared_ptr<DefaultEffector>(DefaultEffector :: NewDefaultEffector());
this->watcher = NULL;

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

this->model->PrintModel();
this->func_map.LoadFunctionMap();
Expand All @@ -297,30 +297,30 @@ void Enforcer :: LoadModel() {
}

// GetModel gets the current model.
Model* Enforcer :: GetModel() {
shared_ptr<Model> Enforcer :: GetModel() {
return this->model;
}

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

this->Initialize();
}

// GetAdapter gets the current adapter.
Adapter* Enforcer :: GetAdapter() {
shared_ptr<Adapter> Enforcer::GetAdapter() {
return this->adapter;
}

// SetAdapter sets the current adapter.
void Enforcer :: SetAdapter(Adapter* adapter) {
void Enforcer::SetAdapter(shared_ptr<Adapter> adapter) {
this->adapter = adapter;
}

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

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

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

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

Expand All @@ -351,7 +351,7 @@ void Enforcer :: ClearPolicy() {
// LoadPolicy reloads the policy from file/database.
void Enforcer :: LoadPolicy() {
this->model->ClearPolicy();
this->adapter->LoadPolicy(this->model);
this->adapter->LoadPolicy(this->model.get());
this->model->PrintPolicy();

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

if (this->adapter->IsFiltered()) {
void* adapter = this->adapter;
void* adapter = this->adapter.get();
filteredAdapter = (FilteredAdapter*)adapter;
}
else
Expand All @@ -390,12 +390,12 @@ void Enforcer :: SavePolicy() {
if(this->IsFiltered())
throw CasbinEnforcerException("cannot save a filtered policy");

this->adapter->SavePolicy(this->model);
this->adapter->SavePolicy(this->model.get());

if(this->watcher != NULL){
if (IsInstanceOf<WatcherEx>(this->watcher)) {
void* watcher = this->watcher;
((WatcherEx*)watcher)->UpdateForSavePolicy(this->model);
if (IsInstanceOf<WatcherEx>(this->watcher.get())) {
void* watcher = this->watcher.get();
((WatcherEx*)watcher)->UpdateForSavePolicy(this->model.get());
}
else
return this->watcher->Update();
Expand Down Expand Up @@ -431,12 +431,12 @@ void Enforcer :: EnableAutoBuildRoleLinks(bool auto_build_role_links) {
void Enforcer :: BuildRoleLinks() {
this->rm->Clear();

this->model->BuildRoleLinks(this->rm);
this->model->BuildRoleLinks(this->rm.get());
}

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

// Enforce decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
Expand Down
45 changes: 23 additions & 22 deletions casbin/enforcer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef CASBIN_CPP_ENFORCER
#define CASBIN_CPP_ENFORCER

#include<memory>
#include "./rbac/role_manager.h"
#include "./model/function.h"
#include "./enforcer_interface.h"
Expand All @@ -27,12 +28,12 @@ class Enforcer : public IEnforcer{
private:

string model_path;
Model* model;
shared_ptr<Model> model;
FunctionMap func_map;
Effector* eft;
shared_ptr<Effector> eft;

Adapter* adapter;
Watcher* watcher;
shared_ptr<Adapter> adapter;
shared_ptr<Watcher> watcher;

bool enabled;
bool auto_save;
Expand All @@ -44,79 +45,79 @@ class Enforcer : public IEnforcer{

public:

RoleManager* rm;
shared_ptr<RoleManager> rm;

/**
* Enforcer is the default constructor.
*/
static Enforcer* NewEnforcer();
static shared_ptr<Enforcer> NewEnforcer();
/**
* Enforcer initializes an enforcer with a model file and a policy file.
*
* @param model_path the path of the model file.
* @param policyFile the path of the policy file.
*/
static Enforcer* NewEnforcer(string model_path, string policyFile);
static shared_ptr<Enforcer> NewEnforcer(string model_path, string policyFile);
/**
* Enforcer initializes an enforcer with a database adapter.
*
* @param model_path the path of the model file.
* @param adapter the adapter.
*/
static Enforcer* NewEnforcer(string model_path, Adapter* adapter);
static shared_ptr<Enforcer> NewEnforcer(string model_path, shared_ptr<Adapter> adapter);
/**
* Enforcer initializes an enforcer with a model and a database adapter.
*
* @param m the model.
* @param adapter the adapter.
*/
static Enforcer* NewEnforcer(Model* m, Adapter* adapter);
static shared_ptr<Enforcer> NewEnforcer(shared_ptr<Model> m, shared_ptr<Adapter> adapter);
/**
* Enforcer initializes an enforcer with a model.
*
* @param m the model.
*/
static Enforcer* NewEnforcer(Model* m);
static shared_ptr<Enforcer> NewEnforcer(shared_ptr<Model> m);
/**
* Enforcer initializes an enforcer with a model file.
*
* @param model_path the path of the model file.
*/
static Enforcer* NewEnforcer(string model_path);
static shared_ptr<Enforcer> NewEnforcer(string model_path);
/**
* Enforcer initializes an enforcer with a model file, a policy file and an enable log flag.
*
* @param model_path the path of the model file.
* @param policyFile the path of the policy file.
* @param enableLog whether to enable Casbin's log.
*/
static Enforcer* NewEnforcer(string model_path, string policyFile, bool enableLog);
static shared_ptr<Enforcer> NewEnforcer(string model_path, string policyFile, bool enableLog);
// InitWithFile initializes an enforcer with a model file and a policy file.
void InitWithFile(string model_path, string policyPath);
// InitWithAdapter initializes an enforcer with a database adapter.
void InitWithAdapter(string model_path, Adapter* adapter);
void InitWithAdapter(string model_path, shared_ptr<Adapter> adapter);
// InitWithModelAndAdapter initializes an enforcer with a model and a database adapter.
void InitWithModelAndAdapter(Model* m, Adapter* adapter);
void InitWithModelAndAdapter(shared_ptr<Model> m, shared_ptr<Adapter> adapter);
void Initialize();
// LoadModel reloads the model from the model CONF file.
// Because the policy is attached to a model, so the policy is invalidated and needs to be reloaded by calling LoadPolicy().
void LoadModel();
// GetModel gets the current model.
Model* GetModel();
shared_ptr<Model> GetModel();
// SetModel sets the current model.
void SetModel(Model* m);
void SetModel(shared_ptr<Model> m);
// GetAdapter gets the current adapter.
Adapter* GetAdapter();
shared_ptr<Adapter> GetAdapter();
// SetAdapter sets the current adapter.
void SetAdapter(Adapter* adapter);
void SetAdapter(shared_ptr<Adapter> adapter);
// SetWatcher sets the current watcher.
void SetWatcher(Watcher* watcher);
void SetWatcher(shared_ptr<Watcher> watcher);
// GetRoleManager gets the current role manager.
RoleManager* GetRoleManager();
shared_ptr<RoleManager> GetRoleManager();
// SetRoleManager sets the current role manager.
void SetRoleManager(RoleManager* rm);
void SetRoleManager(shared_ptr <RoleManager> rm);
// SetEffector sets the current effector.
void SetEffector(Effector* eft);
void SetEffector(shared_ptr<Effector> eft);
// ClearPolicy clears all policy.
void ClearPolicy();
// LoadPolicy reloads the policy from file/database.
Expand Down
20 changes: 10 additions & 10 deletions casbin/enforcer_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ class IEnforcer {

/* Enforcer API */
virtual void InitWithFile(string modelPath, string policyPath) = 0;
virtual void InitWithAdapter(string modelPath, Adapter* adapter) = 0;
virtual void InitWithModelAndAdapter(Model* m, Adapter* adapter) = 0;
virtual void InitWithAdapter(string modelPath, shared_ptr<Adapter> adapter) = 0;
virtual void InitWithModelAndAdapter(shared_ptr<Model> m, shared_ptr<Adapter> adapter) = 0;
virtual void Initialize() = 0;
virtual void LoadModel() = 0;
virtual Model* GetModel() = 0;
virtual void SetModel(Model* m) = 0;
virtual Adapter* GetAdapter() = 0;
virtual void SetAdapter(Adapter* adapter) = 0;
virtual void SetWatcher(Watcher* watcher) = 0;
virtual RoleManager* GetRoleManager() = 0;
virtual void SetRoleManager(RoleManager* rm) = 0;
virtual void SetEffector(Effector* eft) = 0;
virtual shared_ptr<Model> GetModel() = 0;
virtual void SetModel(shared_ptr<Model> m) = 0;
virtual shared_ptr<Adapter> GetAdapter() = 0;
virtual void SetAdapter(shared_ptr<Adapter> adapter) = 0;
virtual void SetWatcher(shared_ptr<Watcher> watcher) = 0;
virtual shared_ptr<RoleManager> GetRoleManager() = 0;
virtual void SetRoleManager(shared_ptr<RoleManager> rm) = 0;
virtual void SetEffector(shared_ptr<Effector> eft) = 0;
virtual void ClearPolicy() = 0;
virtual void LoadPolicy() = 0;

Expand Down
Loading

0 comments on commit bf6096a

Please sign in to comment.