~ubuntu-branches/ubuntu/trusty/advancecomp/trusty

« back to all changes in this revision

Viewing changes to except.h

  • Committer: Bazaar Package Importer
  • Author(s): Piotr Ozarowski
  • Date: 2006-05-13 21:15:49 UTC
  • Revision ID: james.westby@ubuntu.com-20060513211549-2vu7peis643ojcm5
Tags: upstream-1.15
ImportĀ upstreamĀ versionĀ 1.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the Advance project.
 
3
 *
 
4
 * Copyright (C) 2002 Andrea Mazzoleni
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details. 
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 */
 
20
 
 
21
#ifndef __EXCEPT_H
 
22
#define __EXCEPT_H
 
23
 
 
24
#include <string>
 
25
#include <iostream>
 
26
#include <sstream>
 
27
#include <iomanip>
 
28
 
 
29
class error {
 
30
        std::string function;
 
31
        std::string file;
 
32
        unsigned line;
 
33
        std::string desc;
 
34
public:
 
35
        error() : line(0)
 
36
        {
 
37
        }
 
38
 
 
39
        error(const char* Afunction, const char* Afile, unsigned Aline) : function(Afunction), file(Afile), line(Aline)
 
40
        {
 
41
        }
 
42
 
 
43
        const std::string& desc_get() const
 
44
        {
 
45
                return desc;
 
46
        }
 
47
 
 
48
        const std::string& function_get() const
 
49
        {
 
50
                return function;
 
51
        }
 
52
 
 
53
        const std::string& file_get() const
 
54
        {
 
55
                return file;
 
56
        }
 
57
 
 
58
        unsigned line_get() const
 
59
        {
 
60
                return line;
 
61
        }
 
62
 
 
63
        error& operator<<(const char* A)
 
64
        {
 
65
                desc += A;
 
66
                return *this;
 
67
        }
 
68
 
 
69
        error& operator<<(const std::string& A)
 
70
        {
 
71
                desc += A;
 
72
                return *this;
 
73
        }
 
74
 
 
75
        error& operator<<(const unsigned A)
 
76
        {
 
77
                std::ostringstream s;
 
78
                s << A /* << " (" << std::hex << A << "h)" */ ;
 
79
                desc += s.str();
 
80
                return *this;
 
81
        }
 
82
};
 
83
 
 
84
class error_invalid : public error {
 
85
public:
 
86
        error_invalid() : error()
 
87
        {
 
88
        }
 
89
 
 
90
        error_invalid& operator<<(const char* A)
 
91
        {
 
92
                error::operator<<(A);
 
93
                return *this;
 
94
        }
 
95
 
 
96
        error_invalid& operator<<(const std::string& A)
 
97
        {
 
98
                error::operator<<(A);
 
99
                return *this;
 
100
        }
 
101
 
 
102
        error_invalid& operator<<(const unsigned A)
 
103
        {
 
104
                error::operator<<(A);
 
105
                return *this;
 
106
        }
 
107
};
 
108
 
 
109
class error_unsupported : public error {
 
110
public:
 
111
        error_unsupported() : error()
 
112
        {
 
113
        }
 
114
 
 
115
        error_unsupported& operator<<(const char* A)
 
116
        {
 
117
                error::operator<<(A);
 
118
                return *this;
 
119
        }
 
120
 
 
121
        error_unsupported& operator<<(const std::string& A)
 
122
        {
 
123
                error::operator<<(A);
 
124
                return *this;
 
125
        }
 
126
 
 
127
        error_unsupported& operator<<(const unsigned A)
 
128
        {
 
129
                error::operator<<(A);
 
130
                return *this;
 
131
        }
 
132
};
 
133
 
 
134
#define error() \
 
135
        error(__PRETTY_FUNCTION__, __FILE__, __LINE__)
 
136
 
 
137
static inline std::ostream& operator<<(std::ostream& os, const error& e)
 
138
{
 
139
        os << e.desc_get();
 
140
 
 
141
        if (e.function_get().length() || e.file_get().length() || e.line_get())
 
142
                os << " [at " << e.function_get() << ":" << e.file_get() << ":" << e.line_get() << "]";
 
143
 
 
144
        return os;
 
145
}
 
146
 
 
147
#endif
 
148