1 | /* |
2 | * PROGRAM: Common class definition |
3 | * MODULE: fb_pair.h |
4 | * DESCRIPTION: Provides almost that same functionality, |
5 | * that STL::pair does, but behaves |
6 | * MemoryPools friendly. |
7 | * |
8 | * The contents of this file are subject to the Initial |
9 | * Developer's Public License Version 1.0 (the "License"); |
10 | * you may not use this file except in compliance with the |
11 | * License. You may obtain a copy of the License at |
12 | * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl. |
13 | * |
14 | * Software distributed under the License is distributed AS IS, |
15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. |
16 | * See the License for the specific language governing rights |
17 | * and limitations under the License. |
18 | * |
19 | * The Original Code was created by Alexander Peshkoff |
20 | * for the Firebird Open Source RDBMS project. |
21 | * |
22 | * Copyright (c) 2004 Alexander Peshkoff <peshkoff@mail.ru> |
23 | * and all contributors signed below. |
24 | * |
25 | * All Rights Reserved. |
26 | * Contributor(s): ______________________________________. |
27 | */ |
28 | |
29 | #ifndef CLASSES_FB_PAIR_H |
30 | #define CLASSES_FB_PAIR_H |
31 | |
32 | #include "../common/classes/alloc.h" |
33 | |
34 | namespace Firebird |
35 | { |
36 | |
37 | // Non MemoryPool'ed pair - left and right object in such pair hasn't MemoryPool'ed |
38 | // constructor (typically POD or builtin type). |
39 | |
40 | template<typename parLeft, typename parRight> |
41 | struct NonPooled |
42 | { |
43 | typedef parLeft first_type; |
44 | typedef parRight second_type; |
45 | explicit NonPooled(MemoryPool&) : first(), second() { } |
46 | explicit NonPooled(MemoryPool&, const parLeft& v1, const parRight& v2) |
47 | : first(v1), second(v2) { } |
48 | explicit NonPooled(MemoryPool&, const NonPooled& lp) |
49 | : first(lp.first), second(lp.second) { } |
50 | parLeft first; |
51 | parRight second; |
52 | }; |
53 | |
54 | // Right pair - right object in such pair has MemoryPool'ed constructor, |
55 | // left one - doesn't (typically POD or builtin type) |
56 | |
57 | template<typename parLeft, typename parRight> |
58 | struct Right |
59 | { |
60 | typedef parLeft first_type; |
61 | typedef parRight second_type; |
62 | explicit Right(MemoryPool& p) : first(), second(p) { } |
63 | explicit Right(MemoryPool& p, const parLeft& v1, const parRight& v2) |
64 | : first(v1), second(p, v2) { } |
65 | explicit Right(MemoryPool& p, const Right& lp) |
66 | : first(lp.first), second(p, lp.second) { } |
67 | parLeft first; |
68 | parRight second; |
69 | }; |
70 | |
71 | // Left pair - left object in such pair has MemoryPool'ed constructor, |
72 | // right one - doesn't (typically POD or builtin type) |
73 | |
74 | template<typename parLeft, typename parRight> |
75 | struct Left |
76 | { |
77 | typedef parLeft first_type; |
78 | typedef parRight second_type; |
79 | explicit Left(MemoryPool& p) : first(p), second() { } |
80 | explicit Left(MemoryPool& p, const parLeft& v1, const parRight& v2) |
81 | : first(p, v1), second(v2) { } |
82 | explicit Left(MemoryPool& p, const Left& lp) |
83 | : first(p, lp.first), second(lp.second) { } |
84 | parLeft first; |
85 | parRight second; |
86 | }; |
87 | |
88 | // Full pair - both objects in such pair have MemoryPool'ed constructors. |
89 | |
90 | template<typename parLeft, typename parRight> |
91 | struct Full |
92 | { |
93 | typedef parLeft first_type; |
94 | typedef parRight second_type; |
95 | explicit Full(MemoryPool& p) : first(p), second(p) { } |
96 | explicit Full(MemoryPool& p, const parLeft& v1, const parRight& v2) |
97 | : first(p, v1), second(p, v2) { } |
98 | explicit Full(MemoryPool& p, const Full& lp) |
99 | : first(p, lp.first), second(p, lp.second) { } |
100 | parLeft first; |
101 | parRight second; |
102 | }; |
103 | |
104 | // Pair - template providing full bool op-s set |
105 | |
106 | template<typename BasePair> |
107 | struct Pair : public BasePair |
108 | { |
109 | typedef typename Pair::first_type Pair_first_type; |
110 | typedef typename Pair::second_type Pair_second_type; |
111 | explicit Pair(MemoryPool& p) : BasePair(p) { } |
112 | explicit Pair(MemoryPool& p, const Pair_first_type& v1, |
113 | const Pair_second_type& v2) : BasePair(p, v1, v2) { } |
114 | explicit Pair(MemoryPool& p, const Pair& lp) |
115 | : BasePair(p, lp) { } |
116 | Pair() : BasePair(AutoStorage::getAutoMemoryPool()) { } |
117 | Pair(const Pair_first_type& v1, const Pair_second_type& v2) |
118 | : BasePair(AutoStorage::getAutoMemoryPool(), v1, v2) { } |
119 | Pair(const Pair& lp) |
120 | : BasePair(AutoStorage::getAutoMemoryPool(), lp) { } |
121 | bool operator==(const Pair& v) const |
122 | { |
123 | return this->first == v.first && this->second == v.second; |
124 | } |
125 | bool operator<(const Pair& v) const |
126 | { |
127 | return this->first < v.first || (this->first == v.first && this->second < v.second); |
128 | } |
129 | bool operator!=(const Pair& v) const |
130 | { |
131 | return ! (*this == v); |
132 | } |
133 | bool operator>(const Pair& v) const |
134 | { |
135 | return v < *this; |
136 | } |
137 | bool operator<=(const Pair& v) const |
138 | { |
139 | return ! (v < *this); |
140 | } |
141 | bool operator>=(const Pair& v) const |
142 | { |
143 | return ! (*this < v); |
144 | } |
145 | }; |
146 | |
147 | template <typename P> |
148 | class FirstKey |
149 | { |
150 | public: |
151 | typedef typename P::first_type Pair_first_type; |
152 | static const Pair_first_type& generate(const P& item) |
153 | { |
154 | return item.first; |
155 | } |
156 | static const Pair_first_type& generate(const void* /*sender*/, const P& item) |
157 | { |
158 | return item.first; |
159 | } |
160 | }; |
161 | |
162 | template <typename P> |
163 | class FirstPointerKey |
164 | { |
165 | public: |
166 | typedef typename P::first_type Pair_first_type; |
167 | static const Pair_first_type* generate(const P* item) |
168 | { |
169 | return &item->first; |
170 | } |
171 | static const Pair_first_type* generate(const void* /*sender*/, const P* item) |
172 | { |
173 | return &item->first; |
174 | } |
175 | }; |
176 | |
177 | template <typename P> |
178 | class FirstObjectKey |
179 | { |
180 | public: |
181 | typedef typename P::first_type Pair_first_type; |
182 | static const Pair_first_type& generate(const P* item) |
183 | { |
184 | return item->first; |
185 | } |
186 | static const Pair_first_type& generate(const void* /*sender*/, const P* item) |
187 | { |
188 | return item->first; |
189 | } |
190 | }; |
191 | } // namespace Firebird |
192 | |
193 | #endif // CLASSES_FB_PAIR_H |
194 | |