ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

JFrame(用C++11特性重构系列——optional的实现)

2022-05-14 10:32:02  阅读:223  来源: 互联网

标签:11 JFrame const data C++ init other operator Optional


  1 #pragma once
  2 #include <type_traits>
  3 #include <utility>
  4 #include <stdexcept>
  5 
  6 template<typename T>
  7 class Optional
  8 {
  9     using data_t = typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type;
 10 public:
 11     Optional() : has_init_(false) {}
 12 
 13     Optional(const T& v)
 14     {
 15         create(v);
 16     }
 17 
 18     Optional(T&& v) : has_init_(false)
 19     {
 20         create(std::move(v));
 21     }
 22 
 23     ~Optional()
 24     {
 25         destroy();
 26     }
 27 
 28     Optional(const Optional& other) : has_init_(false)
 29     {
 30         if (other.isInit())
 31             assign(other);
 32     }
 33 
 34     Optional(Optional&& other) : has_init_(false)
 35     {
 36         if (other.isInit())
 37         {
 38             assign(std::move(other));
 39             other.destroy();
 40         }
 41     }
 42 
 43     Optional& operator=(Optional &&other)
 44     {
 45         assign(std::move(other));
 46         return *this;
 47     }
 48 
 49     Optional& operator=(const Optional &other)
 50     {
 51         assign(other);
 52         return *this;
 53     }
 54 
 55     template<class... Args>
 56     void emplace(Args&&... args)
 57     {
 58         destroy();
 59         create(std::forward<Args>(args)...);
 60     }
 61 
 62     bool isInit() const { return has_init_; }
 63 
 64     explicit operator bool() const
 65     {
 66         return isInit();
 67     }
 68 
 69     T& operator*()
 70     {
 71         if(isInit())
 72         {
 73             return *((T*) (&data_));
 74         }
 75 
 76         throw std::logic_error{"try to get data in a Optional which is not initialized"};
 77     }
 78 
 79     const T& operator*() const
 80     {
 81         if(isInit())
 82         {
 83             return *((T*) (&data_));
 84         }
 85 
 86         throw std::logic_error{"try to get data in a Optional which is not initialized"};
 87     }
 88 
 89     T* operator->()
 90     {
 91         return &operator*();
 92     }
 93 
 94     const T* operator->() const
 95     {
 96         return &operator*();
 97     }
 98 
 99     bool operator==(const Optional<T>& rhs) const
100     {
101         return (!bool(*this)) != (!rhs) ? false : (!bool(*this) ? true : (*(*this)) == (*rhs));
102     }
103 
104     bool operator<(const Optional<T>& rhs) const
105     {
106         return !rhs ? false : (!bool(*this) ? true : (*(*this) < (*rhs)));
107     }
108 
109     bool operator!=(const Optional<T>& rhs)
110     {
111         return !(*this == (rhs));
112     }
113 private:
114     template<class... Args>
115     void create(Args&&... args)
116     {
117         new (&data_) T(std::forward<Args>
118 
119             (args)...);
120         has_init_ = true;
121     }
122 
123     void destroy()
124     {
125         if (has_init_)
126         {
127             has_init_ = false;
128             ((T*) (&data_))->~T();
129         }
130     }
131 
132     void assign(const Optional& other)
133     {
134         if (other.isInit())
135         {
136             copy(other.data_);
137             has_init_ = true;
138         }
139         else
140         {
141             destroy();
142         }
143     }
144 
145     void assign(Optional&& other)
146     {
147         if (other.isInit())
148         {
149             move(std::move(other.data_));
150             has_init_ = true;
151             other.destroy();
152         }
153         else
154         {
155             destroy();
156         }
157     }
158 
159     void move(data_t&& val)
160     {
161         destroy();
162         new (&data_) T(std::move(*((T*)(&val))));
163     }
164 
165     void copy(const data_t& val)
166     {
167         destroy();
168         new (&data_) T(*((T*) (&val)));
169     }
170 
171 private:
172     bool has_init_;
173     data_t data_;
174 };

使用到的 C++11 技术:

  • 内存对齐

  • 可变模板参数
  • 右值&完美转发

 

标签:11,JFrame,const,data,C++,init,other,operator,Optional
来源: https://www.cnblogs.com/JackZheng/p/16269270.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有