//  Beta release only.
//
//  Copyright(c) Ivyware Pty Ltd 2013  (all rights reserved)
//               MELBOURNE, VICTORIA, AUSTRALIA, 3130
//
//  Provided no fee is charged (either by the distributer or user)
//  permission to use, copy, modify, and distribute this software and
//  its documentation for any purpose and without fee is hereby granted,
//  provided that the above copyright notice appear in all copies and
//  that both that copyright notice and this permission notice appear
//  in supporting documentation.
//
//  No waranty or suitablity for purpose is implied.
//
//
//  Definitions and prototypes for Microsoft MAPI C++ extensions
//
#pragma once
#include "P2PmsgMAPI.h"
#include "P2Pevent.h"

//
//  Stack based safe MAPI buffer container
//  NOTES: MAPIAllocateBuffer()'s are managed within the life cycle
//         of this object.
class P2PmsgMAPI_EXT P2PsafeMAPIbuffer
{
    public:
        P2PsafeMAPIbuffer ( );
        P2PsafeMAPIbuffer ( LPVOID lpvBuffer );
       ~P2PsafeMAPIbuffer ( );
    public:
      LPVOID
        operator = ( LPVOID lpvBUffer );
    private:
        LPVOID  m_lpvBuffer;
};

//
//  Stack based safe MAPI SRowSet container
//  NOTES: MAPI SRowSet pointers are managed within the life cycle
//         of this object.
class P2PmsgMAPI_EXT P2PsafeSRowSet
{
    public:
        P2PsafeSRowSet ( );
        P2PsafeSRowSet ( SRowSet *pSRowSet );
       ~P2PsafeSRowSet ( );
    public:
      SRowSet*
        operator = ( SRowSet *pSRowSet );
      SRowSet*
        operator->() const;
      SRowSet*
        operator*();
        operator SRowSet* ( );
        operator SRowSet** ( );
    private:
        SRowSet  *m_pSRowSet;
};

//
//  Convenient MAPI definitions
#define RELEASE(pI) if(pI!=nullptr){pI->Release();pI=nullptr;}

#ifdef UNICODE
#define T_MAPI_UNICODE MAPI_UNICODE
#else
#define T_MAPI_UNICODE 0
#endif

//
//  Internal MAPI helpers
LPCTSTR
MAPI_GetValidString ( SPropValue& oProp );
LPCTSTR
MAPI_GetValidMVString ( SPropValue& oProp, int nIndex );
void
MAPI_GetNarrowString ( SPropValue& oProp, CString& strNarrow );
LPCTSTR
MAPI_Check4BadString ( LPCTSTR lpszString );

//
//  Error reporting helpers
P2PmsgMAPI_EXT P2Pevent*
SetP2PeventMAPI ( LPCTSTR lpszModule, LPCTSTR lpszMessage_, HRESULT hr );
#define MAPI_HRESULT(hr,lpszMessage)  SetP2PeventMAPI(T__FUNCTION__,lpszMessage,hr)->GetHRESULT()
#define EVERR_MAPI(hr,lpszMessage)    SetP2PeventMAPI(T__FUNCTION__,lpszMessage,hr)
#define THROW_MAPI(hr,lpszMessage)    SetP2PeventMAPI(T__FUNCTION__,lpszMessage,hr)->Throw()
