/* /////////////////////////////////////////////////////////////////////////////
 * File:        comstl_initialisers.h (originally MOInit.h, ::SynesisCom)
 *
 * Purpose:     Contains classes for initialising COM/OLE.
 *
 * Created:     8th February 1999
 * Updated:     26th February 2004
 *
 * Author:      Matthew Wilson, Synesis Software Pty Ltd.
 *
 * License:     (Licensed under the Synesis Software Standard Source License)
 *
 *              Copyright (C) 2002-2003, Synesis Software Pty Ltd.
 *
 *              All rights reserved.
 *
 *              www:        http://www.synesis.com.au/comstl
 *                          http://www.comstl.org/
 *
 *              email:      submissions@comstl.org  for submissions
 *                          admin@comstl.org        for other enquiries
 *
 *              Redistribution and use in source and binary forms, with or
 *              without modification, are permitted provided that the following
 *              conditions are met:
 *
 *              (i) Redistributions of source code must retain the above
 *              copyright notice and contact information, this list of
 *              conditions and the following disclaimer.
 *
 *              (ii) Any derived versions of this software (howsoever modified)
 *              remain the sole property of Synesis Software.
 *
 *              (iii) Any derived versions of this software (howsoever modified)
 *              remain subject to all these conditions.
 *
 *              (iv) Neither the name of Synesis Software nor the names of any
 *              subdivisions, employees or agents of Synesis Software, nor the
 *              names of any other contributors to this software may be used to
 *              endorse or promote products derived from this software without
 *              specific prior written permission.
 *
 *              This source code is provided by Synesis Software "as is" and any
 *              warranties, whether expressed or implied, including, but not
 *              limited to, the implied warranties of merchantability and
 *              fitness for a particular purpose are disclaimed. In no event
 *              shall the Synesis Software be liable for any direct, indirect,
 *              incidental, special, exemplary, or consequential damages
 *              (including, but not limited to, procurement of substitute goods
 *              or services; loss of use, data, or profits; or business
 *              interruption) however caused and on any theory of liability,
 *              whether in contract, strict liability, or tort (including
 *              negligence or otherwise) arising in any way out of the use of
 *              this software, even if advised of the possibility of such
 *              damage.
 *
 * ////////////////////////////////////////////////////////////////////////// */


#ifndef COMSTL_INCL_H_COMSTL_INITIALISERS
#define COMSTL_INCL_H_COMSTL_INITIALISERS

#ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
# define _COMSTL_VER_H_COMSTL_INITIALISERS_MAJOR    1
# define _COMSTL_VER_H_COMSTL_INITIALISERS_MINOR    8
# define _COMSTL_VER_H_COMSTL_INITIALISERS_REVISION 1
# define _COMSTL_VER_H_COMSTL_INITIALISERS_EDIT     35
#endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */

/* ////////////////////////////////////////////////////////////////////////////
 * Includes
 */

#ifndef COMSTL_INCL_H_COMSTL
# include "comstl.h"                // Include the COMSTL root header
#endif /* !COMSTL_INCL_H_COMSTL */

/* /////////////////////////////////////////////////////////////////////////////
 * Namespace
 *
 * The COMSTL components are contained within the comstl namespace. This is
 * actually an alias for stlsoft::comstl_project,
 *
 * The definition matrix is as follows:
 *
 * _STLSOFT_NO_NAMESPACE    _COMSTL_NO_NAMESPACE    comstl definition
 * ---------------------    --------------------    -----------------
 *  not defined              not defined             = stlsoft::comstl_project
 *  not defined              defined                 not defined
 *  defined                  not defined             comstl
 *  defined                  defined                 not defined
 *
 */

