Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
IEquatable< Type > Class Template Reference

Allows Type to be hashable, use operator==, and use operator!=. More...

#include <IEquatable.hxx>

+ Collaboration diagram for IEquatable< Type >:

Public Member Functions

 IEquatable ()
 Create an Equatable type. More...
 
constexpr bool operator!= (const IEquatable< Type > &other) const noexcept
 
bool operator== (const IEquatable< Type > &other) const noexcept
 

Protected Member Functions

void onHashChanged (std::size_t hash) noexcept
 Call when the hash has changed. More...
 
void onHashChanged () noexcept
 Call when the hash has changed. More...
 

Private Attributes

std::size_t m_Hash
 

Detailed Description

template<typename Type>
class IEquatable< Type >

Allows Type to be hashable, use operator==, and use operator!=.

First, Type should implement std::size_t Hash() const noexcept. Whenever your data is mutated, including at constructor-time, you must call onHashChanged(). If you are concerned with hash conflicts, then Type should implement bool Equals(const Type& other) const noexcept and operator== will also verify for strong equality. Otherwise, it will only compare hashes. This abstraction is especially efficient for compound types.

Template Parameters
Typethe data type that must be Equatable

Definition at line 28 of file IEquatable.hxx.

Constructor & Destructor Documentation

◆ IEquatable()

template<typename Type >
IEquatable< Type >::IEquatable

Create an Equatable type.

Definition at line 2 of file IEquatable.inl.hxx.

3 {
4  using FunctorType = typename GetMemberType<decltype(&Type::Hash)>::type;
5  static_assert(std::is_same_v<FunctorType, std::size_t() const noexcept>,
6  "You must implement std::size_t Hash() const noexcept and make it accessible to IEquatable");
7 }

Member Function Documentation

◆ onHashChanged() [1/2]

template<typename Type >
void IEquatable< Type >::onHashChanged
protectednoexcept

Call when the hash has changed.

Definition at line 27 of file IEquatable.inl.hxx.

27 { m_Hash = static_cast<Type &>(*this).Hash(); }

◆ onHashChanged() [2/2]

template<typename Type >
void IEquatable< Type >::onHashChanged ( std::size_t  hash)
protectednoexcept

Call when the hash has changed.

Parameters
hashthe new hash

Definition at line 26 of file IEquatable.inl.hxx.

26 { m_Hash = hash; }

◆ operator!=()

template<typename Type >
constexpr bool IEquatable< Type >::operator!= ( const IEquatable< Type > &  other) const
constexprnoexcept
Returns
true if not equal

Definition at line 9 of file IEquatable.inl.hxx.

10 {
11  return m_Hash != other.m_Hash;
12 }

◆ operator==()

template<typename Type >
bool IEquatable< Type >::operator== ( const IEquatable< Type > &  other) const
noexcept
Returns
true if equal

Definition at line 14 of file IEquatable.inl.hxx.

15 {
17  {
18  return m_Hash == other.m_Hash && Type::Equals(other);
19  }
20  else
21  {
22  return m_Hash == other.m_Hash;
23  }
24 }

Member Data Documentation

◆ m_Hash

template<typename Type >
std::size_t IEquatable< Type >::m_Hash
private

Definition at line 32 of file IEquatable.hxx.


The documentation for this class was generated from the following files:
IEquatable::m_Hash
std::size_t m_Hash
Definition: IEquatable.hxx:32
GetMemberType
Definition: Meta.hxx:94
is_strong_equatable_type
Is true if a type is strongly equatable.
Definition: IEquatable.hxx:9