Created
January 10, 2019 15:50
-
-
Save neontorrent/78603ab60974c1c9b37c9f8a2a117755 to your computer and use it in GitHub Desktop.
Simple Auto Pointer Impl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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