Advertisement
JoshDreamland

plugin_ptr.hpp

Sep 27th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. /* Copyright (c) 2014 Josh Ventura
  2.  *
  3.  * This program is free software: you can redistribute it and/or modify
  4.  * it under the terms of the GNU Lesser General Public License as published
  5.  * by the Free Software Foundation, either version 3 of the License, or
  6.  * (at your option) any later version.
  7.  *
  8.  * This program is distributed in the hope that it will be useful,
  9.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  * GNU Lesser General Public License for more details.
  12.  *
  13.  * You should have received a copy of the GNU Lesser General Public License
  14.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15. */
  16.  
  17. #ifndef PLUGIN_PTR_H
  18. #define PLUGIN_PTR_H
  19. #include <memory>
  20. #include <list>
  21. #include <thread>
  22.  
  23. template<class T, class Plugin> class plugin_ptr;
  24.  
  25. namespace plugin_ptr_garbage {
  26.   class collecter {
  27.    public:
  28.     virtual ~collecter() {}
  29.   };
  30.   template<class Plugin> class plugin_ptr_list {
  31.     // If you use this file from more than one source,
  32.     // these must be moved into each Plugin class:
  33.     static size_t global_reference_count;
  34.     static std::list<collecter*> all_pointers;
  35.    
  36.     static void add(collecter *who) {
  37.       all_pointers.push_back(who);
  38.     }
  39.     static void collect() {
  40.       for (std::list<collecter*>::iterator it = all_pointers.begin();
  41.           it != all_pointers.end(); ++it) {
  42.         delete *it;
  43.       }
  44.       all_pointers.clear();
  45.       while (global_reference_count) {
  46.         std::this_thread::yield();
  47.       }
  48.     }
  49.    
  50.     template<class T, class P> friend class plugin_ptr;
  51.     template<class P> friend void collect(P *);
  52.   };
  53.  
  54.   // If you use this file from more than one source, delete these.
  55.   template<class Plugin>
  56.       size_t plugin_ptr_list<Plugin>::global_reference_count;
  57.   template<class Plugin>
  58.       std::list<collecter*> plugin_ptr_list<Plugin>::all_pointers;
  59.  
  60.   // Call this to clean up
  61.   template<class Plugin> void collect(Plugin *) {
  62.     plugin_ptr_list<Plugin>::collect();
  63.   }
  64. }
  65.  
  66. template<class T, class Plugin> class plugin_ptr:
  67.     plugin_ptr_garbage::collecter {
  68.   class deleter {
  69.    public:
  70.     void operator() (T* t) {
  71.       delete t;
  72.       --plugin_ptr_garbage::plugin_ptr_list<Plugin>::global_reference_count;
  73.     }
  74.   };
  75.  
  76.   std::shared_ptr<T> data_share;
  77.  
  78.  public:
  79.  
  80.   plugin_ptr(T* data): data_share(data, deleter()) {
  81.     ++plugin_ptr_garbage::plugin_ptr_list<Plugin>::global_reference_count;
  82.     plugin_ptr_garbage::plugin_ptr_list<Plugin>::add(this);
  83.   }
  84.  
  85.   std::weak_ptr<T> get_pointer() const {
  86.     return std::weak_ptr<T>(data_share);
  87.   }
  88.  
  89.   ~plugin_ptr() {}
  90. };
  91.  
  92. #endif // PLUGIN_PTR_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement