Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / include / alloc_ptr.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2017 Red Hat, Inc.
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation.  See file COPYING.
12  *
13  */
14
15 #ifndef CEPH_ALLOC_PTR_H
16 #define CEPH_ALLOC_PTR_H
17
18 #include <memory>
19
20 template <class T>
21 class alloc_ptr
22 {
23 public:
24     typedef typename std::pointer_traits< std::unique_ptr<T> >::pointer pointer;
25     typedef typename std::pointer_traits< std::unique_ptr<T> >::element_type element_type;
26
27     alloc_ptr() : ptr() {}
28
29     template<class U>
30       alloc_ptr(U&& u) : ptr(std::forward<U>(u)) {}
31
32     alloc_ptr(alloc_ptr<pointer>&& rhs) : ptr(std::move(rhs.ptr)) {}
33     alloc_ptr(const alloc_ptr<pointer>& rhs) = delete;
34     alloc_ptr& operator=(const alloc_ptr<pointer>&& rhs) {
35         ptr = rhs.ptr;
36     }
37     alloc_ptr& operator=(const alloc_ptr<pointer>& rhs) {
38         ptr = rhs.ptr;
39     }
40
41     void swap (alloc_ptr<pointer>& rhs) {
42         ptr.swap(rhs.ptr);
43     }
44     element_type* release() {
45         return ptr.release();
46     }
47     void reset(element_type *p = nullptr) {
48         ptr.reset(p);
49     }
50     element_type* get() const {
51         if (!ptr)
52           ptr.reset(new element_type);
53         return ptr.get();
54     }
55     element_type& operator*() const {
56         if (!ptr)
57           ptr.reset(new element_type);
58         return *ptr;
59     }
60     element_type* operator->() const {
61         if (!ptr)
62           ptr.reset(new element_type);
63         return ptr.get();
64     }
65     operator bool() const {
66         return !!ptr;
67     }
68
69     friend bool operator< (const alloc_ptr& lhs, const alloc_ptr& rhs) {
70         return std::less<element_type>(*lhs, *rhs);
71     }
72     friend bool operator<=(const alloc_ptr& lhs, const alloc_ptr& rhs) {
73         return std::less_equal<element_type>(*lhs, *rhs);
74     }
75     friend bool operator> (const alloc_ptr& lhs, const alloc_ptr& rhs) {
76         return std::greater<element_type>(*lhs, *rhs);
77     }
78     friend bool operator>=(const alloc_ptr& lhs, const alloc_ptr& rhs) {
79         return std::greater_equal<element_type>(*lhs, *rhs);
80     }
81     friend bool operator==(const alloc_ptr& lhs, const alloc_ptr& rhs) {
82         return *lhs == *rhs;
83     }
84     friend bool operator!=(const alloc_ptr& lhs, const alloc_ptr& rhs) {
85         return *lhs != *rhs;
86     }
87 private:
88     mutable std::unique_ptr<element_type> ptr;
89 };
90
91 #endif