]>
Commit | Line | Data |
---|---|---|
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 | |
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 | |
11fdf7f2 | 30 | ceph_assert(id == 0); |
7c673cae FG |
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 | ||
11fdf7f2 | 43 | TrackedIntPtr& operator=(const TrackedIntPtr &rhs) { |
7c673cae FG |
44 | TrackedIntPtr o(rhs.ptr); |
45 | swap(o); | |
11fdf7f2 | 46 | return *this; |
7c673cae | 47 | } |
31f18b77 | 48 | T &operator*() const { |
7c673cae FG |
49 | return *ptr; |
50 | } | |
31f18b77 | 51 | T *operator->() const { |
7c673cae FG |
52 | return ptr; |
53 | } | |
31f18b77 FG |
54 | T *get() const { return ptr; } |
55 | ||
7c673cae FG |
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 | } | |
31f18b77 FG |
65 | |
66 | void reset() { | |
67 | if (ptr) | |
68 | put_with_id(ptr, id); | |
69 | ptr = nullptr; | |
70 | id = 0; | |
71 | } | |
7c673cae FG |
72 | }; |
73 | ||
74 | #endif |