OSDN Git Service

2007-12-27 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / system_error
1 // <system_error> -*- C++ -*-
2
3 // Copyright (C) 2007 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library 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 library; see the file COPYING.  If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /** @file include/system_error
31  *  This is a Standard C++ Library header.
32  */
33
34 #ifndef _GLIBCXX_SYSTEM_ERROR
35 #define _GLIBCXX_SYSTEM_ERROR 1
36
37 #pragma GCC system_header
38
39 #ifndef __GXX_EXPERIMENTAL_CXX0X__
40 # include <c++0x_warning.h>
41 #endif
42
43 #include <bits/c++config.h>
44 #include <bits/error_constants.h>
45 #include <iosfwd>
46 #include <stdexcept>
47
48 _GLIBCXX_BEGIN_NAMESPACE(std)
49
50   class system_error;
51   class error_code;
52   class error_category;
53
54   extern const error_category& system_category;
55
56   struct error_category
57   {
58     error_category() { }
59
60     bool 
61     operator==(const error_category& __other) const
62     { return this == &__other; }
63
64     bool 
65     operator!=(const error_category& __other) const
66     { return this != &__other; }
67
68     virtual posix_error::posix_errno 
69     posix(int __v) const = 0;
70
71     virtual const string& 
72     name() const = 0;
73
74   private:
75     error_category(const error_category&);
76
77     error_category& 
78     operator=(const error_category&);
79   };
80
81   struct error_code
82   {
83     error_code() throw()
84     : _M_value(0), _M_cat(&system_category) { }
85
86     error_code(int __v, const error_category& __cat) throw()
87     : _M_value(__v), _M_cat(&__cat) { }
88
89     error_code(posix_error::posix_errno __v)
90     : _M_value(__v), _M_cat(&system_category) { }
91
92     void 
93     assign(int __v, const error_category& __cat) throw()
94     {
95       _M_value = __v;
96       _M_cat = &__cat; 
97     }
98
99     void 
100     clear() throw()
101     { 
102       _M_value = 0;
103       _M_cat = &system_category;
104    } 
105
106     int
107     value() const throw() { return _M_value; }
108       
109     const error_category&  
110     category() const { return *_M_cat; }
111
112     posix_error::posix_errno            
113     posix() const throw() { return this->category().posix(_M_value); }
114
115     // Safe bool idiom.
116     // explicit operator bool() const throw()
117     // { return _M_value != 0; }
118     typedef void (*__bool_type)();
119
120     static void __not_bool_type() { }
121
122     operator __bool_type() const throw()
123     { return _M_value != 0 ? &__not_bool_type : false; }
124
125     bool operator==(const error_code& __other) const
126     { return value() == __other.value() && category() == __other.category(); }
127
128     bool operator!=(const error_code& __other) const
129     { return !(this == &__other); }
130
131   private:
132     int                         _M_value;
133     const error_category*       _M_cat;
134   };
135
136   class system_error : public std::runtime_error
137   {
138   private:
139     error_code  _M_code;
140
141   public:
142     system_error(const string& __what, error_code __ec = error_code())
143     : runtime_error(__what), _M_code(__ec) { }
144
145     system_error(const string& __what, int __v, const error_category& __ecat)
146     : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
147
148     virtual ~system_error() throw();
149
150     const error_code& 
151     code() const throw() { return _M_code; }
152   };
153
154 _GLIBCXX_END_NAMESPACE
155
156 #endif 
157