00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00050 #ifndef COMSTL_INCL_COMSTL_UTIL_HPP_INITIALISERS
00051 #define COMSTL_INCL_COMSTL_UTIL_HPP_INITIALISERS
00052
00053 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00054 # define COMSTL_VER_COMSTL_UTIL_HPP_INITIALISERS_MAJOR 3
00055 # define COMSTL_VER_COMSTL_UTIL_HPP_INITIALISERS_MINOR 3
00056 # define COMSTL_VER_COMSTL_UTIL_HPP_INITIALISERS_REVISION 2
00057 # define COMSTL_VER_COMSTL_UTIL_HPP_INITIALISERS_EDIT 80
00058 #endif
00059
00060
00061
00062
00063
00064 #ifndef COMSTL_INCL_COMSTL_H_COMSTL
00065 # include <comstl/comstl.h>
00066 #endif
00067 #ifndef COMSTL_INCL_COMSTL_ERROR_HPP_EXCEPTIONS
00068 # include <comstl/error/exceptions.hpp>
00069 #endif
00070 #ifndef STLSOFT_INCL_STLSOFT_ERROR_HPP_THROW_POLICIES
00071 # include <stlsoft/error/throw_policies.hpp>
00072 #endif
00073 #ifndef STLSOFT_INCL_H_OLE2
00074 # define STLSOFT_INCL_H_OLE2
00075 # include <ole2.h>
00076 #endif
00077
00078
00079
00080
00081
00082 #ifndef _COMSTL_NO_NAMESPACE
00083 # if defined(_STLSOFT_NO_NAMESPACE) || \
00084 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00085
00086 namespace comstl
00087 {
00088 # else
00089
00090
00091 namespace stlsoft
00092 {
00093
00094 namespace comstl_project
00095 {
00096
00097 # endif
00098 #endif
00099
00100
00101
00102
00103
00108 class com_initialisation_exception
00109 : public com_exception
00110 {
00113 public:
00114 typedef com_exception parent_class_type;
00115 typedef com_initialisation_exception class_type;
00117
00120 public:
00121 ss_explicit_k com_initialisation_exception(HRESULT hr)
00122 : parent_class_type(hr)
00123 {}
00124 com_initialisation_exception(char const* reason, HRESULT hr)
00125 : parent_class_type(reason, hr)
00126 {}
00128
00131 private:
00132 virtual char const* real_what_() const throw()
00133 {
00134 return "COM initialisation failure";
00135 }
00137 };
00138
00140
00141
00146
00147 struct com_initialisation_exception_policy
00148 {
00151 public:
00153 typedef com_initialisation_exception thrown_type;
00155
00158 public:
00162 void operator ()(HRESULT hr)
00163 {
00164 STLSOFT_THROW_X(com_initialisation_exception(hr));
00165 }
00167 };
00168
00173 typedef stlsoft_ns_qual(null_exception_policy) ignore_initialisation_exception_policy;
00174
00175
00176
00177
00178
00232 template< ss_typename_param_k IP
00233 , ss_typename_param_k XP
00234 >
00235 class initialiser
00236 {
00239 private:
00240 typedef initialiser class_type;
00241 public:
00243 typedef IP initialisation_policy_type;
00245 typedef XP exception_policy_type;
00247 typedef ss_typename_type_k exception_policy_type::thrown_type thrown_type;
00249
00252 public:
00254 initialiser();
00255 #ifdef __COMSTL_CF_DCOM_SUPPORT
00257 ss_explicit_k initialiser(cs_dword_t dwInit );
00258 #endif
00260 ~initialiser() stlsoft_throw_0();
00262
00265 public:
00267 cs_bool_t is_initialised() const;
00269 cs_bool_t operator !() const;
00271 HRESULT get_HRESULT() const;
00273
00276 private:
00277 HRESULT const m_hr;
00279
00282 private:
00283 initialiser(class_type const& rhs);
00284 class_type const& operator =(class_type const& rhs);
00286 };
00287
00289
00290
00300 struct CoInitialize_policy
00301 {
00302 public:
00303 static HRESULT init()
00304 {
00305 return ::CoInitialize(NULL);
00306 }
00307 #ifdef __COMSTL_CF_DCOM_SUPPORT
00308 static HRESULT init(cs_dword_t coInit)
00309 {
00310 return ::CoInitializeEx(NULL, coInit);
00311 }
00312 #endif
00313 static void uninit()
00314 {
00315 ::CoUninitialize();
00316 }
00317 };
00318
00327 struct OleInitialize_policy
00328 {
00329 public:
00330 static HRESULT init()
00331 {
00332 return ::OleInitialize(NULL);
00333 }
00334 static void uninit()
00335 {
00336 ::OleUninitialize();
00337 }
00338 };
00339
00341
00342
00349 typedef initialiser<CoInitialize_policy, ignore_initialisation_exception_policy> com_init_nothrow;
00356 typedef initialiser<OleInitialize_policy, ignore_initialisation_exception_policy> ole_init_nothrow;
00357
00364 typedef initialiser<CoInitialize_policy, com_initialisation_exception_policy> com_init;
00371 typedef initialiser<OleInitialize_policy, com_initialisation_exception_policy> ole_init;
00372
00381 typedef com_init_nothrow com_initialiser;
00390 typedef ole_init_nothrow ole_initialiser;
00391
00393
00394
00399 typedef com_initialiser com_initializer;
00404 typedef ole_initialiser ole_initializer;
00405
00407
00408
00409 #ifdef STLSOFT_UNITTEST
00410 # include "./unittest/initialisers_unittest_.h"
00411 #endif
00412
00414
00415
00416
00417
00418 template< ss_typename_param_k IP
00419 , ss_typename_param_k XP
00420 >
00421 inline initialiser<IP, XP>::initialiser()
00422 : m_hr(initialisation_policy_type::init())
00423 {
00424 if(FAILED(m_hr))
00425 {
00426 exception_policy_type xp;
00427
00428 xp(m_hr);
00429 }
00430 }
00431
00432 #ifdef __COMSTL_CF_DCOM_SUPPORT
00433 template< ss_typename_param_k IP
00434 , ss_typename_param_k XP
00435 >
00436 inline initialiser<IP, XP>::initialiser(cs_dword_t coInit)
00437 : m_hr(initialisation_policy_type::init(coInit))
00438 {
00439 if(FAILED(m_hr))
00440 {
00441 exception_policy_type xp;
00442
00443 xp(m_hr);
00444 }
00445 }
00446 #endif // __COMSTL_CF_DCOM_SUPPORT
00447
00448 template< ss_typename_param_k IP
00449 , ss_typename_param_k XP
00450 >
00451 inline initialiser<IP, XP>::~initialiser() stlsoft_throw_0()
00452 {
00453 if(is_initialised())
00454 {
00455 initialisation_policy_type::uninit();
00456 }
00457 }
00458
00459 template< ss_typename_param_k IP
00460 , ss_typename_param_k XP
00461 >
00462 inline cs_bool_t initialiser<IP, XP>::is_initialised() const
00463 {
00464 return SUCCEEDED(m_hr);
00465 }
00466
00467 template< ss_typename_param_k IP
00468 , ss_typename_param_k XP
00469 >
00470 inline cs_bool_t initialiser<IP, XP>::operator !() const
00471 {
00472 return !is_initialised();
00473 }
00474
00475 template< ss_typename_param_k IP
00476 , ss_typename_param_k XP
00477 >
00478 inline HRESULT initialiser<IP, XP>::get_HRESULT() const
00479 {
00480 return m_hr;
00481 }
00482
00483
00484
00485 #ifndef _COMSTL_NO_NAMESPACE
00486 # if defined(_STLSOFT_NO_NAMESPACE) || \
00487 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00488 }
00489 # else
00490 }
00491 }
00492 # endif
00493 #endif
00494
00495
00496
00497 #endif
00498
00499