]> git.lyx.org Git - lyx.git/blob - boost/boost/utility.hpp
5942fb902f6de39f373bba9c18e4d3c01fe3ecfc
[lyx.git] / boost / boost / utility.hpp
1 //  boost utility.hpp header file  -------------------------------------------//
2
3 //  (C) Copyright boost.org 1999. Permission to copy, use, modify, sell
4 //  and distribute this software is granted provided this copyright
5 //  notice appears in all copies. This software is provided "as is" without
6 //  express or implied warranty, and with no claim as to its suitability for
7 //  any purpose.
8
9 //  See http://www.boost.org for most recent version including documentation.
10
11 //  Classes appear in alphabetical order
12
13 //  Revision History
14 //  21 May 01  checked_delete() and checked_array_delete() added (Beman Dawes,
15 //             suggested by Dave Abrahams, generalizing idea from Vladimir Prus)
16 //  21 May 01  made next() and prior() inline (Beman Dawes)  
17 //  26 Jan 00  protected noncopyable destructor added (Miki Jovanovic)
18 //  10 Dec 99  next() and prior() templates added (Dave Abrahams)
19 //  30 Aug 99  moved cast templates to cast.hpp (Beman Dawes)
20 //   3 Aug 99  cast templates added
21 //  20 Jul 99  name changed to utility.hpp 
22 //   9 Jun 99  protected noncopyable default ctor
23 //   2 Jun 99  Initial Version. Class noncopyable only contents (Dave Abrahams)
24
25 #ifndef BOOST_UTILITY_HPP
26 #define BOOST_UTILITY_HPP
27
28 #include <boost/config.hpp>        // broken compiler workarounds 
29 #include <boost/static_assert.hpp> 
30 #include <cstddef>                 // for size_t
31 #include <utility>                 // for std::pair
32
33 namespace boost
34 {
35 //  checked_delete() and checked_array_delete()  -----------------------------//
36
37     // verify that types are complete for increased safety
38
39     template< typename T >
40     inline void checked_delete(T * x)
41     {
42 # if !((defined(__BORLANDC__) && __BORLANDC__ <= 0x0551) || (defined(__ICL) && __ICL <= 500))
43         BOOST_STATIC_ASSERT( sizeof(T) ); // assert type complete at point
44                                           // of instantiation
45 # else
46         sizeof(T); // force error if type incomplete
47 # endif
48         delete x;
49     }
50
51     template< typename T >
52     inline void checked_array_delete(T  * x)
53     {
54 # if !((defined(__BORLANDC__) && __BORLANDC__ <= 0x0551) || (defined(__ICL) && __ICL <= 500))
55         BOOST_STATIC_ASSERT( sizeof(T) ); // assert type complete at point
56                                           // of instantiation
57 # else
58         sizeof(T); // force error if type incomplete
59 # endif
60         delete [] x;
61     }
62
63 //  next() and prior() template functions  -----------------------------------//
64
65     //  Helper functions for classes like bidirectional iterators not supporting
66     //  operator+ and operator-.
67     //
68     //  Usage:
69     //    const std::list<T>::iterator p = get_some_iterator();
70     //    const std::list<T>::iterator prev = boost::prior(p);
71
72     //  Contributed by Dave Abrahams
73
74     template <class T>
75     inline T next(T x) { return ++x; }
76
77     template <class T>
78     inline T prior(T x) { return --x; }
79
80
81 //  class noncopyable  -------------------------------------------------------//
82
83     //  Private copy constructor and copy assignment ensure classes derived from
84     //  class noncopyable cannot be copied.
85
86     //  Contributed by Dave Abrahams
87
88     class noncopyable
89     {
90     protected:
91         noncopyable(){}
92         ~noncopyable(){}
93     private:  // emphasize the following members are private
94         noncopyable( const noncopyable& );
95         const noncopyable& operator=( const noncopyable& );
96     }; // noncopyable
97
98 //  class tied  -------------------------------------------------------//
99
100     // A helper for conveniently assigning the two values from a pair
101     // into separate variables. The idea for this comes from Jaakko J\84rvi's
102     // Binder/Lambda Library.
103
104     // Constributed by Jeremy Siek
105
106     template <class A, class B>
107     class tied {
108     public:
109       inline tied(A& a, B& b) : _a(a), _b(b) { }
110       template <class U, class V>
111       inline tied& operator=(const std::pair<U,V>& p) {
112         _a = p.first;
113         _b = p.second;
114         return *this;
115       }
116     protected:
117       A& _a;
118       B& _b;
119     };
120
121     template <class A, class B>
122     inline tied<A,B> tie(A& a, B& b) { return tied<A,B>(a, b); }
123
124 } // namespace boost
125
126 #endif  // BOOST_UTILITY_HPP
127