Files
oemcrypto/util/include/wv_class_utils.h
2024-09-05 07:21:15 +00:00

113 lines
4.6 KiB
C++

// Copyright 2024 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
#ifndef WVCDM_UTIL_CLASS_UTILS_HPP_
#define WVCDM_UTIL_CLASS_UTILS_HPP_
// ==== Disallow Operators ====
#define WVCDM_DISALLOW_COPY(ClassName) \
ClassName(const ClassName&) = delete; \
ClassName& operator=(const ClassName&) = delete
#define WVCDM_DISALLOW_MOVE(ClassName) \
ClassName(ClassName&&) = delete; \
ClassName& operator=(ClassName&&) = delete
#define WVCDM_DISALLOW_COPY_AND_MOVE(ClassName) \
WVCDM_DISALLOW_COPY(ClassName); \
WVCDM_DISALLOW_MOVE(ClassName)
// ==== Default Operators ====
#define WVCDM_DEFAULT_COPY(ClassName) \
ClassName(const ClassName&) = default; \
ClassName& operator=(const ClassName&) = default
#define WVCDM_DEFAULT_MOVE(ClassName) \
ClassName(ClassName&&) = default; \
ClassName& operator=(ClassName&&) = default
#define WVCDM_DEFAULT_COPY_AND_MOVE(ClassName) \
WVCDM_DEFAULT_COPY(ClassName); \
WVCDM_DEFAULT_MOVE(ClassName)
// ==== Constexpr Default Operators ====
#define WVCDM_CONSTEXPR_DEFAULT_COPY(ClassName) \
constexpr ClassName(const ClassName&) = default; \
constexpr ClassName& operator=(const ClassName&) = default
#define WVCDM_CONSTEXPR_DEFAULT_MOVE(ClassName) \
constexpr ClassName(ClassName&&) = default; \
constexpr ClassName& operator=(ClassName&&) = default
#define WVCDM_CONSTEXPR_DEFAULT_COPY_AND_MOVE(ClassName) \
WVCDM_CONSTEXPR_DEFAULT_COPY(ClassName); \
WVCDM_CONSTEXPR_DEFAULT_MOVE(ClassName)
// ==== Comparison Operators ====
// If a class defines the comparison method "bool IsEqualTo() const",
// this macro will define the == and != operators for that class.
#define WVCDM_DEFINE_EQ_OPERATORS(ClassName) \
bool operator==(const ClassName& other) const { return IsEqualTo(other); } \
bool operator!=(const ClassName& other) const { return !IsEqualTo(other); }
// If a class defines the comparison method "int CompareTo() const",
// this macro will define the < <= != == >= > operators for that class.
#define WVCDM_DEFINE_CMP_OPERATORS(ClassName) \
bool operator<(const ClassName& other) const { \
return CompareTo(other) < 0; \
} \
bool operator<=(const ClassName& other) const { \
return CompareTo(other) <= 0; \
} \
bool operator>(const ClassName& other) const { \
return CompareTo(other) > 0; \
} \
bool operator>=(const ClassName& other) const { \
return CompareTo(other) >= 0; \
}
// If a class defines the comparison methods "bool IsEqualTo() const"
// and "int CompareTo() const", this macro will define the < <= != ==,
// >= > operators.
#define WVCDM_DEFINE_EQ_AND_CMP_OPERATORS(ClassName) \
WVCDM_DEFINE_EQ_OPERATORS(ClassName) \
WVCDM_DEFINE_CMP_OPERATORS(ClassName)
// ==== Constexpr Comparison Operators ====
// Same as WVCDM_DEFINE_EQ_OPERATORS, but requires class to define
// "constexpr bool IsEqualTo() const" method.
#define WVCDM_DEFINE_CONSTEXPR_EQ_OPERATORS(ClassName) \
constexpr bool operator==(const ClassName& other) const { \
return IsEqualTo(other); \
} \
constexpr bool operator!=(const ClassName& other) const { \
return !IsEqualTo(other); \
}
// Same as WVCDM_DEFINE_CMP_OPERATORS, but requires class to define
// "constexpr int CompareTo() const" method.
#define WVCDM_DEFINE_CONSTEXPR_CMP_OPERATORS(ClassName) \
constexpr bool operator<(const ClassName& other) const { \
return CompareTo(other) < 0; \
} \
constexpr bool operator<=(const ClassName& other) const { \
return CompareTo(other) <= 0; \
} \
constexpr bool operator>(const ClassName& other) const { \
return CompareTo(other) > 0; \
} \
constexpr bool operator>=(const ClassName& other) const { \
return CompareTo(other) >= 0; \
}
#define WVCDM_DEFINE_CONSTEXPR_EQ_AND_CMP_OPERATORS(ClassName) \
WVCDM_DEFINE_CONSTEXPR_EQ_OPERATORS(ClassName) \
WVCDM_DEFINE_CONSTEXPR_CMP_OPERATORS(ClassName)
#endif // WVCDM_UTIL_CLASS_UTILS_HPP_