Statistics
| Branch: | Tag: | Revision:

root / host / include / uhd / exception.hpp @ 09ed8e69

History | View | Annotate | Download (5.91 KB)

1
//
2
// Copyright 2010-2011 Ettus Research LLC
3
//
4
// This program is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

    
18
#ifndef INCLUDED_UHD_UTILS_EXCEPTION_HPP
19
#define INCLUDED_UHD_UTILS_EXCEPTION_HPP
20

    
21
#include <uhd/config.hpp>
22
#include <boost/current_function.hpp>
23
#include <stdexcept>
24
#include <string>
25

    
26
/*!
27
 * Define common exceptions used throughout the code:
28
 *
29
 * - The python built-in exceptions were used as inspiration.
30
 * - Exceptions inherit from std::exception to provide what().
31
 * - Exceptions inherit from uhd::exception to provide code().
32
 *
33
 * The code() provides an error code which allows the application
34
 * the option of printing a cryptic error message from the 1990s.
35
 *
36
 * The dynamic_clone() and dynamic_throw() methods allow us to:
37
 * catch an exception by dynamic type (i.e. derived class), save it,
38
 * and later rethrow it, knowing only the static type (i.e. base class),
39
 * and then finally to catch it again using the derived type.
40
 *
41
 * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2106.html
42
 */
43
namespace uhd{
44

    
45
    struct UHD_API exception : std::runtime_error{
46
        exception(const std::string &what);
47
        virtual unsigned code(void) const = 0;
48
        virtual exception *dynamic_clone(void) const = 0;
49
        virtual void dynamic_throw(void) const = 0;
50
    };
51

    
52
    struct UHD_API assertion_error : exception{
53
        assertion_error(const std::string &what);
54
        virtual unsigned code(void) const;
55
        virtual assertion_error *dynamic_clone(void) const;
56
        virtual void dynamic_throw(void) const;
57
    };
58

    
59
    struct UHD_API lookup_error : exception{
60
        lookup_error(const std::string &what);
61
        virtual unsigned code(void) const;
62
        virtual lookup_error *dynamic_clone(void) const;
63
        virtual void dynamic_throw(void) const;
64
    };
65

    
66
    struct UHD_API index_error : lookup_error{
67
        index_error(const std::string &what);
68
        virtual unsigned code(void) const;
69
        virtual index_error *dynamic_clone(void) const;
70
        virtual void dynamic_throw(void) const;
71
    };
72

    
73
    struct UHD_API key_error : lookup_error{
74
        key_error(const std::string &what);
75
        virtual unsigned code(void) const;
76
        virtual key_error *dynamic_clone(void) const;
77
        virtual void dynamic_throw(void) const;
78
    };
79

    
80
    struct UHD_API type_error : exception{
81
        type_error(const std::string &what);
82
        virtual unsigned code(void) const;
83
        virtual type_error *dynamic_clone(void) const;
84
        virtual void dynamic_throw(void) const;
85
    };
86

    
87
    struct UHD_API value_error : exception{
88
        value_error(const std::string &what);
89
        virtual unsigned code(void) const;
90
        virtual value_error *dynamic_clone(void) const;
91
        virtual void dynamic_throw(void) const;
92
    };
93

    
94
    struct UHD_API runtime_error : exception{
95
        runtime_error(const std::string &what);
96
        virtual unsigned code(void) const;
97
        virtual runtime_error *dynamic_clone(void) const;
98
        virtual void dynamic_throw(void) const;
99
    };
100

    
101
    struct UHD_API not_implemented_error : runtime_error{
102
        not_implemented_error(const std::string &what);
103
        virtual unsigned code(void) const;
104
        virtual not_implemented_error *dynamic_clone(void) const;
105
        virtual void dynamic_throw(void) const;
106
    };
107

    
108
    struct UHD_API environment_error : exception{
109
        environment_error(const std::string &what);
110
        virtual unsigned code(void) const;
111
        virtual environment_error *dynamic_clone(void) const;
112
        virtual void dynamic_throw(void) const;
113
    };
114

    
115
    struct UHD_API io_error : environment_error{
116
        io_error(const std::string &what);
117
        virtual unsigned code(void) const;
118
        virtual io_error *dynamic_clone(void) const;
119
        virtual void dynamic_throw(void) const;
120
    };
121

    
122
    struct UHD_API os_error : environment_error{
123
        os_error(const std::string &what);
124
        virtual unsigned code(void) const;
125
        virtual os_error *dynamic_clone(void) const;
126
        virtual void dynamic_throw(void) const;
127
    };
128

    
129
    struct UHD_API system_error : exception{
130
        system_error(const std::string &what);
131
        virtual unsigned code(void) const;
132
        virtual system_error *dynamic_clone(void) const;
133
        virtual void dynamic_throw(void) const;
134
    };
135

    
136
    /*!
137
     * Create a formated string with throw-site information.
138
     * Fills in the function name, file name, and line number.
139
     * \param what the std::exeption message
140
     * \return the formatted exception message
141
     */
142
    #define UHD_THROW_SITE_INFO(what) std::string( \
143
        std::string(what) + "\n" + \
144
        "  in " + std::string(BOOST_CURRENT_FUNCTION) + "\n" + \
145
        "  at " + std::string(__FILE__) + ":" + BOOST_STRINGIZE(__LINE__) + "\n" \
146
    )
147

    
148
    /*!
149
     * Throws an invalid code path exception with throw-site information.
150
     * Use this macro in places that code execution is not supposed to go.
151
     */
152
    #define UHD_THROW_INVALID_CODE_PATH() \
153
        throw uhd::system_error(UHD_THROW_SITE_INFO("invalid code path"))
154

    
155
    /*!
156
     * Assert the result of the code evaluation.
157
     * If the code evaluates to false, throw an assertion error.
158
     * \param code the code that resolved to a boolean
159
     */
160
    #define UHD_ASSERT_THROW(code) if (not (code)) \
161
        throw uhd::assertion_error(UHD_THROW_SITE_INFO(#code)); \
162
    else void(0)
163

    
164
} //namespace uhd
165

    
166
#endif /* INCLUDED_UHD_UTILS_EXCEPTION_HPP */