KeyWrapper.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #pragma once
  2. template<class T>
  3. struct KeyWrapper
  4. {
  5. typedef T wrapped_type;
  6. typedef KeyWrapper<T> self_type;
  7. enum KeyTypeEnum { KeyType_Normal, KeyType_Empty, KeyType_Deleted };
  8. KeyTypeEnum type;
  9. T key;
  10. KeyWrapper() : type(KeyType_Normal), key(T()) {}
  11. KeyWrapper(KeyTypeEnum type_) : type(type_), key(T()) {}
  12. KeyWrapper(const T& key_) : key(key_), type(KeyType_Normal) {}
  13. KeyWrapper(const self_type& other) : type(other.type), key(other.key) {}
  14. operator const T&() const { return key; }
  15. bool isNormal() const { return (type == KeyType_Normal); }
  16. template<typename KeyComparer>
  17. struct EqualsComparer
  18. {
  19. EqualsComparer(KeyComparer keyComparer) :
  20. m_KeyComparer(keyComparer)
  21. {
  22. }
  23. bool operator()(const KeyWrapper<T>& left, const KeyWrapper<T>& right) const
  24. {
  25. if (left.type != right.type)
  26. return false;
  27. if (!left.isNormal())
  28. return true;
  29. return m_KeyComparer(left.key, right.key);
  30. }
  31. private:
  32. KeyComparer m_KeyComparer;
  33. };
  34. };