FXPolicies.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                                Policy classes                                 *
00004 *                                                                               *
00005 *********************************************************************************
00006 *        Copyright (C) 2003 by Niall Douglas.   All Rights Reserved.            *
00007 *       NOTE THAT I DO NOT PERMIT ANY OF MY CODE TO BE PROMOTED TO THE GPL      *
00008 *********************************************************************************
00009 * This code is free software; you can redistribute it and/or modify it under    *
00010 * the terms of the GNU Library General Public License v2.1 as published by the  *
00011 * Free Software Foundation EXCEPT that clause 3 does not apply ie; you may not  *
00012 * "upgrade" this code to the GPL without my prior written permission.           *
00013 * Please consult the file "License_Addendum2.txt" accompanying this file.       *
00014 *                                                                               *
00015 * This code is distributed in the hope that it will be useful,                  *
00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                          *
00018 *********************************************************************************
00019 * $Id:                                                                          *
00020 ********************************************************************************/
00021 
00022 #ifndef FXPOLICIES_H
00023 #define FXPOLICIES_H
00024 
00025 #include "FXException.h"
00026 
00027 namespace FX {
00028 
00033 class QMtxHold;
00034 
00035 namespace Pol {
00036 
00040 class None0
00041 {
00042 protected:
00043     None0() { }
00044     ~None0() { }
00045 };
00049 template<class type> class None1
00050 {
00051 protected:
00052     None1() { }
00053     ~None1() { }
00054 };
00055 
00063 template<class type> class deepCopy
00064 {
00065 protected:
00066     type *data;
00067 public:
00068     deepCopy(type *d) : data(d) { }
00069     deepCopy(const deepCopy &o) : data(o.data->copy()) { FXERRHM(data); }
00070     deepCopy &operator=(const deepCopy &o)
00071     {
00072         FXDELETE(data);
00073         FXERRHM(data=o.data->copy());
00074         return *this;
00075     }
00076     ~deepCopy() { FXDELETE(data); }
00077     friend type *PtrPtr(deepCopy &p) { return p.data; }
00078     friend const type *PtrPtr(const deepCopy &p) { return p.data; }
00079     friend type *&PtrRef(deepCopy &p) { return p.data; }
00080     friend const type *PtrRef(const deepCopy &p) { return p.data; }
00081 };
00082 
00090 template<class type> class refCounted
00091 {
00092 protected:
00093     type *data;
00094 public:
00095     refCounted(type *d) : data(d) { ++data->refCount(); }
00096     refCounted(const refCounted &o) : data(o.data) { ++data->refCount(); }
00097     refCounted &operator=(const refCounted &o)
00098     {
00099         if(!--data->refCount()) FXDELETE(data);
00100         data=o.data;
00101         ++data->refCount();
00102         return *this;
00103     }
00104     ~refCounted()
00105     {
00106         if(!--data->refCount()) FXDELETE(data);
00107     }
00108     friend type *PtrPtr(refCounted &p) { return p.data; }
00109     friend const type *PtrPtr(const refCounted &p) { return p.data; }
00110     friend type *&PtrRef(refCounted &p) { return p.data; }
00111     friend const type *PtrRef(const refCounted &p) { return p.data; }
00112 };
00113 
00119 template<class type> class destructiveCopyNoDelete
00120 {
00121 protected:
00122     mutable type *data;
00123     destructiveCopyNoDelete &operator=(const destructiveCopyNoDelete &o);   // implementation needs to be replaced
00124 public:
00125     destructiveCopyNoDelete(type *d) : data(d) { }
00126     destructiveCopyNoDelete(const destructiveCopyNoDelete &o) : data(o.data) { o.data=0; }
00127     friend type *PtrPtr(destructiveCopyNoDelete &p) { return p.data; }
00128     friend const type *PtrPtr(const destructiveCopyNoDelete &p) { return p.data; }
00129     friend type *&PtrRef(destructiveCopyNoDelete &p) { return p.data; }
00130     friend const type *PtrRef(const destructiveCopyNoDelete &p) { return p.data; }
00131 };
00132 
00138 template<class type> class destructiveCopy : public destructiveCopyNoDelete<type>
00139 {
00140 public:
00141     destructiveCopy(type *d) : destructiveCopyNoDelete<type>(d) { }
00142     ~destructiveCopy() { FXDELETE(destructiveCopyNoDelete<type>::data); }
00143     destructiveCopy &operator=(const destructiveCopy &o)
00144     {
00145         FXDELETE(destructiveCopyNoDelete<type>::data);
00146         destructiveCopyNoDelete<type>::data=o.data;
00147         o.data=0;
00148         return *this;
00149     }
00150 };
00151 
00157 template<class type> class noCopyNoDelete
00158 {
00159 protected:
00160     type *data;
00161 public:
00162     noCopyNoDelete(type *d) : data(d) { }
00163     friend type *PtrPtr(noCopyNoDelete &p) { return p.data; }
00164     friend const type *PtrPtr(const noCopyNoDelete &p) { return p.data; }
00165     friend type *&PtrRef(noCopyNoDelete &p) { return p.data; }
00166     friend const type *PtrRef(const noCopyNoDelete &p) { return p.data; }
00167 };
00168 
00174 template<class type> class noCopy : public noCopyNoDelete<type>
00175 {
00176 public:
00177     noCopy(type *d) : noCopyNoDelete<type>(d) { }
00178     ~noCopy() { FXDELETE(noCopyNoDelete<type>::data); }
00179 };
00180 
00187 template<class type> struct itMove
00188 {
00189     template<class L, class I> void move(L &list, I to, I &from) const
00190     {
00191         list.splice(to, list, from);
00192         from=--to;
00193     }
00194 };
00201 template<class type> struct itSwap
00202 {
00203     template<class L, class I> void swap(L &list, I &a, I &b) const
00204     {
00205         I _a=a, _b=b; ++_a; ++_b;
00206         if(_b==a)
00207         {
00208             list.splice(b, list, a);
00209             a=--_a; b=--_a;
00210         }
00211         else if(_a==b)
00212         {
00213             list.splice(a, list, b);
00214             b=--_b; a=--_b;
00215         }
00216         else
00217         {
00218             list.splice(b, list, a);
00219             list.splice(_a, list, b);
00220             a=--_a; b=--_b;
00221         }
00222     }
00223 };
00230 template<class type> struct itCompare
00231 {
00232     template<class L, class I> bool compare(L &list, I &a, I &b) const
00233     {
00234         return *a<*b;
00235     }
00236 };
00243 template<class type> struct itRevCompare
00244 {
00245     template<class L, class I> bool compare(L &list, I &a, I &b) const
00246     {
00247         return *a>*b;
00248     }
00249 };
00250 
00251 } // namespace
00252 
00253 } // namespace
00254 
00255 #endif

(C) 2002-2009 Niall Douglas. Some parts (C) to assorted authors.
Generated on Fri Nov 20 18:31:23 2009 for TnFOX by doxygen v1.4.7