Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / common / tracked_int_ptr.hpp
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) 2013 Inktank Storage, 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_TRACKEDINTPTR_H
16 #define CEPH_TRACKEDINTPTR_H
17
18
19 template <class T>
20 class TrackedIntPtr {
21   T *ptr;
22   uint64_t id;
23 public:
24   TrackedIntPtr() : ptr(NULL), id(0) {}
25   TrackedIntPtr(T *ptr) : ptr(ptr), id(ptr ? get_with_id(ptr) : 0) {}
26   ~TrackedIntPtr() {
27     if (ptr)
28       put_with_id(ptr, id);
29     else
30       assert(id == 0);
31   }
32   void swap(TrackedIntPtr &other) {
33     T *optr = other.ptr;
34     uint64_t oid = other.id;
35     other.ptr = ptr;
36     other.id = id;
37     ptr = optr;
38     id = oid;
39   }
40   TrackedIntPtr(const TrackedIntPtr &rhs) :
41     ptr(rhs.ptr), id(ptr ? get_with_id(ptr) : 0) {}
42
43   void operator=(const TrackedIntPtr &rhs) {
44     TrackedIntPtr o(rhs.ptr);
45     swap(o);
46   }
47   T &operator*() const {
48     return *ptr;
49   }
50   T *operator->() const {
51     return ptr;
52   }
53   T *get() const { return ptr; }
54
55   operator bool() const {
56     return ptr != NULL;
57   }
58   bool operator<(const TrackedIntPtr &lhs) const {
59     return ptr < lhs.ptr;
60   }
61   bool operator==(const TrackedIntPtr &lhs) const {
62     return ptr == lhs.ptr;
63   }
64
65   void reset() {
66     if (ptr) 
67       put_with_id(ptr, id);
68     ptr = nullptr;
69     id = 0;
70   }
71 };
72
73 #endif