#ifndef BASE_TEST_MOVE_ONLY_INT_H_
#define BASE_TEST_MOVE_ONLY_INT_H_
#include <utility>
#include "base/functional/callback.h"
#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
namespace base {
class MoveOnlyInt {
public:
explicit MoveOnlyInt(int data = 1) : data_(data) {}
MoveOnlyInt(MoveOnlyInt&& other) : data_(other.data_) { other.data_ = 0; }
MoveOnlyInt(const MoveOnlyInt&) = delete;
MoveOnlyInt& operator=(const MoveOnlyInt&) = delete;
~MoveOnlyInt();
MoveOnlyInt& operator=(MoveOnlyInt&& other) {
data_ = other.data_;
other.data_ = 0;
return *this;
}
friend bool operator==(const MoveOnlyInt& lhs,
const MoveOnlyInt& rhs) = default;
friend auto operator<=>(const MoveOnlyInt& lhs,
const MoveOnlyInt& rhs) = default;
friend bool operator==(const MoveOnlyInt& lhs, int rhs) {
return lhs.data_ == rhs;
}
friend bool operator==(int lhs, const MoveOnlyInt& rhs) {
return lhs == rhs.data_;
}
friend auto operator<=>(const MoveOnlyInt& lhs, int rhs) {
return lhs.data_ <=> rhs;
}
friend auto operator<=>(int lhs, const MoveOnlyInt& rhs) {
return lhs <=> rhs.data_;
}
int data() const { return data_; }
static auto SetScopedDestructionCallback(
RepeatingCallback<void(int)> callback) {
GetDestructionCallbackStorage() = std::move(callback);
return absl::Cleanup([] { GetDestructionCallbackStorage().Reset(); });
}
private:
static RepeatingCallback<void(int)>& GetDestructionCallbackStorage();
volatile int data_;
};
}
#endif