home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / ole / tstcon / propbag.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  3.2 KB  |  176 lines

  1. #include "StdAfx.H"
  2. #include "TestCon.H"
  3.  
  4. #ifdef _DEBUG
  5. #define new DEBUG_NEW
  6. #undef THIS_FILE
  7. static char THIS_FILE[] = __FILE__;
  8. #endif
  9.  
  10. IMPLEMENT_DYNAMIC( CPropertyBagItem, CObject );
  11.  
  12. CPropertyBagItem::CPropertyBagItem( LPCOLESTR pszName, VARIANT* pvarValue )
  13. {
  14.    m_strName = pszName;
  15.    m_varValue = pvarValue;
  16. }
  17.  
  18.  
  19. CPropertyBag::CPropertyBag() :
  20.    m_nRefCount( 0 )
  21. {
  22. }
  23.  
  24. CPropertyBag::~CPropertyBag()
  25. {
  26.    CPropertyBagItem* pItem;
  27.  
  28.    while( !m_lpItems.IsEmpty() )
  29.    {
  30.       pItem = m_lpItems.RemoveHead();
  31.       delete pItem;
  32.    }
  33. }
  34.  
  35. POSITION CPropertyBag::GetFirstItemPosition() const
  36. {
  37.    return( m_lpItems.GetHeadPosition() );
  38. }
  39.  
  40. CPropertyBagItem* CPropertyBag::GetNextItem( POSITION& posItem ) const
  41. {
  42.    return( m_lpItems.GetNext( posItem ) );
  43. }
  44.  
  45. STDMETHODIMP_( ULONG ) CPropertyBag::AddRef()
  46. {
  47.    m_nRefCount++;
  48.  
  49.    return( m_nRefCount );
  50. }
  51.  
  52. STDMETHODIMP_( ULONG ) CPropertyBag::Release()
  53. {
  54.    m_nRefCount--;
  55.  
  56.    if( m_nRefCount == 0 )
  57.    {
  58.       delete this;
  59.       return( 0 );
  60.    }
  61.    else
  62.    {
  63.       return( m_nRefCount );
  64.    }
  65. }
  66.  
  67. STDMETHODIMP CPropertyBag::QueryInterface( REFIID iid, void** ppInterface )
  68. {
  69.    ASSERT( ppInterface != NULL );
  70.    *ppInterface = NULL;
  71.  
  72.    if( iid == IID_IUnknown )
  73.    {
  74.       *ppInterface = (IUnknown*)this;
  75.    }
  76.    else if( iid == IID_IPropertyBag )
  77.    {
  78.       *ppInterface = (IPropertyBag*)this;
  79.    }
  80.  
  81.    if( *ppInterface != NULL )
  82.    {
  83.       AddRef();
  84.       return( S_OK );
  85.    }
  86.    else
  87.    {
  88.       return( E_NOINTERFACE );
  89.    }
  90. }
  91.  
  92. STDMETHODIMP CPropertyBag::Read( LPCOLESTR pszPropName, VARIANT* pvarValue,
  93.    IErrorLog* pErrorLog )
  94. {
  95.    USES_CONVERSION;
  96.    POSITION posItem;
  97.    CPropertyBagItem* pItem;
  98.    CString strPropName;
  99.    HRESULT hResult;
  100.    EXCEPINFO error;
  101.  
  102.    if( pszPropName == NULL )
  103.    {
  104.       return( E_POINTER );
  105.    }
  106.    if( pvarValue == NULL )
  107.    {
  108.       return( E_POINTER );
  109.    }
  110.  
  111.    memset( &error, 0, sizeof( error ) );
  112.  
  113.    strPropName = OLE2CT( pszPropName );
  114.  
  115.    posItem = m_lpItems.GetHeadPosition();
  116.    while( posItem != NULL )
  117.    {
  118.       pItem = m_lpItems.GetNext( posItem );
  119.       if( pItem->m_strName == strPropName )
  120.       {
  121.          // We found the property.
  122.          if( pvarValue->vt == VT_EMPTY )
  123.          {
  124.             hResult = VariantCopy( pvarValue, &pItem->m_varValue );
  125.             if( FAILED( hResult ) )
  126.             {
  127.                if( pErrorLog != NULL )
  128.                {
  129.                   error.scode = hResult;
  130.                   pErrorLog->AddError( pszPropName, &error );
  131.                }
  132.                return( E_FAIL );
  133.             }
  134.          }
  135.          else
  136.          {
  137.             hResult = VariantChangeType( pvarValue, &pItem->m_varValue, 0,
  138.                pvarValue->vt );
  139.             if( FAILED( hResult ) )
  140.             {
  141.                if( pErrorLog != NULL )
  142.                {
  143.                   error.scode = hResult;
  144.                   pErrorLog->AddError( pszPropName, &error );
  145.                }
  146.                return( E_FAIL );
  147.             }
  148.          }
  149.          return( S_OK );
  150.       }
  151.    }
  152.  
  153.    return( E_INVALIDARG );
  154. }
  155.  
  156. STDMETHODIMP CPropertyBag::Write( LPCOLESTR pszPropName, VARIANT* pvarValue )
  157. {
  158.    USES_CONVERSION;
  159.    CPropertyBagItem* pItem;
  160.  
  161.    if( (pszPropName == NULL) || (pvarValue == NULL) )
  162.    {
  163.       return( E_POINTER );
  164.    }
  165.  
  166.    pItem = new CPropertyBagItem( pszPropName, pvarValue );
  167.    if( pItem == NULL )
  168.    {
  169.       return( E_OUTOFMEMORY );
  170.    }
  171.  
  172.    m_lpItems.AddTail( pItem );
  173.  
  174.    return( S_OK );
  175. }
  176.