home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / include / k3d / k3dsdk / pipeline_data.h < prev    next >
C/C++ Source or Header  |  2008-11-14  |  3KB  |  158 lines

  1. #ifndef K3DSDK_PIPELINE_DATA_H
  2. #define K3DSDK_PIPELINE_DATA_H
  3.  
  4. // K-3D
  5. // Copyright (c) 1995-2008, Timothy M. Shead
  6. //
  7. // Contact: tshead@k-3d.com
  8. //
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. // General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public
  20. // License along with this program; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  
  23. /** \file
  24.     \author Tim Shead (tshead@k-3d.com)
  25. */
  26.  
  27. #include "types.h"
  28. #include <boost/shared_ptr.hpp>
  29.  
  30. namespace k3d
  31. {
  32.  
  33. template<typename T>
  34. class pipeline_data_traits
  35. {
  36. public:
  37.     static T* create()
  38.     {
  39.         return new T();
  40.     }
  41.  
  42.     static T* clone(const T& Other)
  43.     {
  44.         return new T(Other);
  45.     }
  46. };
  47.  
  48. template<typename T>
  49. class pipeline_data
  50. {
  51. public:
  52.     typedef T value_type;
  53.     typedef T element_type;
  54.  
  55.     pipeline_data() :
  56.         originator(false)
  57.     {
  58.     }
  59.  
  60.     pipeline_data(const pipeline_data<T>& Other) :
  61.         storage(Other.storage),
  62.         originator(false)
  63.     {
  64.     }
  65.  
  66.     pipeline_data(T* Other) :
  67.         storage(Other),
  68.         originator(true)
  69.     {
  70.     }
  71.  
  72.     T& create()
  73.     {
  74.         storage.reset(pipeline_data_traits<T>::create());
  75.         originator = storage.get() ? true : false;
  76.         return *storage;
  77.     }
  78.  
  79.     T& create(T* Instance)
  80.     {
  81.         storage.reset(Instance);
  82.         originator = storage.get() ? true : false;
  83.         return *storage;
  84.     }
  85.  
  86.     template<typename Y>
  87.     T& create(Y* Instance)
  88.     {
  89.         storage.reset(Instance);
  90.         originator = storage.get() ? true : false;
  91.         return *storage;
  92.     }
  93.  
  94.     void reset()
  95.     {
  96.         storage.reset();
  97.         originator = false;
  98.     }
  99.  
  100.     const T& operator*() const
  101.     {
  102.         return *storage;
  103.     }
  104.  
  105.     const T* operator->() const
  106.     {
  107.         return storage.operator->();
  108.     }
  109.  
  110.     const T* get() const
  111.     {
  112.         return storage.get();
  113.     }
  114.  
  115.     T& writable()
  116.     {
  117.         if(originator)
  118.             return *storage;
  119.  
  120.         storage.reset(pipeline_data_traits<T>::clone(*storage));
  121.         originator = true;
  122.         return *storage;
  123.     }
  124.  
  125.     long use_count() const
  126.     {
  127.         return storage.use_count();
  128.     }
  129.  
  130.     pipeline_data& operator=(const pipeline_data& Other)
  131.     {
  132.         storage = Other.storage;
  133.         originator = false;
  134.         return *this;
  135.     }
  136.  
  137.     operator bool() const
  138.     {
  139.         return storage;
  140.     }
  141.  
  142.     bool operator!() const
  143.     {
  144.         return !storage;
  145.     }
  146.  
  147. private:
  148.     typedef boost::shared_ptr<T> storage_type;
  149.  
  150.     storage_type storage;
  151.     bool_t originator;
  152. };
  153.  
  154. } // namespace k3d
  155.  
  156. #endif // !K3DSDK_PIPELINE_DATA_H
  157.  
  158.