@@ -34,12 +34,12 @@ namespace casbin {
3434 class ConfigInterface {
3535 public:
3636
37- virtual std::string GetString (std::string key) = 0;
38- virtual std::vector<std::string> GetStrings (std::string key) = 0;
39- virtual bool GetBool (std::string key) = 0;
40- virtual int GetInt (std::string key) = 0;
41- virtual float GetFloat (std::string key) = 0;
42- virtual void Set (std::string key, std::string value) = 0;
37+ virtual std::string GetString (std::string_view key) = 0;
38+ virtual std::vector<std::string> GetStrings (std::string_view key) = 0;
39+ virtual bool GetBool (std::string_view key) = 0;
40+ virtual int GetInt (std::string_view key) = 0;
41+ virtual float GetFloat (std::string_view key) = 0;
42+ virtual void Set (std::string_view key, const std::string& value) = 0;
4343
4444 };
4545
@@ -56,9 +56,9 @@ namespace casbin {
5656 /* *
5757 * addConfig adds a new section->key:value to the configuration.
5858 */
59- bool AddConfig (std::string section, std::string option, std::string value);
59+ bool AddConfig (std::string section, const std::string& option, const std::string& value);
6060
61- void Parse (std::string f_name);
61+ void Parse (const std::string& f_name);
6262
6363 void ParseBuffer (std::istream* buf);
6464
@@ -70,29 +70,29 @@ namespace casbin {
7070 * @param confName the path of the model file.
7171 * @return the constructor of Config.
7272 */
73- static std::shared_ptr<Config> NewConfig (std::string conf_name);
73+ static std::shared_ptr<Config> NewConfig (const std::string& conf_name);
7474
7575 /* *
7676 * newConfigFromText create an empty configuration representation from text.
7777 *
7878 * @param text the model text.
7979 * @return the constructor of Config.
8080 */
81- static std::shared_ptr<Config> NewConfigFromText (std::string text);
81+ static std::shared_ptr<Config> NewConfigFromText (const std::string& text);
8282
83- bool GetBool (std::string key);
83+ bool GetBool (std::string_view key);
8484
85- int GetInt (std::string key);
85+ int GetInt (std::string_view key);
8686
87- float GetFloat (std::string key);
87+ float GetFloat (std::string_view key);
8888
89- std::string GetString (std::string key);
89+ std::string GetString (std::string_view key);
9090
91- std::vector<std::string> GetStrings (std::string key);
91+ std::vector<std::string> GetStrings (std::string_view key);
9292
93- void Set (std::string key, std::string value);
93+ void Set (std::string_view key, const std::string& value);
9494
95- std::string Get (std::string key);
95+ std::string Get (std::string_view key);
9696 };
9797
9898
@@ -167,109 +167,109 @@ namespace casbin {
167167
168168 // AssertionMap is the collection of assertions, can be "r", "p", "g", "e", "m".
169169 class AssertionMap {
170- public:
170+ public:
171171
172- std::unordered_map<std::string, std::shared_ptr<Assertion>> assertion_map;
172+ std::unordered_map<std::string, std::shared_ptr<Assertion>> assertion_map;
173173 };
174174
175175 // Model represents the whole access control model.
176176 class Model {
177- private:
177+ private:
178178
179- static std::unordered_map<std::string, std::string> section_name_map;
179+ static std::unordered_map<std::string, std::string> section_name_map;
180180
181- static void LoadSection (Model* model , std::shared_ptr<ConfigInterface> cfg, const std::string& sec);
181+ static void LoadSection (Model* raw_ptr , std::shared_ptr<ConfigInterface> cfg, const std::string& sec);
182182
183- static std::string GetKeySuffix (int i);
183+ static std::string GetKeySuffix (int i);
184184
185- static bool LoadAssertion (Model* model , std::shared_ptr<ConfigInterface> cfg, const std::string& sec, const std::string& key);
185+ static bool LoadAssertion (Model* raw_ptr , std::shared_ptr<ConfigInterface> cfg, const std::string& sec, const std::string& key);
186186
187- public:
187+ public:
188188
189- Model ();
189+ Model ();
190190
191- Model (const std::string& path);
191+ Model (const std::string& path);
192192
193- std::unordered_map<std::string, AssertionMap> m;
193+ std::unordered_map<std::string, AssertionMap> m;
194194
195- // Minimal required sections for a model to be valid
196- static std::vector<std::string> required_sections;
195+ // Minimal required sections for a model to be valid
196+ static std::vector<std::string> required_sections;
197197
198- bool HasSection (const std::string& sec);
198+ bool HasSection (const std::string& sec);
199199
200- // AddDef adds an assertion to the model.
201- bool AddDef (const std::string& sec, const std::string& key, const std::string& value);
200+ // AddDef adds an assertion to the model.
201+ bool AddDef (const std::string& sec, const std::string& key, const std::string& value);
202202
203- // LoadModel loads the model from model CONF file.
204- void LoadModel (const std::string& path);
203+ // LoadModel loads the model from model CONF file.
204+ void LoadModel (const std::string& path);
205205
206- // LoadModelFromText loads the model from the text.
207- void LoadModelFromText (const std::string& text);
206+ // LoadModelFromText loads the model from the text.
207+ void LoadModelFromText (const std::string& text);
208208
209- void LoadModelFromConfig (std::shared_ptr<ConfigInterface> cfg);
209+ void LoadModelFromConfig (std::shared_ptr<ConfigInterface> cfg);
210210
211- // PrintModel prints the model to the log.
212- void PrintModel ();
211+ // PrintModel prints the model to the log.
212+ void PrintModel ();
213213
214- // NewModel creates an empty model.
215- static Model* NewModel ();
214+ // NewModel creates an empty model.
215+ static std::shared_ptr< Model> NewModel ();
216216
217- // NewModel creates a model from a .CONF file.
218- static Model* NewModelFromFile (const std::string& path);
217+ // NewModel creates a model from a .CONF file.
218+ static std::shared_ptr< Model> NewModelFromFile (const std::string& path);
219219
220- // NewModel creates a model from a std::string which contains model text.
221- static Model* NewModelFromString (const std::string& text);
220+ // NewModel creates a model from a std::string which contains model text.
221+ static std::shared_ptr< Model> NewModelFromString (const std::string& text);
222222
223- void BuildIncrementalRoleLinks (std::shared_ptr<RoleManager> rm, policy_op op, const std::string& sec, const std::string& p_type, const std::vector<std::vector<std::string>>& rules);
223+ void BuildIncrementalRoleLinks (std::shared_ptr<RoleManager>& rm, policy_op op, const std::string& sec, const std::string& p_type, const std::vector<std::vector<std::string>>& rules);
224224
225- // BuildRoleLinks initializes the roles in RBAC.
226- void BuildRoleLinks (std::shared_ptr<RoleManager> rm);
225+ // BuildRoleLinks initializes the roles in RBAC.
226+ void BuildRoleLinks (std::shared_ptr<RoleManager>& rm);
227227
228- // PrintPolicy prints the policy to log.
229- void PrintPolicy ();
228+ // PrintPolicy prints the policy to log.
229+ void PrintPolicy ();
230230
231- // ClearPolicy clears all current policy.
232- void ClearPolicy ();
231+ // ClearPolicy clears all current policy.
232+ void ClearPolicy ();
233233
234- // GetPolicy gets all rules in a policy.
235- std::vector<std::vector<std::string>> GetPolicy (const std::string& sec, const std::string& p_type);
234+ // GetPolicy gets all rules in a policy.
235+ std::vector<std::vector<std::string>> GetPolicy (const std::string& sec, const std::string& p_type);
236236
237- // GetFilteredPolicy gets rules based on field filters from a policy.
238- std::vector<std::vector<std::string>> GetFilteredPolicy (const std::string& sec, const std::string& p_type, int field_index, const std::vector<std::string>& field_values);
237+ // GetFilteredPolicy gets rules based on field filters from a policy.
238+ std::vector<std::vector<std::string>> GetFilteredPolicy (const std::string& sec, const std::string& p_type, int field_index, const std::vector<std::string>& field_values);
239239
240- // HasPolicy determines whether a model has the specified policy rule.
241- bool HasPolicy (const std::string& sec, const std::string& p_type, const std::vector<std::string>& rule);
240+ // HasPolicy determines whether a model has the specified policy rule.
241+ bool HasPolicy (const std::string& sec, const std::string& p_type, const std::vector<std::string>& rule);
242242
243- // AddPolicy adds a policy rule to the model.
244- bool AddPolicy (const std::string& sec, const std::string& p_type, const std::vector<std::string>& rule);
243+ // AddPolicy adds a policy rule to the model.
244+ bool AddPolicy (const std::string& sec, const std::string& p_type, const std::vector<std::string>& rule);
245245
246- // AddPolicies adds policy rules to the model.
247- bool AddPolicies (const std::string& sec, const std::string& p_type, const std::vector<std::vector<std::string>>& rules);
246+ // AddPolicies adds policy rules to the model.
247+ bool AddPolicies (const std::string& sec, const std::string& p_type, const std::vector<std::vector<std::string>>& rules);
248248
249- // UpdatePolicy updates a policy rule from the model.
250- bool UpdatePolicy (const std::string& sec, const std::string& p_type, const std::vector<std::string>& oldRule, const std::vector<std::string>& newRule);
249+ // UpdatePolicy updates a policy rule from the model.
250+ bool UpdatePolicy (const std::string& sec, const std::string& p_type, const std::vector<std::string>& oldRule, const std::vector<std::string>& newRule);
251251
252- // UpdatePolicies updates a set of policy rules from the model.
253- bool UpdatePolicies (const std::string& sec, const std::string& p_type, const std::vector<std::vector<std::string>>& oldRules, const std::vector<std::vector<std::string>>& newRules);
252+ // UpdatePolicies updates a set of policy rules from the model.
253+ bool UpdatePolicies (const std::string& sec, const std::string& p_type, const std::vector<std::vector<std::string>>& oldRules, const std::vector<std::vector<std::string>>& newRules);
254254
255- // RemovePolicy removes a policy rule from the model.
256- bool RemovePolicy (const std::string& sec, const std::string& p_type, const std::vector<std::string>& rule);
255+ // RemovePolicy removes a policy rule from the model.
256+ bool RemovePolicy (const std::string& sec, const std::string& p_type, const std::vector<std::string>& rule);
257257
258- // RemovePolicies removes policy rules from the model.
259- bool RemovePolicies (const std::string& sec, const std::string& p_type, const std::vector<std::vector<std::string>>& rules);
258+ // RemovePolicies removes policy rules from the model.
259+ bool RemovePolicies (const std::string& sec, const std::string& p_type, const std::vector<std::vector<std::string>>& rules);
260260
261- // RemoveFilteredPolicy removes policy rules based on field filters from the model.
262- std::pair<bool , std::vector<std::vector<std::string>>> RemoveFilteredPolicy (const std::string& sec, const std::string& p_type, int field_index, const std::vector<std::string>& field_values);
261+ // RemoveFilteredPolicy removes policy rules based on field filters from the model.
262+ std::pair<bool , std::vector<std::vector<std::string>>> RemoveFilteredPolicy (const std::string& sec, const std::string& p_type, int field_index, const std::vector<std::string>& field_values);
263263
264- // GetValuesForFieldInPolicy gets all values for a field for all rules in a policy, duplicated values are removed.
265- std::vector<std::string> GetValuesForFieldInPolicy (const std::string& sec, const std::string& p_type, int field_index);
264+ // GetValuesForFieldInPolicy gets all values for a field for all rules in a policy, duplicated values are removed.
265+ std::vector<std::string> GetValuesForFieldInPolicy (const std::string& sec, const std::string& p_type, int field_index);
266266
267- // GetValuesForFieldInPolicyAllTypes gets all values for a field for all rules in a policy of all p_types, duplicated values are removed.
268- std::vector<std::string> GetValuesForFieldInPolicyAllTypes (const std::string& sec, int field_index);
267+ // GetValuesForFieldInPolicyAllTypes gets all values for a field for all rules in a policy of all p_types, duplicated values are removed.
268+ std::vector<std::string> GetValuesForFieldInPolicyAllTypes (const std::string& sec, int field_index);
269269 };
270270
271271 // LoadPolicyLine loads a text line as a policy rule to model.
272- void LoadPolicyLine (std::string line, Model* model);
272+ void LoadPolicyLine (std::string line, const std::shared_ptr< Model>& model);
273273
274274 /* *
275275 * Adapter is the interface for Casbin adapters.
@@ -285,14 +285,14 @@ namespace casbin {
285285 *
286286 * @param model the model.
287287 */
288- virtual void LoadPolicy (Model* model) = 0;
288+ virtual void LoadPolicy (const std::shared_ptr< Model>& model) = 0;
289289
290290 /* *
291291 * SavePolicy saves all policy rules to the storage.
292292 *
293293 * @param model the model.
294294 */
295- virtual void SavePolicy (Model* model) = 0;
295+ virtual void SavePolicy (const std::shared_ptr< Model>& model) = 0;
296296
297297 /* *
298298 * AddPolicy adds a policy rule to the storage.
@@ -431,12 +431,12 @@ namespace casbin {
431431 FileAdapter (std::string file_path);
432432
433433 // LoadPolicy loads all policy rules from the storage.
434- void LoadPolicy (Model* model);
434+ void LoadPolicy (const std::shared_ptr< Model>& model);
435435
436436 // SavePolicy saves all policy rules to the storage.
437- void SavePolicy (Model* model);
437+ void SavePolicy (const std::shared_ptr< Model>& model);
438438
439- void LoadPolicyFile (Model* model, void (*handler)( std::string, Model*) );
439+ void LoadPolicyFile (const std::shared_ptr< Model>& model, std::function< void ( std::string, const std::shared_ptr< Model>&)> handler );
440440
441441 void SavePolicyFile (std::string text);
442442
@@ -553,21 +553,29 @@ namespace casbin {
553553 // KeyMatch determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
554554 // For example, "/foo/bar" matches "/foo/*"
555555 ReturnType KeyMatch (Scope scope);
556- bool KeyMatch (std::string key1, std::string key2);
557-
556+ bool KeyMatch (const std::string& key1, const std::string& key2);
557+
558558 // KeyMatch2 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
559559 // For example, "/foo/bar" matches "/foo/*", "/resource1" matches "/:resource"
560560 ReturnType KeyMatch2 (Scope scope);
561- bool KeyMatch2 (std::string key1, std::string key2);
562-
561+ bool KeyMatch2 (const std::string& key1, const std::string& key2);
562+
563563 // KeyMatch3 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
564564 // For example, "/foo/bar" matches "/foo/*", "/resource1" matches "/{resource}"
565565 ReturnType KeyMatch3 (Scope scope);
566- bool KeyMatch3 (std::string key1, std::string key2);
567-
566+ bool KeyMatch3 (const std::string& key1, const std::string& key2);
567+
568568 // RegexMatch determines whether key1 matches the pattern of key2 in regular expression.
569569 ReturnType RegexMatch (Scope scope);
570- bool RegexMatch (std::string key1, std::string key2);
570+ bool RegexMatch (const std::string& key1, const std::string& key2);
571+
572+ // IPMatch determines whether IP address ip1 matches the pattern of IP address ip2, ip2 can be an IP address or a CIDR pattern.
573+ // For example, "192.168.2.123" matches "192.168.2.0/24"
574+ ReturnType IPMatch (Scope scope);
575+ bool IPMatch (const std::string& ip1, const std::string& ip2);
576+
577+ // GFunction is the method of the g(_, _) function.
578+ ReturnType GFunction (Scope scope);
571579
572580 // IPMatch determines whether IP address ip1 matches the pattern of IP address ip2, ip2 can be an IP address or a CIDR pattern.
573581 // For example, "192.168.2.123" matches "192.168.2.0/24"
@@ -660,21 +668,21 @@ namespace casbin {
660668 using std::logic_error::logic_error;
661669 };
662670
663- typedef bool (*MatchingFunc)(std::string, std::string);
671+ typedef bool (*MatchingFunc)(const std::string&, const std::string& );
664672
665673 /* *
666- * Role represents the data structure for a role in RBAC.
667- */
674+ * Role represents the data structure for a role in RBAC.
675+ */
668676 class Role {
669-
677+
670678 private:
671679 std::vector<Role*> roles;
672680
673681 public:
674682 std::string name;
675683
676684 static Role* NewRole (std::string name);
677-
685+
678686 void AddRole (Role* role);
679687
680688 void DeleteRole (Role* role);
0 commit comments