#ifndef _COMSTL_NO_NAMESPACE
# ifdef _STLSOFT_NO_NAMESPACE
/* There is no stlsoft namespace, so must define ::comstl */
namespace comstl
{
# else
/* Define stlsoft::comstl_project */

namespace stlsoft
{

namespace comstl_project
{

# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_COMSTL_NO_NAMESPACE */

/* /////////////////////////////////////////////////////////////////////////////
 * Classes
 */

/// Initialises the COM libraries
///
/// This class is used to initialise the COM libraries. If can respond to
/// CoInitializeEx argument flags when translated in a DCOM build.
class com_initialiser
{
private:
    typedef com_initialiser   class_type;

// Construction
public:
    /// Initialises via CoInitialize()
    com_initialiser();
#ifdef __COMSTL_CF_DCOM_SUPPORT
    /// Initialises via CoInitializeEx() taking b>COINIT_*</b> flags
    ss_explicit_k com_initialiser(DWORD dwInit /* = COINIT_APARTMENTTHREADED */);
#endif /* __COMSTL_CF_DCOM_SUPPORT */
    /// Uninitialises via CoUninitialize()
    ~com_initialiser() comstl_throw_0();

// Attributes
public:
    /// Reflects whether the COM libraries were initialised
    cs_bool_t is_initialised() const;
    /// Reflects whether the COM libraries were not initialised
    cs_bool_t operator !() const;
    /// The result of the call to CoInitialize()/CoInitializeEx()
    HRESULT get_HRESULT() const;

// Members
private:
    HRESULT const   m_hr;

// Not to be implemented
private:
    com_initialiser(class_type const &rhs);
    class_type const &operator =(class_type const &rhs);
};

/// Initialises the OLE libraries
///
/// This class is used to initialise the OLE libraries.
class ole_initialiser
{
private:
    typedef ole_initialiser  class_type;

// Construction
public:
    /// Initialises via OleInitialize()
    ole_initialiser();
    /// Initialises via OleUninitialize()
    ~ole_initialiser() comstl_throw_0();

// Attributes
public:
    /// Reflects whether the OLE libraries were initialised
    cs_bool_t is_initialised() const;
    /// Reflects whether the OLE libraries were not initialised
    cs_bool_t operator !() const;
    /// The result of the call to OleInitialize()
    HRESULT get_HRESULT() const;

// Members
private:
    HRESULT const   m_hr;

// Not to be implemented
private:
    ole_initialiser(class_type const &rhs);
    class_type const &operator =(class_type const &rhs);
};

////////////////////////////////////////////////////////////////////////////////
// Typedefs for US-English spellers

typedef com_initialiser	com_initializer;
typedef ole_initialiser	ole_initializer;

////////////////////////////////////////////////////////////////////////////////
// Implementation

// com_initialiser

inline com_initialiser::com_initialiser()
    : m_hr(::CoInitialize(NULL))
{}

#ifdef __COMSTL_CF_DCOM_SUPPORT
inline com_initialiser::com_initialiser(DWORD dwInit)
    : m_hr(::CoInitializeEx(NULL, dwInit))
{}
#endif // __COMSTL_CF_DCOM_SUPPORT

inline com_initialiser::~com_initialiser() comstl_throw_0()
{
    if(is_initialised())
    {
        ::CoUninitialize();
    }
}

inline cs_bool_t com_initialiser::is_initialised() const
{
    return SUCCEEDED(m_hr);
}

inline cs_bool_t com_initialiser::operator !() const
{
    return !is_initialised();
}

inline HRESULT com_initialiser::get_HRESULT() const
{
    return m_hr;
}

// ole_initialiser

inline ole_initialiser::ole_initialiser()
    : m_hr(::OleInitialize(NULL))
{}

inline ole_initialiser::~ole_initialiser() comstl_throw_0()
{
    if(is_initialised())
    {
        ::OleUninitialize();
    }
}

inline cs_bool_t ole_initialiser::is_initialised() const
{
    return SUCCEEDED(m_hr);
}

inline cs_bool_t ole_initialiser::operator !() const
{
    return !is_initialised();
}

inline HRESULT ole_initialiser::get_HRESULT() const
{
    return m_hr;
}

/* ////////////////////////////////////////////////////////////////////////// */

#ifndef _COMSTL_NO_NAMESPACE
# ifdef _STLSOFT_NO_NAMESPACE
} // namespace comstl
# else
} // namespace stlsoft::comstl_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_COMSTL_NO_NAMESPACE */

/* ////////////////////////////////////////////////////////////////////////// */

#endif /* !COMSTL_INCL_H_COMSTL_INITIALISERS */

/* ////////////////////////////////////////////////////////////////////////// */
