Skip to content

Commit

Permalink
default constructor definition to setup PoliciesValues proper way
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyChashchegorov committed Mar 17, 2024
1 parent 0e9ccb0 commit 9ec51f6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
28 changes: 23 additions & 5 deletions casbin/model/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ static bool IsHashsetUsagePossible(const Model& model) {
return expected_matcher == matcher && model.m.find("g") == model.m.end();
}

#include <iostream>

// AddDef adds an assertion to the model.
bool Model::AddDef(const std::string& sec, const std::string& key, const std::string& value) {
if (value == "")
Expand All @@ -114,6 +116,7 @@ bool Model::AddDef(const std::string& sec, const std::string& key, const std::st
std::shared_ptr<Assertion> ast = std::make_shared<Assertion>();
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)
Expand All @@ -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:"<<sec << ":" << key <<":"<<ast->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:"<<sec << ":" << key <<":"<<ast2->policy.is_hash() << ":" << ast2->policy.is_vector() << std::endl;

return true;
}
Expand Down Expand Up @@ -223,18 +228,31 @@ void Model::PrintPolicy() {
// }
}

#include <iostream>

// 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() <<":" <<assertion_ptr->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() <<":" <<p.is_vector() << ":" << p.size() << std::endl;
if (assertion_ptr->policy.size() > 0) {
std::cerr << "DEBUG__:3 " << std::endl;
std::cerr << "DEBUG__:3 " << name << ":" << assertion_ptr->policy.is_hash() <<":" <<assertion_ptr->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();
}
}
}

Expand Down
14 changes: 12 additions & 2 deletions casbin/model/policy_collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include "casbin/model/policy_collection.hpp"

#include <iostream>
PoliciesValues::PoliciesValues()
: opt_base_vector(std::initializer_list<PolicyValues>{}), opt_base_hashset({}) {}

PoliciesValues::PoliciesValues(PoliciesVector&& base_collection)
: opt_base_vector(base_collection), opt_base_hashset({}) {}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
} else {
std::cerr<<"DEBUG_:8_1" << std::endl;
opt_base_hashset->clear();
}
}

void PoliciesValues::erase(const iterator& it) {
Expand Down
4 changes: 3 additions & 1 deletion include/casbin/model/policy_collection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ using PoliciesHashset = std::unordered_set<PolicyValues>;
PoliciesValues(PoliciesVector&& base_collection);
PoliciesValues(PoliciesHashset&& base_collection);
public:
PoliciesValues(const std::initializer_list<PolicyValues>& list={});
PoliciesValues();
PoliciesValues(const std::initializer_list<PolicyValues>& list);
PoliciesValues(size_t capacity);
static PoliciesValues createWithVector(const std::initializer_list<PolicyValues>& list={});
static PoliciesValues createWithHashset(const std::initializer_list<PolicyValues>& list={});

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:
Expand Down

0 comments on commit 9ec51f6

Please sign in to comment.