Skip to content

Commit 737710b

Browse files
authored
Merge pull request #95 from PDLdeLange/improve-portability
feat: Improve portability
2 parents 61143d9 + 3d242dc commit 737710b

File tree

123 files changed

+1915
-1567
lines changed

Some content is hidden

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

123 files changed

+1915
-1567
lines changed

casbin/config/config.cpp

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,30 @@
3030
#include "../exception/illegal_argument_exception.h"
3131
#include "../util/util.h"
3232

33-
const string Config::DEFAULT_SECTION = "default";
34-
const string Config::DEFAULT_COMMENT = "#";
35-
const string Config::DEFAULT_COMMENT_SEM = ";";
36-
mutex Config::mtx_lock;
33+
namespace casbin {
34+
35+
const std::string Config::DEFAULT_SECTION = "default";
36+
const std::string Config::DEFAULT_COMMENT = "#";
37+
const std::string Config::DEFAULT_COMMENT_SEM = ";";
38+
std::mutex Config::mtx_lock;
3739

3840
/**
3941
* addConfig adds a new section->key:value to the configuration.
4042
*/
41-
bool Config :: AddConfig(string section, string option, string value) {
43+
bool Config :: AddConfig(std::string section, std::string option, std::string value) {
4244
if (!section.compare(""))
4345
section = DEFAULT_SECTION;
4446
bool ok = data[section].find(option) != data[section].end();
4547
data[section][option] = value;
4648
return !ok;
4749
}
4850

49-
void Config :: Parse(string f_name) {
51+
void Config :: Parse(std::string f_name) {
5052
mtx_lock.lock();
51-
ifstream infile;
53+
std::ifstream infile;
5254
try {
5355
infile.open(f_name);
54-
} catch (const ifstream::failure e) {
56+
} catch (const std::ifstream::failure e) {
5557
mtx_lock.unlock();
5658
throw IOException("Cannot open file.");
5759
}
@@ -60,10 +62,10 @@ void Config :: Parse(string f_name) {
6062
infile.close();
6163
}
6264

63-
void Config :: ParseBuffer(istream* buf){
64-
string section = "";
65+
void Config ::ParseBuffer(std::istream * buf) {
66+
std::string section = "";
6567
int line_num = 0;
66-
string line;
68+
std::string line;
6769
while (true) {
6870
line_num++;
6971
if (getline(*buf, line, '\n')){
@@ -77,17 +79,17 @@ void Config :: ParseBuffer(istream* buf){
7779
continue;
7880
else if (line.find(DEFAULT_COMMENT_SEM)==0)
7981
continue;
80-
else if (line.find("[")==0 && EndsWith(line, string("]")))
82+
else if (line.find("[")==0 && EndsWith(line, std::string("]")))
8183
section = line.substr(1, line.length() - 2);
8284
else {
83-
vector<string> option_val = Split(line, string("="), 2);
85+
std::vector<std::string> option_val = Split(line, std::string("="), 2);
8486
if (option_val.size() != 2) {
8587
char* error = new char;
8688
sprintf(error, "parse the content error : line %d , %s = ? ", line_num, option_val[0].c_str());
87-
throw IllegalArgumentException(string(error));
89+
throw IllegalArgumentException(std::string(error));
8890
}
89-
string option = Trim(option_val[0]);
90-
string value = Trim(option_val[1]);
91+
std::string option = Trim(option_val[0]);
92+
std::string value = Trim(option_val[1]);
9193
AddConfig(section, option, value);
9294
}
9395
}
@@ -99,8 +101,8 @@ void Config :: ParseBuffer(istream* buf){
99101
* @param confName the path of the model file.
100102
* @return the constructor of Config.
101103
*/
102-
shared_ptr<Config> Config :: NewConfig(string conf_name) {
103-
shared_ptr<Config> c(new Config);
104+
std::shared_ptr<Config> Config :: NewConfig(std::string conf_name) {
105+
std::shared_ptr<Config> c(new Config);
104106
c->Parse(conf_name);
105107
return c;
106108
}
@@ -111,50 +113,50 @@ shared_ptr<Config> Config :: NewConfig(string conf_name) {
111113
* @param text the model text.
112114
* @return the constructor of Config.
113115
*/
114-
shared_ptr<Config> Config :: NewConfigFromText(string text) {
115-
shared_ptr<Config> c(new Config);
116-
stringstream stream(text);
116+
std::shared_ptr<Config> Config :: NewConfigFromText(std::string text) {
117+
std::shared_ptr<Config> c(new Config);
118+
std::stringstream stream(text);
117119
c->ParseBuffer(&stream);
118120
return c;
119121
}
120122

121-
bool Config :: GetBool(string key) {
123+
bool Config :: GetBool(std::string key) {
122124
return Get(key).compare("true")==0;
123125
}
124126

125-
int Config :: GetInt(string key) {
127+
int Config :: GetInt(std::string key) {
126128
return atoi(Get(key).c_str());
127129
}
128130

129-
float Config :: GetFloat(string key) {
131+
float Config :: GetFloat(std::string key) {
130132
return float(atof(Get(key).c_str()));
131133
}
132134

133-
string Config :: GetString(string key) {
135+
std::string Config :: GetString(std::string key) {
134136
return Get(key);
135137
}
136138

137-
vector<string> Config :: GetStrings(string key) {
138-
string v = Get(key);
139+
std::vector<std::string> Config :: GetStrings(std::string key) {
140+
std::string v = Get(key);
139141
if (!v.compare("")) {
140-
vector<string> empty;
142+
std::vector<std::string> empty;
141143
return empty;
142144
}
143-
return Split(v,string(","));
145+
return Split(v,std::string(","));
144146
}
145147

146-
void Config :: Set(string key, string value) {
148+
void Config :: Set(std::string key, std::string value) {
147149
mtx_lock.lock();
148150
if (key.length() == 0) {
149151
mtx_lock.unlock();
150152
throw IllegalArgumentException("key is empty");
151153
}
152154

153-
string section = "";
154-
string option;
155+
std::string section = "";
156+
std::string option;
155157

156158
transform(key.begin(), key.end(), key.begin(), ::tolower);
157-
vector<string> keys = Split(key, string("::"));
159+
std::vector<std::string> keys = Split(key, std::string("::"));
158160
if (keys.size() >= 2) {
159161
section = keys[0];
160162
option = keys[1];
@@ -166,11 +168,11 @@ void Config :: Set(string key, string value) {
166168
mtx_lock.unlock();
167169
}
168170

169-
string Config :: Get(string key) {
170-
string section;
171-
string option;
171+
std::string Config :: Get(std::string key) {
172+
std::string section;
173+
std::string option;
172174
transform(key.begin(), key.end(), key.begin(), ::tolower);
173-
vector<string> keys = Split(key, string("::"));
175+
std::vector<std::string> keys = Split(key, std::string("::"));
174176
if (keys.size() >= 2) {
175177
section = keys[0];
176178
option = keys[1];
@@ -184,4 +186,6 @@ string Config :: Get(string key) {
184186
return "";
185187
}
186188

189+
} // namespace casbin
190+
187191
#endif // CONFIG_CPP

casbin/config/config.h

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,26 @@
2323

2424
#include "./config_interface.h"
2525

26-
using namespace std;
26+
namespace casbin {
2727

2828
class Config : public ConfigInterface {
2929
private:
3030

31-
static const string DEFAULT_SECTION;
32-
static const string DEFAULT_COMMENT;
33-
static const string DEFAULT_COMMENT_SEM;
34-
static mutex mtx_lock;
31+
static const std::string DEFAULT_SECTION;
32+
static const std::string DEFAULT_COMMENT;
33+
static const std::string DEFAULT_COMMENT_SEM;
34+
static std::mutex mtx_lock;
3535

36-
unordered_map<string, unordered_map<string, string>> data;
36+
std::unordered_map<std::string, std::unordered_map<std::string, std::string>> data;
3737

3838
/**
3939
* addConfig adds a new section->key:value to the configuration.
4040
*/
41-
bool AddConfig(string section, string option, string value);
41+
bool AddConfig(std::string section, std::string option, std::string value);
4242

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

45-
void ParseBuffer(istream* buf);
45+
void ParseBuffer(std::istream* buf);
4646

4747
public:
4848

@@ -52,29 +52,31 @@ class Config : public ConfigInterface {
5252
* @param confName the path of the model file.
5353
* @return the constructor of Config.
5454
*/
55-
static shared_ptr<Config> NewConfig(string conf_name);
55+
static std::shared_ptr<Config> NewConfig(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 shared_ptr<Config> NewConfigFromText(string text);
63+
static std::shared_ptr<Config> NewConfigFromText(std::string text);
6464

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

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

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

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

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

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

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

80+
} // namespace casbin
81+
8082
#endif

casbin/config/config_interface.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,20 @@
2020
#include <string>
2121
#include <vector>
2222

23-
using namespace std;
23+
namespace casbin {
2424

2525
class ConfigInterface {
2626
public:
2727

28-
virtual string GetString(string key) = 0;
29-
virtual vector <string> GetStrings(string key) = 0;
30-
virtual bool GetBool(string key) = 0;
31-
virtual int GetInt(string key) = 0;
32-
virtual float GetFloat(string key) = 0;
33-
virtual void Set(string key, string value) = 0;
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;
3434

3535
};
3636

37+
} // namespace casbin
38+
3739
#endif

casbin/effect/default_effector.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
#include "./default_effector.h"
2424
#include "../exception/unsupported_operation_exception.h"
2525

26+
namespace casbin {
27+
2628
/**
2729
* MergeEffects merges all matching results collected by the enforcer into a single decision.
2830
*/
29-
bool DefaultEffector :: MergeEffects(string expr, vector<Effect> effects, vector<float> results) {
31+
bool DefaultEffector :: MergeEffects(std::string expr, std::vector<Effect> effects, std::vector<float> results) {
3032
bool result;
3133

3234
if (!expr.compare("some(where (p.eft == allow))")) {
@@ -74,4 +76,6 @@ bool DefaultEffector :: MergeEffects(string expr, vector<Effect> effects, vector
7476
return result;
7577
}
7678

79+
} // namespace casbin
80+
7781
#endif // DEFAULT_EFFECTOR_CPP

casbin/effect/default_effector.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#include "effector.h"
2121

22+
namespace casbin {
23+
2224
/**
2325
* DefaultEffector is default effector for Casbin.
2426
*/
@@ -28,7 +30,9 @@ class DefaultEffector : public Effector{
2830
/**
2931
* MergeEffects merges all matching results collected by the enforcer into a single decision.
3032
*/
31-
bool MergeEffects(string expr, vector<Effect> effects, vector<float> results);
33+
bool MergeEffects(std::string expr, std::vector<Effect> effects, std::vector<float> results);
3234
};
3335

36+
} // namespace casbin
37+
3438
#endif

casbin/effect/effect.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717
#ifndef CASBIN_CPP_EFFECT_EFFECT
1818
#define CASBIN_CPP_EFFECT_EFFECT
1919

20+
namespace casbin {
21+
2022
enum class Effect{
2123
Allow, Indeterminate, Deny
2224
};
2325

2426
typedef enum Effect Effect;
2527

28+
} // namespace casbin
29+
2630
#endif

casbin/effect/effector.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#include "effect.h"
2424

25-
using namespace std;
25+
namespace casbin {
2626

2727
/**
2828
* Effector is the abstract class for Casbin effectors.
@@ -37,7 +37,9 @@ class Effector{
3737
* @param results the matcher results of all matched rules.
3838
* @return the final effect.
3939
*/
40-
virtual bool MergeEffects(string expr, vector<Effect> effects, vector<float> results) = 0;
40+
virtual bool MergeEffects(std::string expr, std::vector<Effect> effects, std::vector<float> results) = 0;
4141
};
4242

43+
} // namespace casbin
44+
4345
#endif

0 commit comments

Comments
 (0)