Skip to content

Commit e8f4e17

Browse files
authored
fix: Taking in parameters by const reference (#104)
* fix: taking in parameters by const reference Signed-off-by: Yash Pandey (YP) <[email protected]> * fix: fix build error Signed-off-by: Yash Pandey (YP) <[email protected]>
1 parent 542de11 commit e8f4e17

11 files changed

+333
-333
lines changed

casbin/enforcer.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
namespace casbin {
3535

3636
// enforce 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 "".
37-
bool Enforcer :: enforce(std::string matcher, Scope scope) {
37+
bool Enforcer :: enforce(const std::string& matcher, Scope scope) {
3838
// TODO
3939
// defer func() {
4040
// if err := recover(); err != nil {
@@ -179,7 +179,7 @@ Enforcer ::Enforcer() {
179179
* @param model_path the path of the model file.
180180
* @param policyFile the path of the policy file.
181181
*/
182-
Enforcer ::Enforcer(std::string model_path, std::string policy_file)
182+
Enforcer ::Enforcer(const std::string& model_path, const std::string& policy_file)
183183
: Enforcer(model_path, std::shared_ptr<FileAdapter>(new FileAdapter(policy_file))) {
184184
}
185185

@@ -189,7 +189,7 @@ Enforcer ::Enforcer(std::string model_path, std::string policy_file)
189189
* @param model_path the path of the model file.
190190
* @param adapter the adapter.
191191
*/
192-
Enforcer ::Enforcer(std::string model_path, std::shared_ptr<Adapter> adapter)
192+
Enforcer ::Enforcer(const std::string& model_path, std::shared_ptr<Adapter> adapter)
193193
: Enforcer(std::shared_ptr<Model>(new Model(model_path)), adapter) {
194194
this->model_path = model_path;
195195
}
@@ -227,7 +227,7 @@ Enforcer ::Enforcer(std::shared_ptr<Model> m): Enforcer(m, NULL) {
227227
*
228228
* @param model_path the path of the model file.
229229
*/
230-
Enforcer ::Enforcer(std::string model_path): Enforcer(model_path, "") {
230+
Enforcer ::Enforcer(const std::string& model_path): Enforcer(model_path, "") {
231231
}
232232

233233
/**
@@ -237,19 +237,19 @@ Enforcer ::Enforcer(std::string model_path): Enforcer(model_path, "") {
237237
* @param policyFile the path of the policy file.
238238
* @param enableLog whether to enable Casbin's log.
239239
*/
240-
Enforcer :: Enforcer(std::string model_path, std::string policy_file, bool enable_log): Enforcer(model_path, std::shared_ptr<FileAdapter>(new FileAdapter(policy_file))) {
240+
Enforcer :: Enforcer(const std::string& model_path, const std::string& policy_file, bool enable_log): Enforcer(model_path, std::shared_ptr<FileAdapter>(new FileAdapter(policy_file))) {
241241
// e.EnableLog(enable_log);
242242
}
243243

244244

245245
// InitWithFile initializes an enforcer with a model file and a policy file.
246-
void Enforcer :: InitWithFile(std::string model_path, std::string policy_path) {
246+
void Enforcer :: InitWithFile(const std::string& model_path, const std::string& policy_path) {
247247
std::shared_ptr<Adapter> a = std::shared_ptr<FileAdapter>(new FileAdapter(policy_path));
248248
this->InitWithAdapter(model_path, a);
249249
}
250250

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

255255
this->InitWithModelAndAdapter(m, adapter);
@@ -432,7 +432,7 @@ void Enforcer :: BuildRoleLinks() {
432432
}
433433

434434
// BuildIncrementalRoleLinks provides incremental build the role inheritance relations.
435-
void Enforcer :: BuildIncrementalRoleLinks(policy_op op, std::string p_type, std::vector<std::vector<std::string>> rules) {
435+
void Enforcer :: BuildIncrementalRoleLinks(policy_op op, const std::string& p_type, const std::vector<std::vector<std::string>>& rules) {
436436
return this->model->BuildIncrementalRoleLinks(this->rm, op, "g", p_type, rules);
437437
}
438438

@@ -442,22 +442,22 @@ bool Enforcer :: Enforce(Scope scope) {
442442
}
443443

444444
// Enforce with a vector param,decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
445-
bool Enforcer::Enforce(std::vector<std::string> params) {
445+
bool Enforcer::Enforce(const std::vector<std::string>& params) {
446446
return this->EnforceWithMatcher("", params);
447447
}
448448

449449
// Enforce with a map param,decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
450-
bool Enforcer::Enforce(std::unordered_map<std::string, std::string> params) {
450+
bool Enforcer::Enforce(const std::unordered_map<std::string, std::string>& params) {
451451
return this->EnforceWithMatcher("", params);
452452
}
453453

454454
// 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 "".
455-
bool Enforcer :: EnforceWithMatcher(std::string matcher, Scope scope) {
455+
bool Enforcer :: EnforceWithMatcher(const std::string& matcher, Scope scope) {
456456
return this->enforce(matcher, scope);
457457
}
458458

459459
// 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 "".
460-
bool Enforcer::EnforceWithMatcher(std::string matcher, std::vector<std::string> params) {
460+
bool Enforcer::EnforceWithMatcher(const std::string& matcher, const std::vector<std::string>& params) {
461461
std::vector<std::string> r_tokens = this->model->m["r"].assertion_map["r"]->tokens;
462462

463463
int r_cnt = int(r_tokens.size());
@@ -479,7 +479,7 @@ bool Enforcer::EnforceWithMatcher(std::string matcher, std::vector<std::string>
479479
}
480480

481481
// 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 "".
482-
bool Enforcer::EnforceWithMatcher(std::string matcher, std::unordered_map<std::string, std::string> params) {
482+
bool Enforcer::EnforceWithMatcher(const std::string& matcher, const std::unordered_map<std::string, std::string>& params) {
483483
Scope scope = InitializeScope();
484484
PushObject(scope, "r");
485485

0 commit comments

Comments
 (0)