Skip to content

Commit 0025375

Browse files
authored
Merge pull request #125 from EmperorYP7/model-cleanup
chore: Model Cleanup
2 parents 42dd53e + a1d2ed6 commit 0025375

File tree

5 files changed

+98
-95
lines changed

5 files changed

+98
-95
lines changed

casbin/model/assertion.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,40 @@
2727

2828
namespace casbin {
2929

30-
void Assertion :: BuildIncrementalRoleLinks(std::shared_ptr<RoleManager> rm, policy_op op, 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;
32-
int char_count = int(count(this->value.begin(), this->value.end(), '_'));
32+
size_t char_count = count(this->value.begin(), this->value.end(), '_');
3333

3434
if (char_count < 2)
3535
throw IllegalArgumentException("the number of \"_\" in role definition should be at least 2");
3636

37-
for(int i = 0 ; i < rules.size() ; i++){
38-
std::vector<std::string> rule = rules[i];
39-
37+
for(std::vector<std::string> rule : rules) {
4038
if (rule.size() < char_count)
4139
throw IllegalArgumentException("grouping policy elements do not meet role definition");
40+
4241
if (rule.size() > char_count)
4342
rule = std::vector<std::string>(rule.begin(), rule.begin() + char_count);
4443

4544
std::vector<std::string> domain(rule.begin() + 2, rule.end());
4645

4746
switch(op) {
48-
case policy_op :: policy_add:
47+
case policy_op::policy_add:
4948
this->rm->AddLink(rule[0], rule[1], domain);
5049
break;
51-
case policy_op :: policy_remove:
50+
case policy_op::policy_remove:
5251
this->rm->DeleteLink(rule[0], rule[1], domain);
5352
}
5453
}
5554
}
5655

57-
void Assertion :: BuildRoleLinks(std::shared_ptr<RoleManager> rm) {
56+
void Assertion::BuildRoleLinks(std::shared_ptr<RoleManager> rm) {
5857
this->rm = rm;
59-
int char_count = int(count(this->value.begin(), this->value.end(), '_'));
58+
size_t char_count = count(this->value.begin(), this->value.end(), '_');
6059

6160
if (char_count < 2)
6261
throw IllegalArgumentException("the number of \"_\" in role definition should be at least 2");
6362

64-
for(int i = 0 ; i < this->policy.size() ; i++){
65-
std::vector<std::string> rule = policy[i];
66-
63+
for(std::vector<std::string> rule : policy) {
6764
if (rule.size() < char_count)
6865
throw IllegalArgumentException("grouping policy elements do not meet role definition");
6966
if (rule.size() > char_count)
@@ -77,9 +74,9 @@ void Assertion :: BuildRoleLinks(std::shared_ptr<RoleManager> rm) {
7774
// df_logger.EnableLog(true);
7875

7976
// Logger *logger = &df_logger;
80-
// LogUtil :: SetLogger(*logger);
77+
// LogUtil::SetLogger(*logger);
8178

82-
// LogUtil :: LogPrint("Role links for: " + Key);
79+
// LogUtil::LogPrint("Role links for: " + Key);
8380

8481
// this->rm->PrintRoles();
8582
}

casbin/model/assertion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ 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, 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

4545
void BuildRoleLinks(std::shared_ptr<RoleManager> rm);
4646
};

casbin/model/function.cpp

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,101 +25,103 @@
2525

2626
namespace casbin {
2727

28-
FunctionMap :: FunctionMap(){
28+
FunctionMap::FunctionMap(){
2929
scope = NULL;
3030
}
3131

32-
void FunctionMap :: ProcessFunctions(std::string expression){
33-
for(auto func: func_list){
34-
int index = int(expression.find(func+"("));
32+
void FunctionMap::ProcessFunctions(const std::string& expression){
33+
for(const std::string& func: func_list) {
34+
size_t index = expression.find(func+"(");
3535

3636
if (index != std::string::npos) {
37-
int close_index = int(expression.find(")", index));
38-
int start = index + int((func+"(").length());
37+
size_t close_index = expression.find(")", index);
38+
size_t start = index + func.length() + 1;
3939

4040
std::string function_params = expression.substr(start, close_index - start);
4141
FetchIdentifier(this->scope, func);
4242
std::vector<std::string> params = Split(function_params, ",");
4343

44-
for(int i=0;i<params.size();i++){
45-
int quote_index = int(params[i].find("\""));
44+
for(std::string& param : params) {
45+
size_t quote_index = param.find("\"");
46+
4647
if (quote_index == std::string::npos)
47-
Get(this->scope, Trim(params[i]));
48-
else{
49-
params[i] = params[i].replace(quote_index, 1, "'");
50-
int second_quote_index = int(params[i].find("\"", quote_index+1));
51-
params[i] = params[i].replace(second_quote_index, 1, "'");
52-
Get(this->scope, Trim(params[i]));
48+
Get(this->scope, Trim(param));
49+
50+
else {
51+
param = param.replace(quote_index, 1, "'");
52+
size_t second_quote_index = param.find("\"", quote_index + 1);
53+
param = param.replace(second_quote_index, 1, "'");
54+
Get(this->scope, Trim(param));
5355
}
5456
}
5557
}
5658
}
5759
}
5860

59-
int FunctionMap :: GetRLen(){
61+
int FunctionMap::GetRLen(){
6062
bool found = FetchIdentifier(scope, "rlen");
6163
if(found)
6264
return GetInt(scope);
6365
return -1;
6466
}
6567

66-
bool FunctionMap :: Evaluate(std::string expression){
68+
bool FunctionMap::Evaluate(const std::string& expression){
6769
ProcessFunctions(expression);
6870
return Eval(scope, expression);
6971
}
7072

71-
bool FunctionMap :: GetBooleanResult(){
72-
return bool(duk_get_boolean(scope, -1));
73+
bool FunctionMap::GetBooleanResult() {
74+
return static_cast<bool>(duk_get_boolean(scope, -1));
7375
}
7476

7577
// AddFunction adds an expression function.
76-
void FunctionMap :: AddFunction(std::string func_name, Function f, Index nargs) {
78+
void FunctionMap::AddFunction(const std::string& func_name, Function f, Index nargs) {
7779
func_list.push_back(func_name);
7880
PushFunction(scope, f, func_name, nargs);
7981
}
8082

81-
void FunctionMap :: AddFunctionPropToR(std::string identifier, Function func, Index nargs){
83+
void FunctionMap::AddFunctionPropToR(const std::string& identifier, Function func, Index nargs){
8284
PushFunctionPropToObject(scope, "r", func, identifier, nargs);
8385
}
8486

85-
void FunctionMap :: AddBooleanPropToR(std::string identifier, bool val){
87+
void FunctionMap::AddBooleanPropToR(const std::string& identifier, bool val){
8688
PushBooleanPropToObject(scope, "r", val, identifier);
8789
}
8890

89-
void FunctionMap :: AddTruePropToR(std::string identifier){
91+
void FunctionMap::AddTruePropToR(const std::string& identifier){
9092
PushTruePropToObject(scope, "r", identifier);
9193
}
9294

93-
void FunctionMap :: AddFalsePropToR(std::string identifier){
95+
void FunctionMap::AddFalsePropToR(const std::string& identifier){
9496
PushFalsePropToObject(scope, "r", identifier);
9597
}
9698

97-
void FunctionMap :: AddIntPropToR(std::string identifier, int val){
99+
void FunctionMap::AddIntPropToR(const std::string& identifier, int val){
98100
PushIntPropToObject(scope, "r", val, identifier);
99101
}
100102

101-
void FunctionMap :: AddFloatPropToR(std::string identifier, float val){
103+
void FunctionMap::AddFloatPropToR(const std::string& identifier, float val){
102104
PushFloatPropToObject(scope, "r", val, identifier);
103105
}
104106

105-
void FunctionMap :: AddDoublePropToR(std::string identifier, double val){
107+
void FunctionMap::AddDoublePropToR(const std::string& identifier, double val){
106108
PushDoublePropToObject(scope, "r", val, identifier);
107109
}
108110

109-
void FunctionMap :: AddStringPropToR(std::string identifier, std::string val){
111+
void FunctionMap::AddStringPropToR(const std::string& identifier, const std::string& val){
110112
PushStringPropToObject(scope, "r", val, identifier);
111113
}
112114

113-
void FunctionMap :: AddPointerPropToR(std::string identifier, void* val){
115+
void FunctionMap::AddPointerPropToR(const std::string& identifier, void* val){
114116
PushPointerPropToObject(scope, "r", val, identifier);
115117
}
116118

117-
void FunctionMap :: AddObjectPropToR(std::string identifier){
119+
void FunctionMap::AddObjectPropToR(const std::string& identifier){
118120
PushObjectPropToObject(scope, "r", identifier);
119121
}
120122

121123
// LoadFunctionMap loads an initial function map.
122-
void FunctionMap :: LoadFunctionMap() {
124+
void FunctionMap::LoadFunctionMap() {
123125
AddFunction("keyMatch", KeyMatch, 2);
124126
AddFunction("keyMatch2", KeyMatch2, 2);
125127
AddFunction("keyMatch3", KeyMatch3, 2);

casbin/model/function.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,36 @@ class FunctionMap {
3030

3131
FunctionMap();
3232

33-
void ProcessFunctions(std::string expression);
33+
void ProcessFunctions(const std::string& expression);
3434

3535
int GetRLen();
3636

37-
bool Evaluate(std::string expression);
37+
bool Evaluate(const std::string& expression);
3838

3939
bool GetBooleanResult();
4040

4141
// AddFunction adds an expression function.
42-
void AddFunction(std::string func_name, Function f, Index nargs);
42+
void AddFunction(const std::string& func_name, Function f, Index nargs);
4343

44-
void AddFunctionPropToR(std::string identifier, Function func, Index nargs);
44+
void AddFunctionPropToR(const std::string& identifier, Function func, Index nargs);
4545

46-
void AddBooleanPropToR(std::string identifier, bool val);
46+
void AddBooleanPropToR(const std::string& identifier, bool val);
4747

48-
void AddTruePropToR(std::string identifier);
48+
void AddTruePropToR(const std::string& identifier);
4949

50-
void AddFalsePropToR(std::string identifier);
50+
void AddFalsePropToR(const std::string& identifier);
5151

52-
void AddIntPropToR(std::string identifier, int val);
52+
void AddIntPropToR(const std::string& identifier, int val);
5353

54-
void AddFloatPropToR(std::string identifier, float val);
54+
void AddFloatPropToR(const std::string& identifier, float val);
5555

56-
void AddDoublePropToR(std::string identifier, double val);
56+
void AddDoublePropToR(const std::string& identifier, double val);
5757

58-
void AddStringPropToR(std::string identifier, std::string val);
58+
void AddStringPropToR(const std::string& identifier, const std::string& val);
5959

60-
void AddPointerPropToR(std::string identifier, void* val);
60+
void AddPointerPropToR(const std::string& identifier, void* val);
6161

62-
void AddObjectPropToR(std::string identifier);
62+
void AddObjectPropToR(const std::string& identifier);
6363

6464
// LoadFunctionMap loads an initial function map.
6565
void LoadFunctionMap();

0 commit comments

Comments
 (0)