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 caca5a6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
23 changes: 18 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,7 +134,7 @@ 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;

return true;
Expand Down Expand Up @@ -223,18 +226,28 @@ 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.size() << std::endl;
if (assertion_ptr->policy.size() > 0) {
std::cerr << "DEBUG__:3 " << 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
7 changes: 7 additions & 0 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
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 caca5a6

Please sign in to comment.