include/pantheios/util/com/exception_helpers.hpp

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////
00002  * File:        pantheios/util/com/exception_helpers.hpp
00003  *
00004  * Purpose:     Helper functions that invoke a given method and convert any
00005  *              thrown exceptions (that are derived from std::exception) into
00006  *              a panthieos log statement and an HRESULT return code.
00007  *
00008  * Created:     1st May 2006
00009  * Updated:     1st September 2006
00010  *
00011  * Author:      Matthew Wilson
00012  *
00013  * Home:        http://www.pantheios.org/
00014  *
00015  * Copyright:   Matthew Wilson, 2006.
00016  *
00017  * Redistribution and use in source and binary forms, with or without
00018  * modification, are permitted provided that the following conditions are met:
00019  *
00020  * - Redistributions of source code must retain the above copyright notice, this
00021  *   list of conditions and the following disclaimer.
00022  * - Redistributions in binary form must reproduce the above copyright notice,
00023  *   this list of conditions and the following disclaimer in the documentation
00024  *   and/or other materials provided with the distribution.
00025  * - Neither the names of Matthew Wilson and Synesis Software nor the names of
00026  *   any contributors may be used to endorse or promote products derived from
00027  *   this software without specific prior written permission.
00028  *
00029  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00030  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00031  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00032  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00033  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00034  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00035  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00036  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00037  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00038  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00039  * POSSIBILITY OF SUCH DAMAGE.
00040  *
00041  * ////////////////////////////////////////////////////////////////////// */
00042 
00043 
00050 #ifndef PANTHEIOS_INCL_PANTHEIOS_UTIL_COM_HPP_EXCEPTION_HELPERS
00051 #define PANTHEIOS_INCL_PANTHEIOS_UTIL_COM_HPP_EXCEPTION_HELPERS
00052 
00053 /* /////////////////////////////////////////////////////////////////////////
00054  * Version information
00055  */
00056 
00057 #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
00058 # define PANTHEIOS_VER_PANTHEIOS_UTIL_COM_HPP_EXCEPTION_HELPERS_MAJOR       1
00059 # define PANTHEIOS_VER_PANTHEIOS_UTIL_COM_HPP_EXCEPTION_HELPERS_MINOR       0
00060 # define PANTHEIOS_VER_PANTHEIOS_UTIL_COM_HPP_EXCEPTION_HELPERS_REVISION    3
00061 # define PANTHEIOS_VER_PANTHEIOS_UTIL_COM_HPP_EXCEPTION_HELPERS_EDIT        3
00062 #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
00063 
00064 /* /////////////////////////////////////////////////////////////////////////
00065  * Includes
00066  */
00067 
00068 #include <pantheios/pantheios.hpp>
00069 
00070 #include <comstl/comstl.h>
00071 
00072 /* /////////////////////////////////////////////////////////////////////////
00073  * Namespace
00074  */
00075 
00076 #if !defined(PANTHEIOS_NO_NAMESPACE)
00077 namespace pantheios
00078 {
00160 namespace com
00161 {
00162 #endif /* !PANTHEIOS_NO_NAMESPACE */
00163 
00164 /* /////////////////////////////////////////////////////////////////////////
00165  * Functions
00166  */
00167 
00173 template <typename R>
00174 inline R invoke_nothrow(R (STLSOFT_STDCALL *pfn)(), char const *functionName)
00175 {
00176     try
00177     {
00178         HRESULT hr  =   (*pfn)();
00179 
00180         if(E_OUTOFMEMORY == hr)
00181         {
00182             goto out_of_memory;
00183         }
00184 
00185         return hr;
00186     }
00187     catch(std::bad_alloc &)
00188     {
00189         goto out_of_memory;
00190     }
00191     catch(std::exception &x)
00192     {
00193         pantheios::log_ALERT(functionName, ": exception: ", x);
00194 
00195         return E_FAIL;
00196     }
00197     catch(...)
00198     {
00199         pantheios::log_CRITICAL(functionName, ": unexpected exception!");
00200 
00201         return E_UNEXPECTED;
00202     }
00203 
00204 out_of_memory:
00205     pantheios::log_ALERT(functionName, ": out of memory");
00206 
00207     return E_OUTOFMEMORY;
00208 }
00209 
00216 #ifdef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
00217 template <typename R, typename A0>
00218 inline R invoke_nothrow(R (STLSOFT_STDCALL *pfn)(A0), A0 a0, char const *functionName)
00219 #else /* ? PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
00220 template <typename R, typename A0>
00221 inline R invoke_nothrow_1(R (STLSOFT_STDCALL *pfn)(A0), A0 a0, char const *functionName);
00222 template <typename R, typename A0>
00223 inline R invoke_nothrow(R (STLSOFT_STDCALL *pfn)(A0), A0 a0, char const *functionName)
00224 {
00225     return invoke_nothrow_1<R, A0, A1>(pfn, a0, functionName);
00226 }
00227 template <typename R, typename A0>
00228 inline R invoke_nothrow_1(R (STLSOFT_STDCALL *pfn)(A0), A0 a0, char const *functionName)
00229 #endif /* PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
00230 {
00231     try
00232     {
00233         HRESULT hr  =   (*pfn)(a0);
00234 
00235         if(E_OUTOFMEMORY == hr)
00236         {
00237             goto out_of_memory;
00238         }
00239 
00240         return hr;
00241     }
00242     catch(std::bad_alloc &)
00243     {
00244         goto out_of_memory;
00245     }
00246     catch(std::exception &x)
00247     {
00248         pantheios::log_ALERT(functionName, ": exception: ", x);
00249 
00250         return E_FAIL;
00251     }
00252     catch(...)
00253     {
00254         pantheios::log_CRITICAL(functionName, ": unexpected exception!");
00255 
00256         return E_UNEXPECTED;
00257     }
00258 
00259 out_of_memory:
00260     pantheios::log_ALERT(functionName, ": out of memory");
00261 
00262     return E_OUTOFMEMORY;
00263 }
00264 
00272 #ifdef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
00273 template <typename R, typename A0, typename A1>
00274 inline R invoke_nothrow(R (STLSOFT_STDCALL *pfn)(A0, A1), A0 a0, A1 a1, char const *functionName)
00275 #else /* ? PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
00276 template <typename R, typename A0, typename A1>
00277 inline R invoke_nothrow_2(R (STLSOFT_STDCALL *pfn)(A0, A1), A0 a0, A1 a1, char const *functionName);
00278 template <typename R, typename A0, typename A1>
00279 inline R invoke_nothrow(R (STLSOFT_STDCALL *pfn)(A0, A1), A0 a0, A1 a1, char const *functionName)
00280 {
00281     return invoke_nothrow_2<R, A0, A1>(pfn, a0, a1, functionName);
00282 }
00283 template <typename R, typename A0, typename A1>
00284 inline R invoke_nothrow_2(R (STLSOFT_STDCALL *pfn)(A0, A1), A0 a0, A1 a1, char const *functionName)
00285 #endif /* PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
00286 {
00287     try
00288     {
00289         HRESULT hr  =   (*pfn)(a0, a1);
00290 
00291         if(E_OUTOFMEMORY == hr)
00292         {
00293             goto out_of_memory;
00294         }
00295 
00296         return hr;
00297     }
00298     catch(std::bad_alloc &)
00299     {
00300         goto out_of_memory;
00301     }
00302     catch(std::exception &x)
00303     {
00304         pantheios::log_ALERT(functionName, ": exception: ", x);
00305 
00306         return E_FAIL;
00307     }
00308     catch(...)
00309     {
00310         pantheios::log_CRITICAL(functionName, ": unexpected exception!");
00311 
00312         return E_UNEXPECTED;
00313     }
00314 
00315 out_of_memory:
00316     pantheios::log_ALERT(functionName, ": out of memory");
00317 
00318     return E_OUTOFMEMORY;
00319 }
00320 
00329 #ifdef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
00330 template <typename R, typename A0, typename A1, typename A2>
00331 inline R invoke_nothrow(R (STLSOFT_STDCALL *pfn)(A0, A1, A2), A0 a0, A1 a1, A2 a2, char const *functionName)
00332 #else /* ? PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
00333 template <typename R, typename A0, typename A1, typename A2>
00334 inline R invoke_nothrow_3(R (STLSOFT_STDCALL *pfn)(A0, A1, A2), A0 a0, A1 a1, A2 a2, char const *functionName);
00335 template <typename R, typename A0, typename A1, typename A2>
00336 inline R invoke_nothrow(R (STLSOFT_STDCALL *pfn)(A0, A1, A2), A0 a0, A1 a1, A2 a2, char const *functionName)
00337 {
00338     return invoke_nothrow_3<R, A0, A1, A2>(pfn, a0, a1, a2, functionName);
00339 }
00340 template <typename R, typename A0, typename A1, typename A2>
00341 inline R invoke_nothrow_3(R (STLSOFT_STDCALL *pfn)(A0, A1, A2), A0 a0, A1 a1, A2 a2, char const *functionName)
00342 #endif /* PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
00343 {
00344     try
00345     {
00346         HRESULT hr  =   (*pfn)(a0, a1, a2);
00347 
00348         if(E_OUTOFMEMORY == hr)
00349         {
00350             goto out_of_memory;
00351         }
00352 
00353         return hr;
00354     }
00355     catch(std::bad_alloc &)
00356     {
00357         goto out_of_memory;
00358     }
00359     catch(std::exception &x)
00360     {
00361         pantheios::log_ALERT(functionName, ": exception: ", x);
00362 
00363         return E_FAIL;
00364     }
00365     catch(...)
00366     {
00367         pantheios::log_CRITICAL(functionName, ": unexpected exception!");
00368 
00369         return E_UNEXPECTED;
00370     }
00371 
00372 out_of_memory:
00373     pantheios::log_ALERT(functionName, ": out of memory");
00374 
00375     return E_OUTOFMEMORY;
00376 }
00377 
00378 #if 1
00386 template <typename R, typename C, typename A0>
00387 inline R invoke_nothrow_method(C *pThis, R (STLSOFT_STDCALL C::*pfn)(A0), A0 a0, char const *functionName)
00388 {
00389     try
00390     {
00391         HRESULT hr  =   (pThis->*pfn)(a0);
00392 
00393         if(E_OUTOFMEMORY == hr)
00394         {
00395             goto out_of_memory;
00396         }
00397 
00398         return hr;
00399     }
00400     catch(std::bad_alloc &)
00401     {
00402         goto out_of_memory;
00403     }
00404     catch(std::exception &x)
00405     {
00406         pantheios::log_ALERT(functionName, ": exception: ", x);
00407 
00408         return E_FAIL;
00409     }
00410     catch(...)
00411     {
00412         pantheios::log_CRITICAL(functionName, ": unexpected exception!");
00413 
00414         return E_UNEXPECTED;
00415     }
00416 
00417 out_of_memory:
00418     pantheios::log_ALERT(functionName, ": out of memory");
00419 
00420     return E_OUTOFMEMORY;
00421 }
00422 
00431 template <typename R, typename C, typename A0, typename A1>
00432 inline R invoke_nothrow_method(C *pThis, R (STLSOFT_STDCALL C::*pfn)(A0, A1), A0 a0, A1 a1, char const *functionName)
00433 {
00434     try
00435     {
00436         HRESULT hr  =   (pThis->*pfn)(a0, a1);
00437 
00438         if(E_OUTOFMEMORY == hr)
00439         {
00440             goto out_of_memory;
00441         }
00442 
00443         return hr;
00444     }
00445     catch(std::bad_alloc &)
00446     {
00447         goto out_of_memory;
00448     }
00449     catch(std::exception &x)
00450     {
00451         pantheios::log_ALERT(functionName, ": exception: ", x);
00452 
00453         return E_FAIL;
00454     }
00455     catch(...)
00456     {
00457         pantheios::log_CRITICAL(functionName, ": unexpected exception!");
00458 
00459         return E_UNEXPECTED;
00460     }
00461 
00462 out_of_memory:
00463     pantheios::log_ALERT(functionName, ": out of memory");
00464 
00465     return E_OUTOFMEMORY;
00466 }
00467 
00477 template <typename R, typename C, typename A0, typename A1, typename A2>
00478 inline R invoke_nothrow_method(C *pThis, R (STLSOFT_STDCALL C::*pfn)(A0, A1, A2), A0 a0, A1 a1, A2 a2, char const *functionName)
00479 {
00480     try
00481     {
00482         HRESULT hr  =   (pThis->*pfn)(a0, a1, a2);
00483 
00484         if(E_OUTOFMEMORY == hr)
00485         {
00486             goto out_of_memory;
00487         }
00488 
00489         return hr;
00490     }
00491     catch(std::bad_alloc &)
00492     {
00493         goto out_of_memory;
00494     }
00495     catch(std::exception &x)
00496     {
00497         pantheios::log_ALERT(functionName, ": exception: ", x);
00498 
00499         return E_FAIL;
00500     }
00501     catch(...)
00502     {
00503         pantheios::log_CRITICAL(functionName, ": unexpected exception!");
00504 
00505         return E_UNEXPECTED;
00506     }
00507 
00508 out_of_memory:
00509     pantheios::log_ALERT(functionName, ": out of memory");
00510 
00511     return E_OUTOFMEMORY;
00512 }
00513 
00524 template <typename R, typename C, typename A0, typename A1, typename A2, typename A3>
00525 inline R invoke_nothrow_method(C *pThis, R (STLSOFT_STDCALL C::*pfn)(A0, A1, A2, A3), A0 a0, A1 a1, A2 a2, A3 a3, char const *functionName)
00526 {
00527     try
00528     {
00529         HRESULT hr  =   (pThis->*pfn)(a0, a1, a2, a3);
00530 
00531         if(E_OUTOFMEMORY == hr)
00532         {
00533             goto out_of_memory;
00534         }
00535 
00536         return hr;
00537     }
00538     catch(std::bad_alloc &)
00539     {
00540         goto out_of_memory;
00541     }
00542     catch(std::exception &x)
00543     {
00544         pantheios::log_ALERT(functionName, ": exception: ", x);
00545 
00546         return E_FAIL;
00547     }
00548     catch(...)
00549     {
00550         pantheios::log_CRITICAL(functionName, ": unexpected exception!");
00551 
00552         return E_UNEXPECTED;
00553     }
00554 
00555 out_of_memory:
00556     pantheios::log_ALERT(functionName, ": out of memory");
00557 
00558     return E_OUTOFMEMORY;
00559 }
00560 
00572 template <typename R, typename C, typename A0, typename A1, typename A2, typename A3, typename A4>
00573 inline R invoke_nothrow_method(C *pThis, R (STLSOFT_STDCALL C::*pfn)(A0, A1, A2, A3, A4), A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, char const *functionName)
00574 {
00575     try
00576     {
00577         HRESULT hr  =   (pThis->*pfn)(a0, a1, a2, a3, a4);
00578 
00579         if(E_OUTOFMEMORY == hr)
00580         {
00581             goto out_of_memory;
00582         }
00583 
00584         return hr;
00585     }
00586     catch(std::bad_alloc &)
00587     {
00588         goto out_of_memory;
00589     }
00590     catch(std::exception &x)
00591     {
00592         pantheios::log_ALERT(functionName, ": exception: ", x);
00593 
00594         return E_FAIL;
00595     }
00596     catch(...)
00597     {
00598         pantheios::log_CRITICAL(functionName, ": unexpected exception!");
00599 
00600         return E_UNEXPECTED;
00601     }
00602 
00603 out_of_memory:
00604     pantheios::log_ALERT(functionName, ": out of memory");
00605 
00606     return E_OUTOFMEMORY;
00607 }
00608 #endif /* 0 */
00609 
00610 /* /////////////////////////////////////////////////////////////////////////
00611  * Namespace
00612  */
00613 
00614 #if !defined(PANTHEIOS_NO_NAMESPACE)
00615 
00616 } /* namespace com */
00617 
00618 } /* namespace pantheios */
00619 #endif /* !PANTHEIOS_NO_NAMESPACE */
00620 
00621 /* ////////////////////////////////////////////////////////////////////// */
00622 
00623 #endif /* !PANTHEIOS_INCL_PANTHEIOS_UTIL_COM_HPP_EXCEPTION_HELPERS */
00624 
00625 /* ////////////////////////////////////////////////////////////////////// */

pantheios Library documentation © Matthew Wilson, 2006 SourceForge.net Logo