File: ptrutils.h

package info (click to toggle)
regina-normal 4.5-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 16,824 kB
  • ctags: 7,862
  • sloc: cpp: 63,296; ansic: 12,913; sh: 10,556; perl: 3,294; makefile: 947; python: 188
file content (146 lines) | stat: -rw-r--r-- 5,795 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

/**************************************************************************
 *                                                                        *
 *  Regina - A Normal Surface Theory Calculator                           *
 *  Computational Engine                                                  *
 *                                                                        *
 *  Copyright (c) 1999-2008, Ben Burton                                   *
 *  For further details contact Ben Burton (bab@debian.org).              *
 *                                                                        *
 *  This program is free software; you can redistribute it and/or         *
 *  modify it under the terms of the GNU General Public License as        *
 *  published by the Free Software Foundation; either version 2 of the    *
 *  License, or (at your option) any later version.                       *
 *                                                                        *
 *  This program is distributed in the hope that it will be useful, but   *
 *  WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *  General Public License for more details.                              *
 *                                                                        *
 *  You should have received a copy of the GNU General Public             *
 *  License along with this program; if not, write to the Free            *
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,       *
 *  MA 02110-1301, USA.                                                   *
 *                                                                        *
 **************************************************************************/

/* end stub */

/*! \file ptrutils.h
 *  \brief Provides function objects for use in the Standard Template
 *  Library that take pointers as arguments but work with the pointees
 *  instead.
 */

#ifndef __PTRUTILS_H
#ifndef __DOXYGEN
#define __PTRUTILS_H
#endif

namespace regina {

/**
 * \weakgroup utilities
 * @{
 */

/**
 * A simple routine for cloning an object if and only if it exists.
 *
 * If the given pointer is non-null, this routine returns a new clone of the
 * object, created using the copy constructor for type \a T.  Otherwise
 * this routine simply returns a null pointer.
 *
 * The caller of this routine is responsible for deleting the new clone
 * when it is no longer required.
 *
 * This routine can be useful when implementing copy constructors for
 * classes that only initialise internal data members on demand.
 *
 * \ifacespython Not present.
 *
 * @param cloneMe a pointer to the object to clone; this may be null.
 * @return a newly allocated copy of the given object, or the null
 * pointer if \a cloneMe is null.
 */
template <typename T>
inline T* clonePtr(T* cloneMe) {
    return (cloneMe ? new T(*cloneMe) : 0);
}

/**
 * A simple routine for cloning an object if and only if it exists.
 *
 * If the given pointer is non-null, this routine returns a new clone of the
 * object, created using the copy constructor for type \a T.  Otherwise
 * this routine simply returns a null pointer.
 *
 * Note that, even though this routine takes a std::auto_ptr, it returns
 * a raw pointer.  The caller of this routine is responsible for
 * deleting the new clone when it is no longer required.
 *
 * This routine can be useful when implementing copy constructors for
 * classes that only initialise internal data members on demand.
 *
 * \ifacespython Not present.
 *
 * @param cloneMe a pointer to the object to clone; this may be null.
 * @return a newly allocated copy of the given object, or the null
 * pointer if \a cloneMe is null.
 */
template <typename T>
inline T* clonePtr(const std::auto_ptr<T>& cloneMe) {
    return (cloneMe.get() ? new T(*cloneMe) : 0);
}

/**
 * An adaptable binary function used to compare the objects to which
 * pointers are pointing.  This class is for use with the Standard Template
 * Library.
 *
 * The first template argument \a T will generally not be a pointer class.
 * Instead, this function will accept two const \e pointers to \a T.  It
 * will then dereference these pointers and compare these dereferenced
 * objects using the given comparison function (which defaults to std::less,
 * but which can be changed by passing a different second template argument).
 *
 * \ifacespython Not present.
 */
template <typename T, typename Comp = std::less<T> >
class LessDeref {
    public:
        typedef const T* first_argument_type;
            /**< The first argument type for this binary function. */
        typedef const T* second_argument_type;
            /**< The second argument type for this binary function. */
        typedef bool result_type;
            /**< The result type for this binary comparison function. */

    private:
        Comp comp_;
            /**< A function object for performing comparisons between
                 dereferenced objects. */

    public:
        /**
         * Compares the objects to which the given pointers are pointing.
         * The two pointers are dereferenced, and then a function of
         * type \a Comp (the second template argument) is used to
         * compare the dereferenced objects.
         *
         * @param ptr1 a pointer to the first object under examination.
         * @param ptr2 a pointer to the second object under examination.
         * @return \c true if the first dereferenced object is less than
         * the second, or \c false otherwise.
         */
        bool operator() (const T* ptr1, const T* ptr2) const {
            return comp_(*ptr1, *ptr2);
        }
};

/*@}*/

} // namespace regina

#endif