Skip to content

Instantly share code, notes, and snippets.

@neontorrent
Created January 10, 2019 15:50
Show Gist options
  • Save neontorrent/78603ab60974c1c9b37c9f8a2a117755 to your computer and use it in GitHub Desktop.
Save neontorrent/78603ab60974c1c9b37c9f8a2a117755 to your computer and use it in GitHub Desktop.
Simple Auto Pointer Impl
std::unordered_map<void*, size_t> *auto_ref_cnt;
template<typename T>
class AutoPtr {
T * data;
public:
AutoPtr() = delete;
AutoPtr(T* data): data(data) {
if(auto_ref_cnt == nullptr) {
auto_ref_cnt = new std::unordered_map<void*, size_t>;
}
auto_ref_cnt->operator[](data) += 1;
}
~AutoPtr() {
auto_ref_cnt->operator[](data) -= 1;
if (auto_ref_cnt->operator[](data) == 0) {
auto_ref_cnt->erase(data);
if (auto_ref_cnt->empty()) {
delete auto_ref_cnt;
auto_ref_cnt = nullptr;
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment