]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/tracked_int_ptr.hpp
bump version to 12.2.12-pve1
[ceph.git] / ceph / src / common / tracked_int_ptr.hpp
CommitLineData
7c673cae
FG
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
7c673cae
FG
18
19template <class T>
20class TrackedIntPtr {
21 T *ptr;
22 uint64_t id;
23public:
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 }
31f18b77 47 T &operator*() const {
7c673cae
FG
48 return *ptr;
49 }
31f18b77 50 T *operator->() const {
7c673cae
FG
51 return ptr;
52 }
31f18b77
FG
53 T *get() const { return ptr; }
54
7c673cae
FG
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 }
31f18b77
FG
64
65 void reset() {
66 if (ptr)
67 put_with_id(ptr, id);
68 ptr = nullptr;
69 id = 0;
70 }
7c673cae
FG
71};
72
73#endif