summaryrefslogtreecommitdiff
path: root/print/XpsRasFilter/src/Exception.h
blob: 887bfbc4139a593575c81f5592b5051224c7a478 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved
//
// File Name:
//
//    Exception.h
//
// Abstract:
//
//    Exception macro and class declarations.
//

#pragma once

//
// Macro to convert HRESULT into an exception throw
//
#ifndef THROW_ON_FAILED_HRESULT
#define THROW_ON_FAILED_HRESULT(func_)                                              \
{                                                                                   \
    HRESULT hr_ = func_;                                                            \
    if (FAILED(hr_)) { xpsrasfilter::ThrowHRException(hr_, __FILE__, __LINE__); }   \
} 
#endif // THROW_ON_FAILED_HRESULT

#ifndef THROW_LAST_ERROR
#define THROW_LAST_ERROR()                                      \
{                                                               \
        HRESULT errhr_ = HRESULT_FROM_WIN32(::GetLastError());  \
        THROW_ON_FAILED_HRESULT(errhr_);                        \
}
#endif // THROW_LAST_ERROR

//
// Macro to catch various exceptions, including
// HRESULT-turned-exceptions.
//
// Because we have defined USE_NATIVE_EH=1, the
// catch(...) block will not catch structural
// exceptions.
//
#ifndef CATCH_VARIOUS
#define CATCH_VARIOUS(hr_)                  \
    catch(std::bad_alloc const& )           \
    {                                       \
        hr_ = E_OUTOFMEMORY;                \
    }                                       \
    catch(xpsrasfilter::hr_error const& e)  \
    {                                       \
        hr_ = e.hr;                         \
    }                                       \
    catch(std::exception const& )           \
    {                                       \
        hr_ = E_FAIL;                       \
    }                                       \
    catch(...)                              \
    {                                       \
        hr_ = E_UNEXPECTED;                 \
    }
#endif // CATCH_VARIOUS

namespace xpsrasfilter
{

//
// HRESULT exception
//
struct hr_error
{
    HRESULT hr;

    hr_error(HRESULT hr_in) : hr(hr_in)
    { }
};

void ThrowHRException(
    HRESULT hr,
    char const *fileName,
    int lineNum
    );

} // namespace xpsrasfilter