fix doc example typo
[boost.git] / boost / interprocess / errors.hpp
blobcdd4208906cad2295ff0b0162979239ef256fbc8
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-2008. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 // Parts of this code are taken from boost::filesystem library
11 //////////////////////////////////////////////////////////////////////////////
13 // Copyright (C) 2002 Beman Dawes
14 // Copyright (C) 2001 Dietmar Kuehl
15 // Use, modification, and distribution is subject to the Boost Software
16 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy
17 // at http://www.boost.org/LICENSE_1_0.txt)
19 // See library home page at http://www.boost.org/libs/filesystem
21 //////////////////////////////////////////////////////////////////////////////
24 #ifndef BOOST_INTERPROCESS_ERRORS_HPP
25 #define BOOST_INTERPROCESS_ERRORS_HPP
27 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
28 # pragma once
29 #endif
31 #include <boost/interprocess/detail/config_begin.hpp>
32 #include <boost/interprocess/detail/workaround.hpp>
33 #include <stdarg.h>
34 #include <string>
36 #if (defined BOOST_INTERPROCESS_WINDOWS)
37 # include <boost/interprocess/detail/win32_api.hpp>
38 #else
39 # ifdef BOOST_HAS_UNISTD_H
40 # include <errno.h> //Errors
41 # include <cstring> //strerror
42 # else //ifdef BOOST_HAS_UNISTD_H
43 # error Unknown platform
44 # endif //ifdef BOOST_HAS_UNISTD_H
45 #endif //#if (defined BOOST_INTERPROCESS_WINDOWS)
47 //!\file
48 //!Describes the error numbering of interprocess classes
50 namespace boost {
51 namespace interprocess {
52 /// @cond
53 static inline int system_error_code() // artifact of POSIX and WINDOWS error reporting
55 #if (defined BOOST_INTERPROCESS_WINDOWS)
56 return winapi::get_last_error();
57 #else
58 return errno; // GCC 3.1 won't accept ::errno
59 #endif
63 #if (defined BOOST_INTERPROCESS_WINDOWS)
64 inline void fill_system_message(int sys_err_code, std::string &str)
66 void *lpMsgBuf;
67 winapi::format_message(
68 winapi::format_message_allocate_buffer |
69 winapi::format_message_from_system |
70 winapi::format_message_ignore_inserts,
72 sys_err_code,
73 winapi::make_lang_id(winapi::lang_neutral, winapi::sublang_default), // Default language
74 reinterpret_cast<char *>(&lpMsgBuf),
78 str += static_cast<const char*>(lpMsgBuf);
79 winapi::local_free( lpMsgBuf ); // free the buffer
80 while ( str.size()
81 && (str[str.size()-1] == '\n' || str[str.size()-1] == '\r') )
82 str.erase( str.size()-1 );
84 # else
85 static inline void fill_system_message( int system_error, std::string &str)
86 { str = std::strerror(system_error); }
87 # endif
88 /// @endcond
90 enum error_code_t
92 no_error = 0,
93 system_error, // system generated error; if possible, is translated
94 // to one of the more specific errors below.
95 other_error, // library generated error
96 security_error, // includes access rights, permissions failures
97 read_only_error,
98 io_error,
99 path_error,
100 not_found_error,
101 // not_directory_error,
102 busy_error, // implies trying again might succeed
103 already_exists_error,
104 not_empty_error,
105 is_directory_error,
106 out_of_space_error,
107 out_of_memory_error,
108 out_of_resource_error,
109 lock_error,
110 sem_error,
111 mode_error,
112 size_error,
113 corrupted_error,
114 not_such_file_or_directory,
115 invalid_argument
118 typedef int native_error_t;
120 /// @cond
121 struct ec_xlate
123 native_error_t sys_ec;
124 error_code_t ec;
127 static const ec_xlate ec_table[] =
129 #if (defined BOOST_INTERPROCESS_WINDOWS)
130 { /*ERROR_ACCESS_DENIED*/5L, security_error },
131 { /*ERROR_INVALID_ACCESS*/12L, security_error },
132 { /*ERROR_SHARING_VIOLATION*/32L, security_error },
133 { /*ERROR_LOCK_VIOLATION*/33L, security_error },
134 { /*ERROR_LOCKED*/212L, security_error },
135 { /*ERROR_NOACCESS*/998L, security_error },
136 { /*ERROR_WRITE_PROTECT*/19L, read_only_error },
137 { /*ERROR_NOT_READY*/21L, io_error },
138 { /*ERROR_SEEK*/25L, io_error },
139 { /*ERROR_READ_FAULT*/30L, io_error },
140 { /*ERROR_WRITE_FAULT*/29L, io_error },
141 { /*ERROR_CANTOPEN*/1011L, io_error },
142 { /*ERROR_CANTREAD*/1012L, io_error },
143 { /*ERROR_CANTWRITE*/1013L, io_error },
144 { /*ERROR_DIRECTORY*/267L, path_error },
145 { /*ERROR_INVALID_NAME*/123L, path_error },
146 { /*ERROR_FILE_NOT_FOUND*/2L, not_found_error },
147 { /*ERROR_PATH_NOT_FOUND*/3L, not_found_error },
148 { /*ERROR_DEV_NOT_EXIST*/55L, not_found_error },
149 { /*ERROR_DEVICE_IN_USE*/2404L, busy_error },
150 { /*ERROR_OPEN_FILES*/2401L, busy_error },
151 { /*ERROR_BUSY_DRIVE*/142L, busy_error },
152 { /*ERROR_BUSY*/170L, busy_error },
153 { /*ERROR_FILE_EXISTS*/80L, already_exists_error },
154 { /*ERROR_ALREADY_EXISTS*/183L, already_exists_error },
155 { /*ERROR_DIR_NOT_EMPTY*/145L, not_empty_error },
156 { /*ERROR_HANDLE_DISK_FULL*/39L, out_of_space_error },
157 { /*ERROR_DISK_FULL*/112L, out_of_space_error },
158 { /*ERROR_OUTOFMEMORY*/14L, out_of_memory_error },
159 { /*ERROR_NOT_ENOUGH_MEMORY*/8L, out_of_memory_error },
160 { /*ERROR_TOO_MANY_OPEN_FILES*/4L, out_of_resource_error }
161 #else //#if (defined BOOST_INTERPROCESS_WINDOWS)
162 { EACCES, security_error },
163 { EROFS, read_only_error },
164 { EIO, io_error },
165 { ENAMETOOLONG, path_error },
166 { ENOENT, not_found_error },
167 // { ENOTDIR, not_directory_error },
168 { EAGAIN, busy_error },
169 { EBUSY, busy_error },
170 { ETXTBSY, busy_error },
171 { EEXIST, already_exists_error },
172 { ENOTEMPTY, not_empty_error },
173 { EISDIR, is_directory_error },
174 { ENOSPC, out_of_space_error },
175 { ENOMEM, out_of_memory_error },
176 { EMFILE, out_of_resource_error },
177 { ENOENT, not_such_file_or_directory },
178 { EINVAL, invalid_argument }
179 #endif //#if (defined BOOST_INTERPROCESS_WINDOWS)
182 static inline error_code_t lookup_error(native_error_t err)
184 const ec_xlate *cur = &ec_table[0],
185 *end = cur + sizeof(ec_table)/sizeof(ec_xlate);
186 for (;cur != end; ++cur ){
187 if ( err == cur->sys_ec ) return cur->ec;
189 return system_error; // general system error code
192 struct error_info
194 error_info(error_code_t ec = other_error )
195 : m_nat(0), m_ec(ec)
198 error_info(native_error_t sys_err_code)
199 : m_nat(sys_err_code), m_ec(lookup_error(sys_err_code))
202 error_info & operator =(error_code_t ec)
204 m_nat = 0;
205 m_ec = ec;
206 return *this;
209 error_info & operator =(native_error_t sys_err_code)
211 m_nat = sys_err_code;
212 m_ec = lookup_error(sys_err_code);
213 return *this;
216 native_error_t get_native_error()const
217 { return m_nat; }
219 error_code_t get_error_code()const
220 { return m_ec; }
222 private:
223 native_error_t m_nat;
224 error_code_t m_ec;
226 /// @endcond
228 } // namespace interprocess {
229 } // namespace boost
231 #include <boost/interprocess/detail/config_end.hpp>
233 #endif // BOOST_INTERPROCESS_ERRORS_HPP