Skip to content

Commit 32bfa27

Browse files
authored
Merge pull request #127 from EmperorYP7/config-cleanup
chore: Config and enforcer cleanup
2 parents 77dccf5 + de809c6 commit 32bfa27

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+221
-432
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,3 +360,4 @@ MigrationBackup/
360360
# CMake work directory
361361
cmake-build/
362362
xcode-build/
363+
cmake-build*/

bindings/python/py_model.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "py_casbin.h"
2222

2323
void bindPyModel(py::module &m) {
24-
py::class_<casbin::Model>(m, "Model")
24+
py::class_<casbin::Model, std::shared_ptr<casbin::Model>>(m, "Model")
2525
.def(py::init<>())
2626
.def(py::init<const std::string &>())
2727

casbin/config/config.cpp

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919
#ifndef CONFIG_CPP
2020
#define CONFIG_CPP
2121

22-
23-
#include <fstream>
24-
#include <sstream>
25-
#include <algorithm>
26-
#include <cstdio>
27-
2822
#include "./config.h"
2923
#include "../exception/io_exception.h"
3024
#include "../exception/illegal_argument_exception.h"
@@ -40,15 +34,15 @@ std::mutex Config::mtx_lock;
4034
/**
4135
* addConfig adds a new section->key:value to the configuration.
4236
*/
43-
bool Config :: AddConfig(std::string section, std::string option, std::string value) {
37+
bool Config::AddConfig(std::string section, const std::string& option, const std::string& value) {
4438
if (!section.compare(""))
4539
section = DEFAULT_SECTION;
4640
bool ok = data[section].find(option) != data[section].end();
4741
data[section][option] = value;
4842
return !ok;
4943
}
5044

51-
void Config :: Parse(std::string f_name) {
45+
void Config::Parse(const std::string& f_name) {
5246
mtx_lock.lock();
5347
std::ifstream infile;
5448
try {
@@ -62,13 +56,13 @@ void Config :: Parse(std::string f_name) {
6256
infile.close();
6357
}
6458

65-
void Config ::ParseBuffer(std::istream * buf) {
59+
void Config::ParseBuffer(std::istream * buf) {
6660
std::string section = "";
6761
int line_num = 0;
6862
std::string line;
6963
while (true) {
7064
line_num++;
71-
if (getline(*buf, line, '\n')){
65+
if (getline(*buf, line, '\n')) {
7266
if (!line.compare(""))
7367
continue;
7468
}
@@ -79,10 +73,10 @@ void Config ::ParseBuffer(std::istream * buf) {
7973
continue;
8074
else if (line.find(DEFAULT_COMMENT_SEM)==0)
8175
continue;
82-
else if (line.find("[")==0 && EndsWith(line, std::string("]")))
76+
else if (line.find("[") == 0 && EndsWith(line, "]"))
8377
section = line.substr(1, line.length() - 2);
8478
else {
85-
std::vector<std::string> option_val = Split(line, std::string("="), 2);
79+
std::vector<std::string> option_val = Split(line, "=", 2);
8680
if (option_val.size() != 2) {
8781
char* error = new char;
8882
sprintf(error, "parse the content error : line %d , %s = ? ", line_num, option_val[0].c_str());
@@ -101,8 +95,8 @@ void Config ::ParseBuffer(std::istream * buf) {
10195
* @param confName the path of the model file.
10296
* @return the constructor of Config.
10397
*/
104-
std::shared_ptr<Config> Config :: NewConfig(std::string conf_name) {
105-
std::shared_ptr<Config> c(new Config);
98+
std::shared_ptr<Config> Config::NewConfig(const std::string& conf_name) {
99+
std::shared_ptr<Config> c = std::make_shared<Config>();
106100
c->Parse(conf_name);
107101
return c;
108102
}
@@ -113,39 +107,39 @@ std::shared_ptr<Config> Config :: NewConfig(std::string conf_name) {
113107
* @param text the model text.
114108
* @return the constructor of Config.
115109
*/
116-
std::shared_ptr<Config> Config :: NewConfigFromText(std::string text) {
117-
std::shared_ptr<Config> c(new Config);
110+
std::shared_ptr<Config> Config::NewConfigFromText(const std::string& text) {
111+
std::shared_ptr<Config> c = std::make_shared<Config>();
118112
std::stringstream stream(text);
119113
c->ParseBuffer(&stream);
120114
return c;
121115
}
122116

123-
bool Config :: GetBool(std::string key) {
117+
bool Config::GetBool(std::string_view key) {
124118
return Get(key).compare("true")==0;
125119
}
126120

127-
int Config :: GetInt(std::string key) {
121+
int Config::GetInt(std::string_view key) {
128122
return atoi(Get(key).c_str());
129123
}
130124

131-
float Config :: GetFloat(std::string key) {
125+
float Config::GetFloat(std::string_view key) {
132126
return float(atof(Get(key).c_str()));
133127
}
134128

135-
std::string Config :: GetString(std::string key) {
129+
std::string Config::GetString(std::string_view key) {
136130
return Get(key);
137131
}
138132

139-
std::vector<std::string> Config :: GetStrings(std::string key) {
133+
std::vector<std::string> Config::GetStrings(std::string_view key) {
140134
std::string v = Get(key);
141-
if (!v.compare("")) {
142-
std::vector<std::string> empty;
143-
return empty;
144-
}
145-
return Split(v,std::string(","));
135+
136+
if (!v.compare(""))
137+
return {};
138+
139+
return Split(v, ",");
146140
}
147141

148-
void Config :: Set(std::string key, std::string value) {
142+
void Config::Set(std::string_view key, const std::string& value) {
149143
mtx_lock.lock();
150144
if (key.length() == 0) {
151145
mtx_lock.unlock();
@@ -155,8 +149,7 @@ void Config :: Set(std::string key, std::string value) {
155149
std::string section = "";
156150
std::string option;
157151

158-
transform(key.begin(), key.end(), key.begin(), ::tolower);
159-
std::vector<std::string> keys = Split(key, std::string("::"));
152+
std::vector<std::string> keys = Split(std::string(key), "::");
160153
if (keys.size() >= 2) {
161154
section = keys[0];
162155
option = keys[1];
@@ -168,19 +161,19 @@ void Config :: Set(std::string key, std::string value) {
168161
mtx_lock.unlock();
169162
}
170163

171-
std::string Config :: Get(std::string key) {
164+
std::string Config::Get(std::string_view key) {
172165
std::string section;
173166
std::string option;
174-
transform(key.begin(), key.end(), key.begin(), ::tolower);
175-
std::vector<std::string> keys = Split(key, std::string("::"));
167+
168+
std::vector<std::string> keys = Split(std::string(key), "::");
176169
if (keys.size() >= 2) {
177170
section = keys[0];
178171
option = keys[1];
179172
} else {
180173
section = DEFAULT_SECTION;
181174
option = keys[0];
182175
}
183-
bool ok = data.find(section)!=data.end() && data[section].find(option) != data[section].end();
176+
bool ok = data.find(section) != data.end() && data[section].find(option) != data[section].end();
184177
if (ok)
185178
return data[section][option];
186179
return "";

casbin/config/config.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ class Config : public ConfigInterface {
3838
/**
3939
* addConfig adds a new section->key:value to the configuration.
4040
*/
41-
bool AddConfig(std::string section, std::string option, std::string value);
41+
bool AddConfig(std::string section, const std::string& option, const std::string& value);
4242

43-
void Parse(std::string f_name);
43+
void Parse(const std::string& f_name);
4444

4545
void ParseBuffer(std::istream* buf);
4646

@@ -52,29 +52,29 @@ class Config : public ConfigInterface {
5252
* @param confName the path of the model file.
5353
* @return the constructor of Config.
5454
*/
55-
static std::shared_ptr<Config> NewConfig(std::string conf_name);
55+
static std::shared_ptr<Config> NewConfig(const std::string& conf_name);
5656

5757
/**
5858
* newConfigFromText create an empty configuration representation from text.
5959
*
6060
* @param text the model text.
6161
* @return the constructor of Config.
6262
*/
63-
static std::shared_ptr<Config> NewConfigFromText(std::string text);
63+
static std::shared_ptr<Config> NewConfigFromText(const std::string& text);
6464

65-
bool GetBool(std::string key);
65+
bool GetBool(std::string_view key);
6666

67-
int GetInt(std::string key);
67+
int GetInt(std::string_view key);
6868

69-
float GetFloat(std::string key);
69+
float GetFloat(std::string_view key);
7070

71-
std::string GetString(std::string key);
71+
std::string GetString(std::string_view key);
7272

73-
std::vector<std::string> GetStrings(std::string key);
73+
std::vector<std::string> GetStrings(std::string_view key);
7474

75-
void Set(std::string key, std::string value);
75+
void Set(std::string_view key, const std::string& value);
7676

77-
std::string Get(std::string key);
77+
std::string Get(std::string_view key);
7878
};
7979

8080
} // namespace casbin

casbin/config/config_interface.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ namespace casbin {
2525
class ConfigInterface {
2626
public:
2727

28-
virtual std::string GetString(std::string key) = 0;
29-
virtual std::vector<std::string> GetStrings(std::string key) = 0;
30-
virtual bool GetBool(std::string key) = 0;
31-
virtual int GetInt(std::string key) = 0;
32-
virtual float GetFloat(std::string key) = 0;
33-
virtual void Set(std::string key, std::string value) = 0;
28+
virtual std::string GetString(std::string_view key) = 0;
29+
virtual std::vector<std::string> GetStrings(std::string_view key) = 0;
30+
virtual bool GetBool(std::string_view key) = 0;
31+
virtual int GetInt(std::string_view key) = 0;
32+
virtual float GetFloat(std::string_view key) = 0;
33+
virtual void Set(std::string_view key, const std::string& value) = 0;
3434

3535
};
3636

casbin/config/pch.h

Lines changed: 0 additions & 29 deletions
This file was deleted.

casbin/duktape/pch.h

Lines changed: 0 additions & 29 deletions
This file was deleted.

casbin/effect/pch.h

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)