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 / VDXFrame / Unknown.h next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  3.1 KB  |  111 lines

  1. //    VDXFrame - Helper library for VirtualDub plugins
  2. //    Copyright (C) 2008 Avery Lee
  3. //
  4. //    The plugin headers in the VirtualDub plugin SDK are licensed differently
  5. //    differently than VirtualDub and the Plugin SDK themselves.  This
  6. //    particular file is thus licensed as follows (the "zlib" license):
  7. //
  8. //    This software is provided 'as-is', without any express or implied
  9. //    warranty.  In no event will the authors be held liable for any
  10. //    damages arising from the use of this software.
  11. //
  12. //    Permission is granted to anyone to use this software for any purpose,
  13. //    including commercial applications, and to alter it and redistribute it
  14. //    freely, subject to the following restrictions:
  15. //
  16. //    1.    The origin of this software must not be misrepresented; you must
  17. //        not claim that you wrote the original software. If you use this
  18. //        software in a product, an acknowledgment in the product
  19. //        documentation would be appreciated but is not required.
  20. //    2.    Altered source versions must be plainly marked as such, and must
  21. //        not be misrepresented as being the original software.
  22. //    3.    This notice may not be removed or altered from any source
  23. //        distribution.
  24.  
  25. #ifndef f_VD2_VDXFRAME_UNKNOWN_H
  26. #define f_VD2_VDXFRAME_UNKNOWN_H
  27.  
  28. #include <vd2/plugin/vdplugin.h>
  29.  
  30. extern "C" long _InterlockedExchangeAdd(volatile long *p, long v);
  31. #pragma intrinsic(_InterlockedExchangeAdd)
  32.  
  33. template<class T> class vdxunknown : public T {
  34. public:
  35.     vdxunknown() : mRefCount(0) {}
  36.     vdxunknown(const vdxunknown<T>& src) : mRefCount(0) {}        // do not copy the refcount
  37.     virtual ~vdxunknown() {}
  38.  
  39.     vdxunknown<T>& operator=(const vdxunknown<T>&) {}            // do not copy the refcount
  40.  
  41.     virtual int VDXAPIENTRY AddRef() {
  42.         return _InterlockedExchangeAdd(&mRefCount, 1) + 1;
  43.     }
  44.  
  45.     virtual int VDXAPIENTRY Release() {
  46.         long rc = _InterlockedExchangeAdd(&mRefCount, -1) - 1;
  47.         if (!mRefCount) {
  48.             mRefCount = 1;
  49.             delete this;
  50.             return 0;
  51.         }
  52.  
  53.         return rc;
  54.     }
  55.  
  56.     virtual void *VDXAPIENTRY AsInterface(uint32 iid) {
  57.         if (iid == T::kIID)
  58.             return static_cast<T *>(this);
  59.  
  60.         if (iid == IVDXUnknown::kIID)
  61.             return static_cast<IVDXUnknown *>(this);
  62.  
  63.         return NULL;
  64.     }
  65.  
  66. protected:
  67.     volatile long    mRefCount;
  68. };
  69.  
  70. template<class T1, class T2> class vdxunknown2 : public T1, public T2 {
  71. public:
  72.     vdxunknown2() : mRefCount(0) {}
  73.     vdxunknown2(const vdxunknown2& src) : mRefCount(0) {}        // do not copy the refcount
  74.     virtual ~vdxunknown2() {}
  75.  
  76.     vdxunknown2& operator=(const vdxunknown2&) {}                // do not copy the refcount
  77.  
  78.     virtual int VDXAPIENTRY AddRef() {
  79.         return _InterlockedExchangeAdd(&mRefCount, 1) + 1;
  80.     }
  81.  
  82.     virtual int VDXAPIENTRY Release() {
  83.         long rc = _InterlockedExchangeAdd(&mRefCount, -1) - 1;
  84.         if (!mRefCount) {
  85.             mRefCount = 1;
  86.             delete this;
  87.             return 0;
  88.         }
  89.  
  90.         return rc;
  91.     }
  92.  
  93.     virtual void *VDXAPIENTRY AsInterface(uint32 iid) {
  94.         if (iid == T1::kIID)
  95.             return static_cast<T1 *>(this);
  96.  
  97.         if (iid == T2::kIID)
  98.             return static_cast<T2 *>(this);
  99.  
  100.         if (iid == IVDXUnknown::kIID)
  101.             return static_cast<IVDXUnknown *>(static_cast<T1 *>(this));
  102.  
  103.         return NULL;
  104.     }
  105.  
  106. protected:
  107.     volatile long    mRefCount;
  108. };
  109.  
  110. #endif
  111.