Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite permission::has #404

Merged
merged 4 commits into from
May 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions include/dpp/permissions.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,23 @@ class DPP_EXPORT permission {
operator nlohmann::json() const;

/**
* @brief Check if it has a permission flag set. It uses the Bitwise AND operator
* @param p The permission flag from dpp::permissions
* @brief Check for permission flags set. It uses the Bitwise AND operator
* @tparam T one or more uint64_t permission bits
* @param values The permissions (from dpp::permissions) to check for
*
* **Example:**
*
* ```cpp
* bool is_mod = permission.has(dpp::p_kick_members | dpp::p_ban_members);
* bool is_mod = permission.has(dpp::p_kick_members, dpp::p_ban_members);
* // Returns true if the permission bitmask contains p_kick_members and p_ban_members
* ```
*
* @return True if it has the permission
* @return bool True if it has all the given permissions
*/
bool has(uint64_t p) const;
template <typename... T>
braindigitalis marked this conversation as resolved.
Show resolved Hide resolved
bool has(T... values) const {
return (value & (0 | ... | values)) == (0 | ... | values);
}

/**
* @brief Add a permission with the Bitwise OR operation
Expand Down
4 changes: 0 additions & 4 deletions src/dpp/permissions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,4 @@ permission::operator nlohmann::json() const {
return std::to_string(value);
}

bool permission::has(uint64_t p) const {
return (value & p) == p;
}

}
5 changes: 4 additions & 1 deletion src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,14 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
j["value"] = p;
success = dpp::snowflake_not_null(&j, "value") == 5120 && success;
p.set(dpp::p_administrator, dpp::p_ban_members);
success = p.has(dpp::p_administrator) && success;
success = p.has(dpp::p_administrator) && p.has(dpp::p_ban_members) && success;
success = p.has(dpp::p_administrator, dpp::p_ban_members) && success;
success = p.has(dpp::p_administrator | dpp::p_ban_members) && success;

p.set(dpp::p_administrator);
success = ! p.has(dpp::p_administrator | dpp::p_ban_members) && success; // must return false because they're not both set
success = ! p.has(dpp::p_administrator, dpp::p_ban_members) && success; // must return false because they're not both set
success = ! p.has(dpp::p_administrator | dpp::p_ban_members) && success;
set_test("PERMISSION_CLASS", success);
}

Expand Down