Advertisement
kalabukdima

Seminar CRTP for code deduplication

Nov 17th, 2020 (edited)
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. template <class Derived>
  6. struct Comparable {
  7.   const Derived& Myself() const {
  8.     return static_cast<const Derived&>(*this);
  9.   }
  10.  
  11.   inline bool operator>(const Derived& rhs) {
  12.     return rhs < Myself();
  13.   }
  14.   inline bool operator>=(const Derived& rhs) {
  15.     return !(Myself() < rhs);
  16.   }
  17.   inline bool operator<=(const Derived& rhs) {
  18.     return !(rhs < Myself());
  19.   }
  20.   inline bool operator==(const Derived& rhs) {
  21.     return !(Myself() < rhs) && !(rhs < Myself());
  22.   }
  23.   inline bool operator!=(const Derived& rhs) {
  24.     return !(Myself() == rhs);
  25.   }
  26. };
  27.  
  28. template <class Derived>
  29. struct Iterable {
  30.   const Derived& Myself() const {
  31.     return static_cast<const Derived&>(*this);
  32.   }
  33.  
  34.   Derived& Myself() {
  35.     return static_cast<Derived&>(*this);
  36.   }
  37.  
  38.   size_t Size() const {
  39.     return Myself().End() - Myself().Begin();
  40.   }
  41.  
  42.   auto& operator[](size_t index) {
  43.     return *(Myself().Begin() + index);
  44.   }
  45.  
  46.   const auto& operator[](size_t index) const {
  47.     return *(Myself().Begin() + index);
  48.   }
  49.  
  50.   auto& Front() {
  51.     return *(Myself().Begin());
  52.   }
  53.  
  54.   const auto& Front() const {
  55.     return *(Myself().Begin());
  56.   }
  57.  
  58.   auto& Back() {
  59.     return *(Myself().End() - 1);
  60.   }
  61.  
  62.   const auto& Back() const {
  63.     return *(Myself().End() - 1);
  64.   }
  65. };
  66.  
  67. struct String : public Comparable<String>, public Iterable<String> {
  68.   bool operator<(const String& rhs) const {
  69.     std::cout << "Called operator<\n";
  70.     return value < rhs.value;
  71.   }
  72.  
  73.   bool operator==(const String& rhs) const {
  74.     std::cout << "Called operator==\n";
  75.     return value == rhs.value;
  76.   }
  77.  
  78.   auto Begin() {
  79.     return value.begin();
  80.   }
  81.   auto Begin() const {
  82.     return value.begin();
  83.   }
  84.   auto End() {
  85.     return value.end();
  86.   }
  87.   auto End() const {
  88.     return value.end();
  89.   }
  90.  
  91.   std::string value;
  92. };
  93.  
  94. int main() {
  95.   String a;
  96.   a.value.push_back('A');
  97.   a.value.push_back('B');
  98.   a.value.push_back('C');
  99.   std::cout << a.Size() << "\n";
  100.   a[0] = 'D';
  101.   std::cout << a.value << "\n";
  102.   std::cout << a[0] << "\n";
  103.  
  104.   std::cout << a.Front() << " " << a.Back() << "\n";
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement