diff options
author | John Ericson <John.Ericson@Obsidian.Systems> | 2021-09-30 22:42:15 +0000 |
---|---|---|
committer | John Ericson <John.Ericson@Obsidian.Systems> | 2021-09-30 22:42:15 +0000 |
commit | 9af9ab42126869b0be920db0224b7e1da58342a1 (patch) | |
tree | 696cb996c35a75a15729f86ded875a0bcdffdc09 /src/libutil/ref.hh | |
parent | d0ed11ca729344fd00251d0bc31f0c2f32d2c6a7 (diff) | |
parent | f4f3203aa7c2fc9225a8ae220db25593066fb397 (diff) |
Merge branch 'path-info' into ca-drv-exotic
Diffstat (limited to 'src/libutil/ref.hh')
-rw-r--r-- | src/libutil/ref.hh | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/libutil/ref.hh b/src/libutil/ref.hh index 0be2a7e74..d6bf53bb8 100644 --- a/src/libutil/ref.hh +++ b/src/libutil/ref.hh @@ -73,6 +73,16 @@ public: return ref<T2>((std::shared_ptr<T2>) p); } + bool operator == (const ref<T> & other) const + { + return p == other.p; + } + + bool operator != (const ref<T> & other) const + { + return p != other.p; + } + private: template<typename T2, typename... Args> @@ -89,4 +99,47 @@ make_ref(Args&&... args) return ref<T>(p); } + +/* A non-nullable pointer. + This is similar to a C++ "& reference", but mutable. + This is similar to ref<T> but backed by a regular pointer instead of a smart pointer. + */ +template<typename T> +class ptr { +private: + T * p; + +public: + ptr<T>(const ptr<T> & r) + : p(r.p) + { } + + explicit ptr<T>(T * p) + : p(p) + { + if (!p) + throw std::invalid_argument("null pointer cast to ptr"); + } + + T* operator ->() const + { + return &*p; + } + + T& operator *() const + { + return *p; + } + + bool operator == (const ptr<T> & other) const + { + return p == other.p; + } + + bool operator != (const ptr<T> & other) const + { + return p != other.p; + } +}; + } |