|
@@ -0,0 +1,120 @@
|
|
|
|
+#ifndef _WRITE_COPY_HPP_
|
|
|
|
+#define _WRITE_COPY_HPP_
|
|
|
|
+
|
|
|
|
+#include <unordered_map>
|
|
|
|
+#include <algorithm>
|
|
|
|
+#include <memory>
|
|
|
|
+#include <mutex>
|
|
|
|
+
|
|
|
|
+#include <visit.h>
|
|
|
|
+
|
|
|
|
+template<typename T,typename K,typename V>
|
|
|
|
+struct write_copy_base:acceptor<V>
|
|
|
|
+{
|
|
|
|
+ std::unordered_map<K,V> m_map;
|
|
|
|
+ write_copy_base()
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ std::shared_ptr<T> clone()const
|
|
|
|
+ {
|
|
|
|
+ std::shared_ptr<T> ret=std::make_shared<T>();
|
|
|
|
+ ret->m_map.insert(m_map.begin(),m_map.end());
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ std::shared_ptr<T> clone_add(const std::unordered_map<K,V>&m)const
|
|
|
|
+ {
|
|
|
|
+ std::shared_ptr<T> ret=std::move(clone());
|
|
|
|
+ ret->_add(m);
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ std::shared_ptr<T> clone_add(K k,V v)const
|
|
|
|
+ {
|
|
|
|
+ std::shared_ptr<T> ret=std::move(clone());
|
|
|
|
+ ret->_add(k,v);
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ std::shared_ptr<T> clone_remove(K k)const
|
|
|
|
+ {
|
|
|
|
+ std::shared_ptr<T> ret=std::move(clone());
|
|
|
|
+ ret->_remove(k);
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ std::shared_ptr<T> clone_remove(const std::vector<K>&list)const
|
|
|
|
+ {
|
|
|
|
+ std::shared_ptr<T> ret=std::move(clone());
|
|
|
|
+ ret->_remove(list);
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ virtual ~write_copy_base()
|
|
|
|
+ {
|
|
|
|
+// std::for_each(m_list.begin(),m_list.end(),[](K*it){ });
|
|
|
|
+ }
|
|
|
|
+private:
|
|
|
|
+ void _remove(K k)
|
|
|
|
+ {
|
|
|
|
+ m_map.erase(k);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ void _remove(const std::vector<K>& v)
|
|
|
|
+ {
|
|
|
|
+ for(K&k:v)
|
|
|
|
+ {
|
|
|
|
+ m_map.erase(k);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ void _add(K k,V v)
|
|
|
|
+ {
|
|
|
|
+ m_map.insert(std::make_pair(k,v));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ void _add(const std::unordered_map<K,V>&m)
|
|
|
|
+ {
|
|
|
|
+ m_map.insert(m.begin(),m.end());
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+template<typename T,typename K,typename V>
|
|
|
|
+struct single_base:write_copy_base<T,K,V>
|
|
|
|
+{
|
|
|
|
+ typedef write_copy_base<T,K,V> base;
|
|
|
|
+ static std::shared_ptr<T> m_instance;
|
|
|
|
+ static std::shared_ptr<T> instance()
|
|
|
|
+ {
|
|
|
|
+ return m_instance;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ std::mutex m_mutex;
|
|
|
|
+ void add(K k,V c)
|
|
|
|
+ {
|
|
|
|
+ std::lock_guard<std::mutex> lock(m_mutex);
|
|
|
|
+ m_instance=std::move(base::clone_add(k,c));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ void add(const std::unordered_map<K,V>&c)
|
|
|
|
+ {
|
|
|
|
+ std::lock_guard<std::mutex> lock(m_mutex);
|
|
|
|
+ m_instance=std::move(base::clone_add(c));
|
|
|
|
+ }
|
|
|
|
+ void remove(K k,V c)
|
|
|
|
+ {
|
|
|
|
+ std::lock_guard<std::mutex> lock(m_mutex);
|
|
|
|
+ m_instance=std::move(base::clone_remove(k,c));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ void remove(const std::unordered_map<K,V>&c)
|
|
|
|
+ {
|
|
|
|
+ std::lock_guard<std::mutex> lock(m_mutex);
|
|
|
|
+ m_instance=std::move(base::clone_remove(c));
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+#endif
|
|
|
|
+
|