22#ifndef LELY_UTIL_C_TYPE_HPP_
23#define LELY_UTIL_C_TYPE_HPP_
77 constexpr delete_c_type()
noexcept =
default;
79 delete_c_type(
const delete_c_type<U>&)
noexcept {}
82 operator()(T* p)
const {
91template <
class T,
class... Args>
92inline ::std::shared_ptr<T>
94 return ::std::shared_ptr<T>(
new T(::std::forward<Args>(args)...),
109template <
class T,
class... Args>
125 typedef typename c_type_traits<T>::value_type c_value_type;
126 typedef typename c_type_traits<T>::reference c_reference;
127 typedef typename c_type_traits<T>::const_reference c_const_reference;
128 typedef typename c_type_traits<T>::pointer c_pointer;
129 typedef typename c_type_traits<T>::const_pointer c_const_pointer;
131 operator c_value_type()
const noexcept {
return c_ref(); }
132 operator c_reference()
noexcept {
return c_ref(); }
133 operator c_const_reference()
const noexcept {
return c_ref(); }
141 c_ref()
const noexcept {
147 return reinterpret_cast<c_pointer
>(
this);
151 c_ptr()
const noexcept {
152 return reinterpret_cast<c_const_pointer
>(
this);
174class standard_c_type {
176 typedef typename c_type_traits<T>::value_type c_value_type;
177 typedef typename c_type_traits<T>::reference c_reference;
178 typedef typename c_type_traits<T>::const_reference c_const_reference;
179 typedef typename c_type_traits<T>::pointer c_pointer;
180 typedef typename c_type_traits<T>::const_pointer c_const_pointer;
182 operator c_value_type()
const noexcept {
return c_ref(); }
183 operator c_reference()
noexcept {
return c_ref(); }
184 operator c_const_reference()
const noexcept {
return c_ref(); }
192 c_ref()
const noexcept {
198 return reinterpret_cast<c_pointer
>(
this);
202 c_ptr()
const noexcept {
203 return reinterpret_cast<c_const_pointer
>(
this);
207 dtor(standard_c_type* p)
noexcept {
217 operator=(
const standard_c_type& val) {
218 if (!c_type_traits<T>::copy(c_ptr(), val.c_ptr())) impl::throw_bad_copy();
223 operator=(standard_c_type&& val) {
224 if (!c_type_traits<T>::move(c_ptr(), val.c_ptr())) impl::throw_bad_move();
229 template <
class... Args>
230 explicit standard_c_type(Args&&... args) {
231 if (!c_type_traits<T>::init(c_ptr(), std::forward<Args>(args)...))
232 impl::throw_bad_init();
235 ~standard_c_type() { c_type_traits<T>::fini(c_ptr()); }
249class incomplete_c_type {
251 typedef typename c_type_traits<T>::value_type c_value_type;
252 typedef typename c_type_traits<T>::reference c_reference;
253 typedef typename c_type_traits<T>::const_reference c_const_reference;
254 typedef typename c_type_traits<T>::pointer c_pointer;
255 typedef typename c_type_traits<T>::const_pointer c_const_pointer;
257 operator c_reference()
noexcept {
return c_ref(); }
258 operator c_const_reference()
const noexcept {
return c_ref(); }
266 c_ref()
const noexcept {
272 return reinterpret_cast<c_pointer
>(
this);
276 c_ptr()
const noexcept {
277 return reinterpret_cast<c_const_pointer
>(
this);
281 dtor(incomplete_c_type* p)
noexcept {
291 operator new(std::size_t size) {
292 void* ptr =
operator new(size, ::std::nothrow);
298 operator new(std::size_t, const ::std::nothrow_t&)
noexcept {
299 return c_type_traits<T>::alloc();
303 operator delete(
void* ptr)
noexcept {
304 operator delete(ptr, ::std::nothrow);
308 operator delete(
void* ptr, const ::std::nothrow_t&)
noexcept {
309 c_type_traits<T>::free(ptr);
313 operator=(
const incomplete_c_type& val) {
314 if (!c_type_traits<T>::copy(c_ptr(), val.c_ptr())) impl::throw_bad_copy();
319 operator=(incomplete_c_type&& val) {
320 if (!c_type_traits<T>::move(c_ptr(), val.c_ptr())) impl::throw_bad_move();
324 void*
operator new[](std::size_t) =
delete;
325 void*
operator new[](std::size_t, const ::std::nothrow_t&) =
delete;
326 void operator delete[](
void*) =
delete;
327 void operator delete[](
void*, const ::std::nothrow_t&) =
delete;
330 template <
class... Args>
331 explicit incomplete_c_type(Args&&... args) {
332 if (!c_type_traits<T>::init(c_ptr(), ::std::forward<Args>(args)...))
333 impl::throw_bad_init();
336 ~incomplete_c_type() { c_type_traits<T>::fini(c_ptr()); }
351 typedef T value_type;
352 typedef value_type& reference;
353 typedef const value_type& const_reference;
354 typedef value_type* pointer;
355 typedef const value_type* const_pointer;
359 return operator new(
sizeof(T), ::std::nothrow);
363 free(
void* ptr)
noexcept {
364 operator delete(ptr, ::std::nothrow);
368 init(pointer p)
noexcept {
373 init(pointer p,
const T& val)
noexcept {
374 return new (
static_cast<void*
>(p)) T(val);
378 fini(pointer p)
noexcept {
383 copy(pointer p1, const_pointer p2)
noexcept {
389 move(pointer p1, pointer p2)
noexcept {
390 *p1 = ::std::move(*p2);
401 typedef void value_type;
404 typedef __type& reference;
405 typedef const __type& const_reference;
406 typedef value_type* pointer;
407 typedef const value_type* const_pointer;
411 return operator new(0, ::std::nothrow);
415 free(
void* ptr)
noexcept {
416 operator delete(ptr, ::std::nothrow);
420 init(pointer p)
noexcept {
425 fini(pointer p)
noexcept {
430 copy(pointer p1, const_pointer p2)
noexcept {
437 move(pointer p1, pointer p2)
noexcept {
unique_c_ptr< T > make_unique_c(Args &&... args)
Creates an instance of a trivial, standard layout or incomplete C type and wraps it in a lely::unique...
inline ::std::shared_ptr< T > make_shared_c(Args &&... args)
Creates an instance of a trivial, standard layout or incomplete C type and wraps it in a std::shared_...
::std::unique_ptr< T, delete_c_type< T > > unique_c_ptr
A specialization of std::unique_ptr for trivial, standard layout or incomplete C types,...
The type of objects thrown as exceptions to report a failure to copy an instantiation of a C type.
The type of objects thrown as exceptions to report a failure to initialize an instantiation of a C ty...
The type of objects thrown as exceptions to report a failure to move an instantiation of a C type.
The base class for a C++ interface to an incomplete C type.
The base class for a C++ interface to a standard layout C type.
@ ERRNUM_NOMEM
Not enough space.
errnum_t get_errnum(void)
Returns the last (thread-specific) platform-independent error number set by a system call or library ...
This header file is part of the utilities library; it contains the C++ exception declarations.
#define throw_or_abort(e)
If exceptions are disabled, aborts the process instead of throwing an exception.
A class template supplying a uniform interface to certain attributes of C types.
The deleter for trivial, standard layout and incomplete C types.
The base class for a C++ interface to a trivial C type.