home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / include / k3d / k3dsdk / utility.h < prev    next >
C/C++ Source or Header  |  2008-01-23  |  1KB  |  63 lines

  1. #ifndef K3DSDK_UTILITY_H
  2. #define K3DSDK_UTILITY_H
  3.  
  4. namespace k3d
  5. {
  6.  
  7. template<typename container_t>
  8. struct inserter_t
  9. {
  10.     explicit inserter_t(container_t& Container) : container(Container) {}
  11.     
  12.     void operator()(typename container_t::value_type Value)
  13.     {
  14.         container.insert(container.end(), Value);
  15.     }
  16.     
  17.     container_t& container;
  18. };
  19.  
  20. template<typename container_t>
  21. inserter_t<container_t> inserter(container_t& Container)
  22. {
  23.     return inserter_t<container_t>(Container);
  24. }
  25.  
  26. /// Convenience function that returns a collection containing a single item
  27. template<typename container_t, typename value_t>
  28. const container_t make_collection(const value_t& Value)
  29. {
  30.     container_t result;
  31.     result.insert(result.end(), Value);
  32.     
  33.     return result;
  34. }
  35.  
  36. /// copy_if() - as described in "Effective STL" ...
  37. template<typename InputIterator, typename OutputIterator, typename Predicate>
  38. OutputIterator copy_if(InputIterator Begin, InputIterator End, OutputIterator DestBegin, Predicate P)
  39. {
  40.     while(Begin != End)
  41.     {
  42.         if(P(*Begin))
  43.             *DestBegin++ = *Begin;
  44.         ++Begin;
  45.     }
  46.     
  47.     return DestBegin;
  48. }
  49.  
  50. /// Use with std::for_each to destroy collections of objects stored by pointer
  51. struct delete_object
  52. {
  53.     template<typename T>
  54.     void operator()(const T* ptr) const
  55.     {
  56.         delete ptr;
  57.     }
  58. };
  59.  
  60. } // namespace k3d
  61.  
  62. #endif // !K3DSDK_UTILITY_H
  63.