home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / h / vd2 / system / vdalloc.h < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  3.7 KB  |  124 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    System library component
  3. //    Copyright (C) 1998-2004 Avery Lee, All Rights Reserved.
  4. //
  5. //    Beginning with 1.6.0, the VirtualDub system library is licensed
  6. //    differently than the remainder of VirtualDub.  This particular file is
  7. //    thus licensed as follows (the "zlib" license):
  8. //
  9. //    This software is provided 'as-is', without any express or implied
  10. //    warranty.  In no event will the authors be held liable for any
  11. //    damages arising from the use of this software.
  12. //
  13. //    Permission is granted to anyone to use this software for any purpose,
  14. //    including commercial applications, and to alter it and redistribute it
  15. //    freely, subject to the following restrictions:
  16. //
  17. //    1.    The origin of this software must not be misrepresented; you must
  18. //        not claim that you wrote the original software. If you use this
  19. //        software in a product, an acknowledgment in the product
  20. //        documentation would be appreciated but is not required.
  21. //    2.    Altered source versions must be plainly marked as such, and must
  22. //        not be misrepresented as being the original software.
  23. //    3.    This notice may not be removed or altered from any source
  24. //        distribution.
  25.  
  26. #ifndef f_VD2_SYSTEM_VDALLOC_H
  27. #define f_VD2_SYSTEM_VDALLOC_H
  28.  
  29. #ifdef _MSC_VER
  30.     #pragma once
  31. #endif
  32.  
  33. #include <stdlib.h>
  34.  
  35. // Why don't I use STL auto_ptr?  Two reasons.  First, auto_ptr has
  36. // the overhead of an ownership flag, and second, auto_ptr can't
  37. // be used with malloc() blocks.  So think of these as auto_ptr
  38. // objects, but not quite....
  39.  
  40. #pragma warning(push)
  41. #pragma warning(disable: 4284)        // operator-> must return pointer to UDT
  42.  
  43. class vdautoblockptr {
  44. protected:
  45.     void *ptr;
  46.  
  47. public:
  48.     explicit vdautoblockptr(void *p = 0) : ptr(p) {}
  49.     ~vdautoblockptr() { free(ptr); }
  50.  
  51.     vdautoblockptr& operator=(void *src) { free(ptr); ptr = src; return *this; }
  52.  
  53.     operator void*() const { return ptr; }
  54.  
  55.     vdautoblockptr& from(vdautoblockptr& src) { free(ptr); ptr=src.ptr; src.ptr=0; }
  56.     void *get() const { return ptr; }
  57.     void *release() { void *v = ptr; ptr = NULL; return v; }
  58. };
  59.  
  60. template<class T> class vdautoptr2 {
  61. protected:
  62.     T *ptr;
  63.  
  64. public:
  65.     explicit vdautoptr2(T *p = 0) : ptr(p) {}
  66.     ~vdautoptr2() { free((void *)ptr); }
  67.  
  68.     vdautoptr2<T>& operator=(T *src) { free((void *)ptr); ptr = src; return *this; }
  69.  
  70.     operator T*() const { return ptr; }
  71.     T& operator*() const { return *ptr; }
  72.     T *operator->() const { return ptr; }
  73.  
  74.     vdautoptr2<T>& from(vdautoptr2<T>& src) { free((void *)ptr); ptr=src.ptr; src.ptr=0; }
  75.     T *get() const { return ptr; }
  76.     T *release() { T *v = ptr; ptr = NULL; return v; }
  77. };
  78.  
  79. template<class T> class vdautoptr {
  80. protected:
  81.     T *ptr;
  82.  
  83. public:
  84.     explicit vdautoptr(T *p = 0) : ptr(p) {}
  85.     ~vdautoptr() { delete ptr; }
  86.  
  87.     vdautoptr<T>& operator=(T *src) { delete ptr; ptr = src; return *this; }
  88.  
  89.     operator T*() const { return ptr; }
  90.     T& operator*() const { return *ptr; }
  91.     T *operator->() const { return ptr; }
  92.  
  93.     vdautoptr<T>& from(vdautoptr<T>& src) { delete ptr; ptr=src.ptr; src.ptr=0; }
  94.     T *get() const { return ptr; }
  95.     T *release() { T *v = ptr; ptr = NULL; return v; }
  96.  
  97.     void swap(vdautoptr<T>& other) {
  98.         T *p = other.ptr;
  99.         other.ptr = ptr;
  100.         ptr = p;
  101.     }
  102. };
  103.  
  104. template<class T> class vdautoarrayptr {
  105. protected:
  106.     T *ptr;
  107.  
  108. public:
  109.     explicit vdautoarrayptr(T *p = 0) : ptr(p) {}
  110.     ~vdautoarrayptr() { delete[] ptr; }
  111.  
  112.     vdautoarrayptr<T>& operator=(T *src) { delete[] ptr; ptr = src; return *this; }
  113.  
  114.     T& operator[](int offset) const { return ptr[offset]; }
  115.  
  116.     vdautoarrayptr<T>& from(vdautoarrayptr<T>& src) { delete[] ptr; ptr=src.ptr; src.ptr=0; }
  117.     T *get() const { return ptr; }
  118.     T *release() { T *v = ptr; ptr = NULL; return v; }
  119. };
  120.  
  121. #pragma warning(pop)
  122.  
  123. #endif
  124.