Guest User

Untitled

a guest
Jan 3rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. #include <memory>
  5.  
  6.  
  7. namespace eddsl {
  8.  
  9.  
  10. template<typename T>
  11.     class optional {
  12.         using type_value = T;
  13.     public:
  14.  
  15.         optional() : assigne(nullptr) {
  16.         }
  17.  
  18.         optional(const optional& cpy) : optional(*cpy.assigne) {
  19.         }
  20.  
  21.         optional(optional&& cpy) : optional(std::move(*cpy.assigne)) {
  22.             cpy.assigne = nullptr;
  23.         }
  24.  
  25.         optional(const type_value & object) {
  26.             make_copy_object(object);
  27.         }
  28.  
  29.         optional(type_value&& object) {
  30.             make_move_object(std::move(object));
  31.         }
  32.  
  33.         virtual ~optional() {
  34.             if(assigne) {
  35.                 assigne->~type_value();
  36.             }
  37.         }
  38.  
  39.         optional& operator=(const type_value & object) {
  40.             make_copy_object(object);
  41.             return *this;
  42.         }
  43.  
  44.         optional& operator=(type_value && object) {
  45.             make_move_object(std::move(object));
  46.             return *this;
  47.         }
  48.  
  49.         bool has_value() {
  50.             return assigne != nullptr;
  51.         }
  52.  
  53.         explicit operator bool() {
  54.             return has_value();
  55.         }
  56.  
  57.         type_value* operator->() {
  58.             return assigne;
  59.         }
  60.  
  61.         type_value & value() {
  62.             if(assigne) return (type_value) data;
  63.             else return nullptr;
  64.         }
  65.  
  66.         template<typename U>
  67.         type_value & value_or(U&& val) {
  68.             if(assigne) {
  69.                 return *assigne;
  70.             }
  71.             else return val;
  72.         }
  73.  
  74.     private:
  75.         inline void delete_current_object() {
  76.             if(assigne) {
  77.                 assigne->~type_value();
  78.             }
  79.         }
  80.         inline void make_copy_object(const type_value& object) {
  81.             delete_current_object();
  82.             this->assigne = reinterpret_cast<type_value*>(&data);
  83.             new (this->assigne) type_value (object);
  84.         }
  85.  
  86.         inline void make_move_object(type_value&& object) {
  87.             delete_current_object();
  88.             this->assigne = reinterpret_cast<type_value*>(&data);
  89.             new (this->assigne) type_value (std::move(object));
  90.         }
  91.  
  92.         type_value * assigne = nullptr;
  93.         uint8_t data[sizeof(T)];
  94.     };
  95. } //end namespace eddie
  96.  
  97.  
  98. int i = 0;
  99.  
  100. class tete {
  101. public:
  102.     tete(int x) : x(x) {
  103.         p = i++;
  104.         std::cout << "BUILD " << p << std::endl;
  105.  
  106.     }
  107.  
  108.     tete(tete&& t) {
  109.         this->x = t.x;
  110.         t.x = -1;
  111.         p = i++;
  112.     }
  113.     virtual ~tete() {
  114.         std::cout << "Releasing" << p  << " :: " << x << std::endl;
  115.     }
  116.  
  117.     tete& operator=(const tete&) = default;
  118.     int x;
  119.     int p;
  120. };
  121.  
  122.  
  123. int main() {
  124.  
  125.     tete(12);
  126.  
  127.     {
  128.         eddsl::optional<tete> data;
  129.         data = tete {5};
  130.  
  131.         eddsl::optional<tete> data2(std::move(data));
  132.  
  133.         if (data) {
  134.             std::cout << "OK" << std::endl;
  135.         } else {
  136.             std::cout << "NOK" << std::endl;
  137.         }
  138.        // auto res = data.value_or(std::move(tete(2)));
  139.        // std::cout << res.x << std::endl;
  140.         std::cout << "Near out of scope" << std::endl;
  141.     }
  142.     std::cout << "ENDING" << std::endl;
  143.     return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment