Commit 57f635e9 authored by Victor Costan's avatar Victor Costan Committed by Commit Bot

base: Sprinkle constexpr/noexcept on Token methods.

Change-Id: I94dbcfd3e5114b23433900397ec023c5d7513545
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2281551
Commit-Queue: Victor Costan <pwnall@chromium.org>
Reviewed-by: default avatardanakj <danakj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#786152}
parent ed0d2587
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
#include <stdint.h> #include <stdint.h>
#include <iosfwd> #include <string>
#include <tuple> #include <tuple>
#include "base/base_export.h" #include "base/base_export.h"
...@@ -25,28 +25,35 @@ namespace base { ...@@ -25,28 +25,35 @@ namespace base {
class BASE_EXPORT Token { class BASE_EXPORT Token {
public: public:
// Constructs a zero Token. // Constructs a zero Token.
constexpr Token() : high_(0), low_(0) {} constexpr Token() = default;
// Constructs a Token with |high| and |low| as its contents. // Constructs a Token with |high| and |low| as its contents.
constexpr Token(uint64_t high, uint64_t low) : high_(high), low_(low) {} constexpr Token(uint64_t high, uint64_t low) : high_(high), low_(low) {}
constexpr Token(const Token&) = default;
constexpr Token& operator=(const Token&) = default;
constexpr Token(Token&&) noexcept = default;
constexpr Token& operator=(Token&&) = default;
// Constructs a new Token with random |high| and |low| values taken from a // Constructs a new Token with random |high| and |low| values taken from a
// cryptographically strong random source. // cryptographically strong random source.
static Token CreateRandom(); static Token CreateRandom();
// The high and low 64 bits of this Token. // The high and low 64 bits of this Token.
uint64_t high() const { return high_; } constexpr uint64_t high() const { return high_; }
uint64_t low() const { return low_; } constexpr uint64_t low() const { return low_; }
bool is_zero() const { return high_ == 0 && low_ == 0; } constexpr bool is_zero() const { return high_ == 0 && low_ == 0; }
bool operator==(const Token& other) const { constexpr bool operator==(const Token& other) const {
return high_ == other.high_ && low_ == other.low_; return high_ == other.high_ && low_ == other.low_;
} }
bool operator!=(const Token& other) const { return !(*this == other); } constexpr bool operator!=(const Token& other) const {
return !(*this == other);
}
bool operator<(const Token& other) const { constexpr bool operator<(const Token& other) const {
return std::tie(high_, low_) < std::tie(other.high_, other.low_); return std::tie(high_, low_) < std::tie(other.high_, other.low_);
} }
...@@ -57,8 +64,8 @@ class BASE_EXPORT Token { ...@@ -57,8 +64,8 @@ class BASE_EXPORT Token {
// Note: Two uint64_t are used instead of uint8_t[16] in order to have a // Note: Two uint64_t are used instead of uint8_t[16] in order to have a
// simpler implementation, paricularly for |ToString()|, |is_zero()|, and // simpler implementation, paricularly for |ToString()|, |is_zero()|, and
// constexpr value construction. // constexpr value construction.
uint64_t high_; uint64_t high_ = 0;
uint64_t low_; uint64_t low_ = 0;
}; };
// For use in std::unordered_map. // For use in std::unordered_map.
......
...@@ -66,6 +66,11 @@ class BASE_EXPORT UnguessableToken { ...@@ -66,6 +66,11 @@ class BASE_EXPORT UnguessableToken {
// Assign to it with Create() before using it. // Assign to it with Create() before using it.
constexpr UnguessableToken() = default; constexpr UnguessableToken() = default;
constexpr UnguessableToken(const UnguessableToken&) = default;
constexpr UnguessableToken& operator=(const UnguessableToken&) = default;
constexpr UnguessableToken(UnguessableToken&&) noexcept = default;
constexpr UnguessableToken& operator=(UnguessableToken&&) = default;
// NOTE: Serializing an empty UnguessableToken is an illegal operation. // NOTE: Serializing an empty UnguessableToken is an illegal operation.
uint64_t GetHighForSerialization() const { uint64_t GetHighForSerialization() const {
DCHECK(!is_empty()); DCHECK(!is_empty());
...@@ -78,22 +83,22 @@ class BASE_EXPORT UnguessableToken { ...@@ -78,22 +83,22 @@ class BASE_EXPORT UnguessableToken {
return token_.low(); return token_.low();
} }
bool is_empty() const { return token_.is_zero(); } constexpr bool is_empty() const { return token_.is_zero(); }
// Hex representation of the unguessable token. // Hex representation of the unguessable token.
std::string ToString() const { return token_.ToString(); } std::string ToString() const { return token_.ToString(); }
explicit operator bool() const { return !is_empty(); } explicit constexpr operator bool() const { return !is_empty(); }
bool operator<(const UnguessableToken& other) const { constexpr bool operator<(const UnguessableToken& other) const {
return token_ < other.token_; return token_ < other.token_;
} }
bool operator==(const UnguessableToken& other) const { constexpr bool operator==(const UnguessableToken& other) const {
return token_ == other.token_; return token_ == other.token_;
} }
bool operator!=(const UnguessableToken& other) const { constexpr bool operator!=(const UnguessableToken& other) const {
return !(*this == other); return !(*this == other);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment