rippled
Loading...
Searching...
No Matches
SharedWeakCachePointer.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2023 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_BASICS_SHAREDWEAKCACHEPOINTER_H_INCLUDED
21#define RIPPLE_BASICS_SHAREDWEAKCACHEPOINTER_H_INCLUDED
22
23#include <memory>
24#include <variant>
25
26namespace ripple {
27
36template <class T>
38{
39public:
41
43
44 template <class TT>
47
49
50 template <class TT>
53
56
57 template <class TT>
61
62 template <class TT>
66
68
73 getStrong() const;
74
78 explicit
79 operator bool() const noexcept;
80
84 void
86
90 T*
91 get() const;
92
95 std::size_t
96 use_count() const;
97
99 bool
100 expired() const;
101
105 std::shared_ptr<T>
106 lock() const;
107
109 bool
110 isStrong() const;
111
113 bool
114 isWeak() const;
115
121 bool
123
128 bool
130
131private:
132 std::variant<std::shared_ptr<T>, std::weak_ptr<T>> combo_;
133};
134} // namespace ripple
135#endif
A combination of a std::shared_ptr and a std::weak_pointer.
std::shared_ptr< T > const & getStrong() const
Return a strong pointer if this is already a strong pointer (i.e.
bool expired() const
Return true if there is a non-zero strong count.
std::shared_ptr< T > lock() const
If this is a strong pointer, return the strong pointer.
bool convertToWeak()
If this is a strong pointer, attempt to convert it to a weak pointer.
void reset()
Set the pointer to null, decrement the appropriate ref count, and run the appropriate release action.
bool convertToStrong()
If this is a weak pointer, attempt to convert it to a strong pointer.
SharedWeakCachePointer & operator=(SharedWeakCachePointer const &rhs)
std::size_t use_count() const
If this is a strong pointer, return the strong count.
std::variant< std::shared_ptr< T >, std::weak_ptr< T > > combo_
SharedWeakCachePointer(SharedWeakCachePointer const &rhs)
SharedWeakCachePointer(std::shared_ptr< TT > &&rhs)
bool isStrong() const
Return true is this represents a strong pointer.
SharedWeakCachePointer & operator=(std::shared_ptr< TT > const &rhs)
T * get() const
If this is a strong pointer, return the raw pointer.
SharedWeakCachePointer & operator=(std::shared_ptr< TT > &&rhs)
SharedWeakCachePointer(std::shared_ptr< TT > const &rhs)
bool isWeak() const
Return true is this represents a weak pointer.
SharedWeakCachePointer(SharedWeakCachePointer &&rhs)
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
STL namespace.