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 / unknown.h < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  2.8 KB  |  78 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_UNKNOWN_H
  27. #define f_VD2_SYSTEM_UNKNOWN_H
  28.  
  29. #ifdef _MSC_VER
  30.     #pragma once
  31. #endif
  32.  
  33. #include <vd2/system/vdtypes.h>
  34.  
  35. ///////////////////////////////////////////////////////////////////////////
  36. //    IVDUnknown
  37. ///    Base interface for runtime type discovery.
  38. class IVDUnknown {
  39. public:
  40.     /// Attempt to cast to another type. Returns NULL if interface is unsupported.
  41.     virtual void *AsInterface(uint32 id) = 0;
  42.  
  43.     inline const void *AsInterface(uint32 id) const {
  44.         return const_cast<IVDUnknown *>(this)->AsInterface(id);
  45.     }
  46. };
  47.  
  48. ///////////////////////////////////////////////////////////////////////////
  49. //    IVDUnknown
  50. ///    Base interface for runtime type discovery with reference counting.
  51. class IVDRefUnknown : public IVDUnknown {
  52. public:
  53.     virtual int AddRef() = 0;    ///< Add strong reference to object. Returns new reference count (debug builds only).
  54.     virtual int Release() = 0;    ///< Remove strong refence from object, and destroy it if the refcount drops to zero. Returns zero if object was destroyed.
  55. };
  56.  
  57. template<class T>
  58. inline uint32 vdpoly_id_from_ptr(T *p) {
  59.     return T::kTypeID;
  60. }
  61.  
  62. ///////////////////////////////////////////////////////////////////////////
  63. //    vdpoly_cast
  64. ///    Performs a runtime polymorphic cast on an IUnknown-based object.
  65. ///
  66. ///    \param    pUnk    Pointer to cast. May be NULL.
  67. ///
  68. ///    Attempts to cast a pointer to a different type using the
  69. ///    \c AsInterface() method. The destination type must support the
  70. /// \c kTypeID convention for returning the type ID.
  71. /// 
  72. template<class T>
  73. T vdpoly_cast(IVDUnknown *pUnk) {
  74.     return pUnk ? (T)pUnk->AsInterface(vdpoly_id_from_ptr(T(NULL))) : NULL;
  75. }
  76.  
  77. #endif
  78.