]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - misc/mediasource/netradiant-src/libs/generic/reference.h
Move all other sources in a separate subfolder
[voretournament/voretournament.git] / misc / mediasource / netradiant-src / libs / generic / reference.h
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #if !defined(INCLUDED_GENERIC_REFERENCE_H)
23 #define INCLUDED_GENERIC_REFERENCE_H
24
25 /// \file
26 /// \brief Wrappers to allow storing objects in templated containers using 'reference' semantics. 
27
28 /// \brief A reference to a mutable object.
29 /// Has 'reference' semantics, except for \c 'operator==' and \c 'operator.'.
30 /// \param Type The type of the referenced object.
31 template<typename Type>
32 class Reference
33
34   Type* m_contained;
35 public:
36   explicit Reference(Type& contained) : m_contained(&contained)
37   {
38   }
39   operator Type&() const
40   {
41     return *m_contained;
42   }
43   Type& get() const
44   {
45     return *m_contained;
46   }
47   Type* get_pointer() const
48   {
49     return m_contained;
50   }
51 };
52
53 template<typename Type>
54 bool operator<(const Reference<Type>& self, const Reference<Type>& other)
55 {
56   return self.get() < other.get();
57 }
58 template<typename Type>
59 bool operator==(const Reference<Type>& self, const Reference<Type>& other)
60 {
61   return self.get() == other.get();
62 }
63
64 /// \brief construct a reference to a mutable object.
65 template<typename Type>
66 inline Reference<Type> makeReference(Type& value)
67 {
68   return Reference<Type>(value);
69 }
70
71 /// \brief A reference to a non-mutable object.
72 /// Has 'reference' semantics, except for \c 'operator==' and \c 'operator.'.
73 /// \param Type The type of the referenced object.
74 template<typename Type>
75 class ConstReference
76 {
77   const Type* m_contained;
78 public:
79   explicit ConstReference(const Type& contained) : m_contained(&contained)
80   {
81   }
82   operator const Type&() const
83   {
84     return *m_contained;
85   }
86   const Type& get() const
87   {
88     return *m_contained;
89   }
90   const Type* get_pointer() const
91   {
92     return m_contained;
93   }
94 };
95
96 template<typename Type>
97 bool operator<(const ConstReference<Type>& self, const ConstReference<Type>& other)
98 {
99   return self.get() < other.get();
100 }
101 template<typename Type>
102 bool operator==(const ConstReference<Type>& self, const ConstReference<Type>& other)
103 {
104   return self.get() == other.get();
105 }
106
107 /// \brief construct a reference to a non-mutable object.
108 template<typename Type>
109 inline ConstReference<Type> makeReference(const Type& value)
110 {
111   return ConstReference<Type>(value);
112 }
113
114
115 #endif