tc_autoptr.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef __TC_AUTOPTR_H
  17. #define __TC_AUTOPTR_H
  18. #include "util/tc_ex.h"
  19. #include "util/tc_platform.h"
  20. #include <atomic>
  21. #include <typeinfo>
  22. namespace tars
  23. {
  24. /**
  25. * @brief Null Pointer Exception
  26. * @brief 空指针异常
  27. */
  28. struct TC_AutoPtrNull_Exception : public TC_Exception
  29. {
  30. TC_AutoPtrNull_Exception(const string &buffer) : TC_Exception(buffer){};
  31. ~TC_AutoPtrNull_Exception() {};
  32. };
  33. /**
  34. * @brief Smart Pointer Base Class
  35. * @brief 智能指针基类
  36. *
  37. * All classes that require smart pointer support need to inherit from this object.
  38. * 所有需要智能指针支持的类都需要从该对象继承,
  39. *
  40. */
  41. class UTIL_DLL_API TC_HandleBase
  42. {
  43. public:
  44. /**
  45. * @brief Copy
  46. * @brief 复制
  47. *
  48. * @return TC_HandleBase&
  49. */
  50. TC_HandleBase& operator=(const TC_HandleBase&)
  51. {
  52. return *this;
  53. }
  54. /**
  55. * @brief Increase Count
  56. * @brief 增加计数
  57. */
  58. void incRef() { ++_atomic; }
  59. /**
  60. * @brief Decrease Count/减少计数
  61. *
  62. * 当计数==0时, 且需要删除数据时, 释放对象
  63. * When 'count==0' and you need to delete data, you can use this to release object.
  64. *
  65. */
  66. void decRef()
  67. {
  68. if((--_atomic) == 0 && !_bNoDelete)
  69. {
  70. _bNoDelete = true;
  71. delete this;
  72. }
  73. }
  74. /**
  75. * @brief Get Count
  76. * @brief 获取计数.
  77. *
  78. * @return int value of count
  79. * @return int 计数值
  80. */
  81. int getRef() const { return _atomic; }
  82. /**
  83. * @brief Set Automatically-Release Off
  84. * @brief 设置不自动释放.
  85. *
  86. * @param b Determine whether to be deleted automatically or not, true or false.
  87. * @param b 是否自动删除,true or false
  88. */
  89. void setNoDelete(bool b) { _bNoDelete = b; }
  90. protected:
  91. /**
  92. * @brief Constructor
  93. * @brief 构造函数
  94. */
  95. TC_HandleBase() : _atomic(0), _bNoDelete(false)
  96. {
  97. }
  98. /**
  99. * @brief Copy Constructor
  100. * @brief 拷贝构造
  101. */
  102. TC_HandleBase(const TC_HandleBase&) : _atomic(0), _bNoDelete(false)
  103. {
  104. }
  105. /**
  106. * @brief Destructor
  107. * @brief 析构
  108. */
  109. virtual ~TC_HandleBase()
  110. {
  111. }
  112. protected:
  113. /**
  114. * Count
  115. * 计数
  116. */
  117. std::atomic<int> _atomic;
  118. /**
  119. * Determine whether to be deleted automatically or not
  120. * 是否自动删除
  121. */
  122. bool _bNoDelete;
  123. };
  124. /**
  125. * @brief Smart Pointer Template Class
  126. * @brief 智能指针模板类.
  127. *
  128. * This template class an product thread-safe smart pointer which can be placed in a container.
  129. * The smart pointer which is defined by this class can be implemented by reference counting.
  130. * The pointer can be passed in a container.
  131. *
  132. * template<typename T> T MUST BE inherited from TC_HandleBase
  133. *
  134. * 可以放在容器中,且线程安全的智能指针.
  135. * 通过它定义智能指针,该智能指针通过引用计数实现,
  136. * 可以放在容器中传递.
  137. *
  138. * template<typename T> T必须继承于TC_HandleBase
  139. */
  140. template<typename T>
  141. class TC_AutoPtr
  142. {
  143. public:
  144. /**
  145. * Element Type
  146. * 元素类型
  147. */
  148. typedef T element_type;
  149. /**
  150. * @brief Initialize with native pointer, count +1
  151. * @brief 用原生指针初始化, 计数+1.
  152. *
  153. * @param p
  154. */
  155. TC_AutoPtr(T* p = nullptr)
  156. {
  157. _ptr = p;
  158. if(_ptr)
  159. {
  160. _ptr->incRef();
  161. }
  162. }
  163. /**
  164. * @brief Initialize with the native pointer of other smart pointer r, count +1.
  165. * @brief 用其他智能指针r的原生指针初始化, 计数+1.
  166. *
  167. * @param Y
  168. * @param r
  169. */
  170. template<typename Y>
  171. TC_AutoPtr(const TC_AutoPtr<Y>& r)
  172. {
  173. _ptr = r._ptr;
  174. if(_ptr)
  175. {
  176. _ptr->incRef();
  177. }
  178. }
  179. /**
  180. * @brief Copy constructor, count +1
  181. * @brief 拷贝构造, 计数+1.
  182. *
  183. * @param r
  184. */
  185. TC_AutoPtr(const TC_AutoPtr& r)
  186. {
  187. _ptr = r._ptr;
  188. if(_ptr)
  189. {
  190. _ptr->incRef();
  191. }
  192. }
  193. /**
  194. * @brief Destructor
  195. * @brief 析构
  196. */
  197. ~TC_AutoPtr()
  198. {
  199. if(_ptr)
  200. {
  201. _ptr->decRef();
  202. }
  203. }
  204. /**
  205. * @brief Assignment, normal pointer
  206. * @brief 赋值, 普通指针.
  207. *
  208. * @param p
  209. * @return TC_AutoPtr&
  210. */
  211. TC_AutoPtr& operator=(T* p)
  212. {
  213. if(_ptr != p)
  214. {
  215. if(p)
  216. {
  217. p->incRef();
  218. }
  219. T* ptr = _ptr;
  220. _ptr = p;
  221. if(ptr)
  222. {
  223. ptr->decRef();
  224. }
  225. }
  226. return *this;
  227. }
  228. /**
  229. * @brief Assignment, other type of smart pointer
  230. * @brief 赋值, 其他类型智能指针.
  231. *
  232. * @param Y
  233. * @param r
  234. * @return TC_AutoPtr&
  235. */
  236. template<typename Y>
  237. TC_AutoPtr& operator=(const TC_AutoPtr<Y>& r)
  238. {
  239. if(_ptr != r._ptr)
  240. {
  241. if(r._ptr)
  242. {
  243. r._ptr->incRef();
  244. }
  245. T* ptr = _ptr;
  246. _ptr = r._ptr;
  247. if(ptr)
  248. {
  249. ptr->decRef();
  250. }
  251. }
  252. return *this;
  253. }
  254. /**
  255. * @brief Assignment, other ruling pointer of this type.
  256. * @brief 赋值, 该类型其他执政指针.
  257. *
  258. * @param r
  259. * @return TC_AutoPtr&
  260. */
  261. TC_AutoPtr& operator=(const TC_AutoPtr& r)
  262. {
  263. if(_ptr != r._ptr)
  264. {
  265. if(r._ptr)
  266. {
  267. r._ptr->incRef();
  268. }
  269. T* ptr = _ptr;
  270. _ptr = r._ptr;
  271. if(ptr)
  272. {
  273. ptr->decRef();
  274. }
  275. }
  276. return *this;
  277. }
  278. /**
  279. * @brief Replace other types of smart pointers with current types of smart pointers
  280. * @brief 将其他类型的智能指针换成当前类型的智能指针.
  281. *
  282. * @param Y
  283. * @param r
  284. * @return TC_AutoPtr
  285. */
  286. template<class Y>
  287. static TC_AutoPtr dynamicCast(const TC_AutoPtr<Y>& r)
  288. {
  289. return TC_AutoPtr(dynamic_cast<T*>(r._ptr));
  290. }
  291. /**
  292. * @brief Convert pointers of other native types into smart pointers of the current type
  293. * @brief 将其他原生类型的指针转换成当前类型的智能指针.
  294. *
  295. * @param Y
  296. * @param p
  297. * @return TC_AutoPtr
  298. */
  299. template<class Y>
  300. static TC_AutoPtr dynamicCast(Y* p)
  301. {
  302. return TC_AutoPtr(dynamic_cast<T*>(p));
  303. }
  304. /**
  305. * @brief Get Native Pointer
  306. * @brief 获取原生指针.
  307. *
  308. * @return T*
  309. */
  310. T* get() const
  311. {
  312. return _ptr;
  313. }
  314. /**
  315. * @brief Transfer
  316. * @brief 调用.
  317. *
  318. * @return T*
  319. */
  320. T* operator->() const
  321. {
  322. if(!_ptr)
  323. {
  324. throwNullHandleException();
  325. }
  326. return _ptr;
  327. }
  328. /**
  329. * @brief Reference
  330. * @brief 引用.
  331. *
  332. * @return T&
  333. */
  334. T& operator*() const
  335. {
  336. if(!_ptr)
  337. {
  338. throwNullHandleException();
  339. }
  340. return *_ptr;
  341. }
  342. /**
  343. * @brief To define whether it is effective or not.
  344. * @brief 是否有效.
  345. *
  346. * @return bool
  347. */
  348. operator bool() const
  349. {
  350. return _ptr ? true : false;
  351. }
  352. /**
  353. * @brief Swap pointer
  354. * @brief 交换指针.
  355. *
  356. * @param other
  357. */
  358. void swap(TC_AutoPtr& other)
  359. {
  360. std::swap(_ptr, other._ptr);
  361. }
  362. protected:
  363. /**
  364. * @brief Throw Exception
  365. * @brief 抛出异常
  366. */
  367. void throwNullHandleException() const;
  368. public:
  369. T* _ptr;
  370. };
  371. /**
  372. * @brief Throw Exception
  373. * @brief 抛出异常.
  374. *
  375. * @param T
  376. * @param file
  377. * @param line
  378. */
  379. template<typename T> inline void
  380. TC_AutoPtr<T>::throwNullHandleException() const
  381. {
  382. throw TC_AutoPtrNull_Exception("autoptr null handle error![" + string(typeid(T).name()) +"]");
  383. }
  384. /**
  385. * @brief Determine '=='.
  386. * @brief ==判断.
  387. *
  388. * @param T
  389. * @param U
  390. * @param lhs
  391. * @param rhs
  392. *
  393. * @return bool
  394. */
  395. template<typename T, typename U>
  396. inline bool operator==(const TC_AutoPtr<T>& lhs, const TC_AutoPtr<U>& rhs)
  397. {
  398. T* l = lhs.get();
  399. U* r = rhs.get();
  400. // Compare pointers directly instead of comparing values of the two pointers.
  401. // 改为直接比较指针,而不是比较值
  402. return (l == r);
  403. }
  404. /**
  405. * @brief Determine '!='.
  406. * @brief 不等于判断.
  407. *
  408. * @param T
  409. * @param U
  410. * @param lhs
  411. * @param rhs
  412. *
  413. * @return bool
  414. */
  415. template<typename T, typename U>
  416. inline bool operator!=(const TC_AutoPtr<T>& lhs, const TC_AutoPtr<U>& rhs)
  417. {
  418. T* l = lhs.get();
  419. U* r = rhs.get();
  420. // Compare pointers directly instead of comparing values of the two pointers.
  421. // 改为直接比较指针,而不是比较值
  422. return (l != r);
  423. }
  424. /**
  425. * @brief Determine '<', can be used in map and other conrainers.
  426. * @brief 小于判断, 用于放在map等容器中.
  427. *
  428. * @param T
  429. * @param U
  430. * @param lhs
  431. * @param rhs
  432. *
  433. * @return bool
  434. */
  435. template<typename T, typename U>
  436. inline bool operator<(const TC_AutoPtr<T>& lhs, const TC_AutoPtr<U>& rhs)
  437. {
  438. T* l = lhs.get();
  439. U* r = rhs.get();
  440. if(l && r)
  441. {
  442. // return *l < *r;
  443. // Compare pointers directly instead of comparing values of the two pointers.
  444. // 改为直接比较指针,而不是比较值
  445. return (l < r);
  446. }
  447. else
  448. {
  449. return !l && r;
  450. }
  451. }
  452. }
  453. #endif