]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/tracked_int_ptr.hpp
update ceph source to reef 18.2.0
[ceph.git] / 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 ceph_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 TrackedIntPtr& operator=(const TrackedIntPtr &rhs) {
44 TrackedIntPtr o(rhs.ptr);
45 swap(o);
46 return *this;
47 }
48 T &operator*() const {
49 return *ptr;
50 }
51 T *operator->() const {
52 return ptr;
53 }
54 T *get() const { return ptr; }
55
56 operator bool() const {
57 return ptr != NULL;
58 }
59 bool operator<(const TrackedIntPtr &lhs) const {
60 return ptr < lhs.ptr;
61 }
62 bool operator==(const TrackedIntPtr &lhs) const {
63 return ptr == lhs.ptr;
64 }
65
66 void reset() {
67 if (ptr)
68 put_with_id(ptr, id);
69 ptr = nullptr;
70 id = 0;
71 }
72 };
73
74 #endif