59 lines
1.9 KiB
C++
59 lines
1.9 KiB
C++
#include <atomic>
|
|
#ifndef _POWELLCS440
|
|
#include <cassert>
|
|
#include <iostream>
|
|
|
|
#define todo(msg) \
|
|
std::cerr << msg << std::endl; \
|
|
assert(false);
|
|
|
|
namespace cs440 {
|
|
using counter = std::atomic<std::size_t>;
|
|
template <typename T> class SharedPtr {
|
|
counter count;
|
|
(???) underlying;
|
|
|
|
public:
|
|
// typename U meaning a type which inherits from T
|
|
SharedPtr();
|
|
|
|
template <typename U> explicit SharedPtr(U *);
|
|
|
|
SharedPtr(const SharedPtr &p);
|
|
template <typename U> SharedPtr(const SharedPtr<U> &p);
|
|
|
|
SharedPtr(SharedPtr &&p);
|
|
template <typename U> SharedPtr(SharedPtr<U> &&p);
|
|
|
|
// needs to handle self assignment
|
|
SharedPtr &operator=(const SharedPtr &);
|
|
template <typename U> SharedPtr<T> &operator=(const SharedPtr<U> &);
|
|
|
|
SharedPtr &operator=(SharedPtr &&p);
|
|
template <typename U> SharedPtr &operator=(SharedPtr<U> &&p);
|
|
~SharedPtr();
|
|
|
|
void reset() {
|
|
this->count = 0;
|
|
this->underlying = nullptr;
|
|
}
|
|
template <typename U> void reset(U *p);
|
|
T *get() const;
|
|
T &operator*() const { return *this->get(); }
|
|
T *operator->() const { return this->get(); }
|
|
explicit operator bool() const;
|
|
template <typename T1, typename T2>
|
|
friend bool operator==(const SharedPtr<T1> &, const SharedPtr<T2> &);
|
|
friend bool operator==(const SharedPtr<T> &, std::nullptr_t);
|
|
friend bool operator==(std::nullptr_t, const SharedPtr<T> &);
|
|
template <typename T1, typename T2>
|
|
friend bool operator!=(const SharedPtr<T1> &, const SharedPtr<T2> &);
|
|
friend bool operator!=(const SharedPtr<T> &, std::nullptr_t);
|
|
friend bool operator!=(std::nullptr_t, const SharedPtr<T> &);
|
|
template <typename U>
|
|
friend SharedPtr<T> static_pointer_cast(const SharedPtr<U> &sp);
|
|
template <typename U>
|
|
friend SharedPtr<T> dynamic_pointer_cast(const SharedPtr<U> &sp);
|
|
};
|
|
} // namespace cs440
|
|
#endif
|