OSDN Git Service

2008-04-10 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / system_error
1 // <system_error> -*- C++ -*-
2
3 // Copyright (C) 2007, 2008 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 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   /// error_category
57   struct error_category
58   {
59     error_category() { }
60
61     bool 
62     operator==(const error_category& __other) const
63     { return this == &__other; }
64
65     bool 
66     operator!=(const error_category& __other) const
67     { return this != &__other; }
68
69     virtual posix_error::posix_errno 
70     posix(int __v) const = 0;
71
72     virtual const string& 
73     name() const = 0;
74
75   private:
76     error_category(const error_category&);
77
78     error_category& 
79     operator=(const error_category&);
80   };
81
82   /// error_code
83   struct error_code
84   {
85     error_code() throw()
86     : _M_value(0), _M_cat(&system_category) { }
87
88     error_code(int __v, const error_category& __cat) throw()
89     : _M_value(__v), _M_cat(&__cat) { }
90
91     error_code(posix_error::posix_errno __v)
92     : _M_value(__v), _M_cat(&system_category) { }
93
94     void 
95     assign(int __v, const error_category& __cat) throw()
96     {
97       _M_value = __v;
98       _M_cat = &__cat; 
99     }
100
101     void 
102     clear() throw()
103     { 
104       _M_value = 0;
105       _M_cat = &system_category;
106    } 
107
108     int
109     value() const throw() { return _M_value; }
110       
111     const error_category&  
112     category() const { return *_M_cat; }
113
114     posix_error::posix_errno            
115     posix() const throw() { return this->category().posix(_M_value); }
116
117     // Safe bool idiom.
118     // explicit operator bool() const throw()
119     // { return _M_value != 0; }
120     typedef void (*__bool_type)();
121
122     static void __not_bool_type() { }
123
124     operator __bool_type() const throw()
125     { return _M_value != 0 ? &__not_bool_type : false; }
126
127     bool operator==(const error_code& __other) const
128     { return value() == __other.value() && category() == __other.category(); }
129
130     bool operator!=(const error_code& __other) const
131     { return !(this == &__other); }
132
133   private:
134     int                         _M_value;
135     const error_category*       _M_cat;
136   };
137
138   /// Thrown to indicate error code of underlying system.
139   class system_error : public std::runtime_error
140   {
141   private:
142     error_code  _M_code;
143
144   public:
145     system_error(error_code __ec = error_code())
146     : runtime_error(""), _M_code(__ec) { }
147
148     system_error(const string& __what, error_code __ec = error_code())
149     : runtime_error(__what), _M_code(__ec) { }
150
151     system_error(const string& __what, int __v, const error_category& __ecat)
152     : runtime_error(__what), _M_code(error_code(__v, __ecat)) { }
153
154     virtual ~system_error() throw();
155
156     const error_code& 
157     code() const throw() { return _M_code; }
158   };
159
160 _GLIBCXX_END_NAMESPACE
161
162 #endif 
163