Make WvStreams compile with gcc 4.4.
[wvstreams.git] / include / xplc / delete.h
blob7086eafd6078da45026c6d2fbf7c94bf93b2045b
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * XPLC - Cross-Platform Lightweight Components
4 * Copyright (C) 2004, Net Integration Technologies, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 * USA
21 * As a special exception, you may use this file as part of a free
22 * software library without restriction. Specifically, if other files
23 * instantiate templates or use macros or inline functions from this
24 * file, or you compile this file and link it with other files to
25 * produce an executable, this file does not by itself cause the
26 * resulting executable to be covered by the GNU Lesser General Public
27 * License. This exception does not however invalidate any other
28 * reasons why the executable file might be covered by the GNU Lesser
29 * General Public License.
32 #ifndef __XPLC_DELETE_H__
33 #define __XPLC_DELETE_H__
35 #if defined(__GNUC__) && __GNUC__ > 3
36 # pragma GCC system_header
37 #endif
39 /** \file
41 * Include this file right after IObject.h (or instead of) to detect
42 * accidental uses of "delete" on an %XPLC interface. Use "xplcdelete"
43 * instead of "delete" when you know what you are doing.
46 #include <new>
47 #include <memory>
49 #ifdef __XPLC_IOBJECT_H__
50 #error "<xplc/delete.h> has to be included before <xplc/IObject.h>."
51 #endif
53 #include <xplc/IObject.h>
55 /**
56 * \defgroup DeleteDetector This is the "delete detector".
59 /// \ingroup DeleteDetector
60 class CheckIObject {};
61 class CheckIObjectOk {};
62 /// \ingroup DeleteDetector
63 class CheckIObjectOkVector {};
65 template<class T>
66 class ConversionIObject
68 public:
69 //@{
70 typedef char Yes;
71 /// \ingroup DeleteDetector
72 struct No { char dummy[2]; };
73 static T* from;
74 static Yes test(const IObject*);
75 static No test(...);
76 //@}
79 template<bool>
80 struct XPLC_CTAssert;
81 template<>
82 /// \ingroup DeleteDetector
83 struct XPLC_CTAssert<true> {};
85 template<class T>
86 inline void operator&&(CheckIObject, const T* obj) {
87 static_cast<void>(sizeof(XPLC_CTAssert<(sizeof(ConversionIObject<T>::test(ConversionIObject<T>::from)) != sizeof(typename ConversionIObject<T>::Yes))>));
88 delete obj;
91 template<class T>
92 inline void operator&&(CheckIObjectOk, const T* obj) {
93 delete obj;
96 template<class T>
97 inline void operator&&(CheckIObjectOkVector, const T* obj) {
98 delete[] obj;
103 * Undefine xplcdelete. <xplc/utils.h> defines xplcdelete, we should
104 * undo this, in case it has been included before.
106 #undef xplcdelete
109 * Macro used to indicate a valid use of the delete keyword with an
110 * XPLC interface. In some cases, you really need to use delete on an
111 * object that derives from IObject. In those cases, use "xplcdelete"
112 * instead of "delete".
114 #define xplcdelete CheckIObjectOk() &&
117 * Overriding the delete keyword. This replaces the delete keyword
118 * with an invocation of the operator&& using a specific marker class
119 * as the left operand, allowing it to be templated on the right
120 * operand.
122 #define delete CheckIObject() &&
125 * Remplacement for delete[]. Because we cannot capture usage of
126 * delete[] using macros, we have to add a replacement for it,
127 * unfortunately.
129 #define deletev CheckIObjectOkVector() &&
131 #endif /* __XPLC_DELETE_H__ */