Commit 11d39415 authored by Tom Niget's avatar Tom Niget

Make PyDict a refcounted type

parent bee6d698
......@@ -6,7 +6,7 @@
#define TYPON_DICT_HPP
#include <unordered_map>
/*
template <typename K, typename V>
class PyDict : public std::unordered_map<K, V> {
public:
......@@ -81,6 +81,49 @@ public:
};
template <typename K, typename V>
PyDict(std::initializer_list<std::pair<K, V>>) -> PyDict<K, V>;
PyDict(std::initializer_list<std::pair<K, V>>) -> PyDict<K, V>;*/
template <typename K, typename V> class PyDict {
public:
PyDict(std::shared_ptr<std::unordered_map<K, V>> &&m) : _m(std::move(m)) {}
PyDict(std::unordered_map<K, V> &&m)
: _m(std::move(
std::make_shared<std::unordered_map<K, V>>(std::move(m)))) {}
PyDict(std::initializer_list<std::pair<K, V>> &&m)
: _m(std::make_shared<std::unordered_map<K, V>>(std::move(m))) {}
PyDict() : _m(std::make_shared<std::unordered_map<K, V>>()) {}
auto begin() const { return _m->begin(); }
auto end() const { return _m->end(); }
auto py_contains(const K &k) const { return _m->find(k) != _m->end(); }
constexpr const V &operator[](const K &k) const { return (*_m)[k]; }
constexpr V &operator[](const K &k) { return (*_m)[k]; }
size_t py_len() const { return _m->size(); }
void py_repr(std::ostream &s) const {
s << '{';
if (_m->size() > 0) {
repr_to(_m->begin()->first, s);
s << ": ";
repr_to(_m->begin()->second, s);
for (auto it = ++_m->begin(); it != _m->end(); it++) {
s << ", ";
repr_to(it->first, s);
s << ": ";
repr_to(it->second, s);
}
}
s << '}';
}
void py_print(std::ostream &s) const { py_repr(s); }
private:
std::shared_ptr<std::unordered_map<K, V>> _m;
};
#endif // TYPON_DICT_HPP
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment