From b0d15cc9532959f1bf3e35cd7a70979184521bd0 Mon Sep 17 00:00:00 2001 From: Aleksei Chashchegorov Date: Sun, 17 Mar 2024 14:45:02 +0100 Subject: [PATCH] default constructor definition to setup PoliciesValues proper way --- casbin/model/model.cpp | 28 ++++++++++++++++++---- casbin/model/policy_collection.cpp | 14 +++++++++-- include/casbin/model/policy_collection.hpp | 4 +++- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/casbin/model/model.cpp b/casbin/model/model.cpp index 609d3166..c8326013 100644 --- a/casbin/model/model.cpp +++ b/casbin/model/model.cpp @@ -106,6 +106,8 @@ static bool IsHashsetUsagePossible(const Model& model) { return expected_matcher == matcher && model.m.find("g") == model.m.end(); } +#include + // AddDef adds an assertion to the model. bool Model::AddDef(const std::string& sec, const std::string& key, const std::string& value) { if (value == "") @@ -114,6 +116,7 @@ bool Model::AddDef(const std::string& sec, const std::string& key, const std::st std::shared_ptr ast = std::make_shared(); ast->key = key; ast->value = value; + ast->policy = PoliciesValues::createWithVector(); if (sec == "r" || sec == "p") { ast->tokens = Split(ast->value, ","); for (std::string& token : ast->tokens) @@ -131,8 +134,10 @@ bool Model::AddDef(const std::string& sec, const std::string& key, const std::st } ast->policy = IsHashsetUsagePossible(*this) ? PoliciesValues::createWithHashset() : PoliciesValues::createWithVector(); } - + std::cout <<"DEBUG:CREATE:"<policy.is_hash() << ":" << ast->policy.is_vector() << std::endl; m[sec].assertion_map[key] = ast; + auto& ast2 = m[sec].assertion_map[key]; + std::cout <<"DEBUG:CREATE:"<policy.is_hash() << ":" << ast2->policy.is_vector() << std::endl; return true; } @@ -223,18 +228,31 @@ void Model::PrintPolicy() { // } } +#include + // ClearPolicy clears all current policy. void Model::ClearPolicy() { // Caching "p" assertion map by reference for the scope of this function - for (auto [_, assertion_ptr] : this->m["p"].assertion_map) { - if (assertion_ptr->policy.size() > 0) + for (auto& [name, assertion_ptr] : this->m["p"].assertion_map) { + + std::cerr << "DEBUG__:2 " << name << ":" << assertion_ptr->policy.is_hash() <<":" <policy.is_vector() << ":" << assertion_ptr->policy.size() << std::endl; + auto& p = this->m["p"].assertion_map[name]->policy; + std::cerr << "DEBUG__:2 " << name << ":" << p.is_hash() <<":" <policy.size() > 0) { + std::cerr << "DEBUG__:3 " << std::endl; + std::cerr << "DEBUG__:3 " << name << ":" << assertion_ptr->policy.is_hash() <<":" <policy.is_vector() << ":" << assertion_ptr->policy.size() << std::endl; assertion_ptr->policy.clear(); + } } + std::cerr << "DEBUG__:4 " << std::endl; // Caching "g" assertion map by reference for the scope of this function - for (auto [_, assertion_ptr] : this->m["g"].assertion_map) { - if (assertion_ptr->policy.size() > 0) + for (auto& [name, assertion_ptr] : this->m["g"].assertion_map) { + std::cerr << "DEBUG__:5 " << name << ":" << assertion_ptr->policy.is_hash() << std::endl; + if (assertion_ptr->policy.size() > 0) { + std::cerr << "DEBUG__:6 " << std::endl; assertion_ptr->policy.clear(); + } } } diff --git a/casbin/model/policy_collection.cpp b/casbin/model/policy_collection.cpp index 6f422e83..067522fd 100644 --- a/casbin/model/policy_collection.cpp +++ b/casbin/model/policy_collection.cpp @@ -17,6 +17,9 @@ #include "casbin/model/policy_collection.hpp" #include +PoliciesValues::PoliciesValues() + : opt_base_vector(std::initializer_list{}), opt_base_hashset({}) {} + PoliciesValues::PoliciesValues(PoliciesVector&& base_collection) : opt_base_vector(base_collection), opt_base_hashset({}) {} @@ -62,6 +65,10 @@ bool PoliciesValues::is_hash() const { return opt_base_hashset.has_value(); } +bool PoliciesValues::is_vector() const { + return opt_base_vector.has_value(); +} + void PoliciesValues::emplace(const PolicyValues& element) { if (opt_base_vector.has_value()) { std::cerr<<"DEBUG_:4_1" << std::endl; @@ -121,11 +128,14 @@ PoliciesValues::iterator PoliciesValues::find(const PolicyValues& values) { } void PoliciesValues::clear() { - std::cerr<<"DEBUG_:8" << std::endl; - if (opt_base_vector.has_value()) + std::cerr<<"DEBUG_:8:" << is_hash() <<":" << is_vector() << std::endl; + std::cerr<<"DEBUG_:8:" << opt_base_hashset.has_value() <<":" << opt_base_vector.has_value() << std::endl; + if (opt_base_vector.has_value()) { opt_base_vector->clear(); + } elese { std::cerr<<"DEBUG_:8_1" << std::endl; opt_base_hashset->clear(); + } } void PoliciesValues::erase(const iterator& it) { diff --git a/include/casbin/model/policy_collection.hpp b/include/casbin/model/policy_collection.hpp index 14050a6b..3f545ec6 100644 --- a/include/casbin/model/policy_collection.hpp +++ b/include/casbin/model/policy_collection.hpp @@ -45,7 +45,8 @@ using PoliciesHashset = std::unordered_set; PoliciesValues(PoliciesVector&& base_collection); PoliciesValues(PoliciesHashset&& base_collection); public: - PoliciesValues(const std::initializer_list& list={}); + PoliciesValues(); + PoliciesValues(const std::initializer_list& list); PoliciesValues(size_t capacity); static PoliciesValues createWithVector(const std::initializer_list& list={}); static PoliciesValues createWithHashset(const std::initializer_list& list={}); @@ -53,6 +54,7 @@ using PoliciesHashset = std::unordered_set; size_t size() const; bool empty() const; bool is_hash() const; + bool is_vector() const; void emplace(const PolicyValues& element); class iterator final : std::input_iterator_tag { private: