3 // Copyright (C) 2005, 2006 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32 // Permission to use, copy, modify, sell, and distribute this software
33 // is hereby granted without fee, provided that the above copyright
34 // notice appears in all copies, and that both that copyright notice and
35 // this permission notice appear in supporting documentation. None of
36 // the above authors, nor IBM Haifa Research Laboratories, make any
37 // representation about the suitability of this software for any
38 // purpose. It is provided "as is" without express or implied warranty.
41 * @file value_type_adapter.hpp
42 * Contains an adapter of mapping levels.
45 #ifndef VALUE_TYPE_ADAPTER_HPP
46 #define VALUE_TYPE_ADAPTER_HPP
48 #include <ext/pb_assoc/detail/value_type_adapter/ref_pair.hpp>
49 #include <ext/pb_assoc/detail/assoc_cntnr_base.hpp>
50 #include <ext/pb_assoc/detail/value_type_adapter/invalidation_guarantee_selector.hpp>
51 #include <ext/pb_assoc/detail/type_utils.hpp>
54 #include <tr1/type_traits> // for aligned_storage/alignment_of
62 #define PB_ASSOC_STATIC_ASSERT(UNIQUE, E) \
64 pb_assoc::detail::static_assert_dummy_class< \
65 sizeof(pb_assoc::detail::static_assert<(bool)(E)>)> \
66 UNIQUE##static_assert_type
68 #define PB_ASSOC_CLASS_T_DEC \
77 #define PB_ASSOC_CLASS_C_DEC \
86 #define PB_ASSOC_BASE_C_DEC \
96 typename assoc_cntnr_base< \
101 Allocator>::type>::type
103 template<typename Key,
109 struct value_type_adapter : public PB_ASSOC_BASE_C_DEC
113 typedef typename PB_ASSOC_BASE_C_DEC my_base;
115 typedef typename my_base::data_type my_base_data_type;
121 typename my_base::allocator::template rebind<
123 typename my_base_data_type::allocator::template rebind<
127 PB_ASSOC_STATIC_ASSERT(wrong_level, Mapping_Level > 0);
129 PB_ASSOC_STATIC_ASSERT(must_be_same_alloc, same_alloc_type);
131 #include <ext/pb_assoc/detail/value_type_adapter/value_type_traits.hpp>
132 #include <ext/pb_assoc/detail/value_type_adapter/it_value_type_traits.hpp>
135 it_value_type_traits_<
136 typename base_it_key_type<
138 Mapping_Level == 1>::type,
139 typename my_base_data_type::const_key_reference,
142 typename my_base_data_type::data_type,
143 null_data_type>::value,
145 typename my_base_data_type::data_reference>::type,
146 typename my_base_data_type::reference,
147 typename my_base::allocator>
148 it_value_type_traits_t;
150 #include <ext/pb_assoc/detail/value_type_adapter/iterator.hpp>
154 typename my_base::key_type,
155 typename my_base_data_type::key_type,
156 typename my_base_data_type::data_type,
157 typename my_base::allocator>
164 typename my_base_data_type::data_type,
165 null_data_type>::value
170 typedef typename Allocator::size_type size_type;
172 typedef typename Allocator::difference_type difference_type;
174 typedef typename my_base::allocator allocator;
176 typedef typename it_value_type_traits_t::key_type it_key_type;
180 typename my_base::key_type,
181 typename my_base_data_type::key_type>
185 typename allocator::template rebind<
186 key_type>::other::reference
190 typename allocator::template rebind<
191 key_type>::other::const_reference
195 typename allocator::template rebind<
196 key_type>::other::pointer
200 typename allocator::template rebind<
201 key_type>::other::const_pointer
204 typedef typename my_base_data_type::data_type data_type;
207 typename allocator::template rebind<
208 data_type>::other::reference
212 typename allocator::template rebind<
213 data_type>::other::const_reference
214 const_data_reference;
217 typename allocator::template rebind<
218 data_type>::other::pointer
222 typename allocator::template rebind<
223 data_type>::other::const_pointer
226 typedef typename value_type_traits_t::value_type value_type;
228 typedef typename value_type_traits_t::reference reference;
230 typedef typename value_type_traits_t::const_reference const_reference;
232 typedef typename value_type_traits_t::pointer pointer;
234 typedef typename value_type_traits_t::const_pointer const_pointer;
238 typename my_base::const_find_iterator,
239 typename my_base_data_type::const_find_iterator,
246 typename my_base::find_iterator,
247 typename my_base_data_type::find_iterator,
254 typename my_base::const_iterator,
255 typename my_base_data_type::const_iterator,
262 typename my_base::iterator,
263 typename my_base_data_type::iterator,
270 mapping_level = mapping_level_imp<
271 typename my_base::given_data_type>::value -1
276 typedef compound_ds_tag ds_category;
285 compound_data_enabled_ms_tag>::type
292 compound_ds_tag>::type
293 effective_base_ds_tag;
295 typedef ds_traits< my_base_data_type> base_data_ds_traits;
300 base_data_ds_traits::erase_can_throw
306 order_preserving_imp<
308 effective_base_ds_tag>::value&&
309 base_data_ds_traits::order_preserving
315 base_data_ds_traits::erase_iterators
320 typename invalidation_guarantee_imp<
322 effective_base_ds_tag>::type,
324 my_base_data_type>::invalidation_guarantee>::type
325 invalidation_guarantee;
330 reverse_iteration_imp<
332 effective_base_ds_tag>::value&&
333 base_data_ds_traits::reverse_iteration
342 typedef typename my_base_data_type::data_pointer erase_imp_ret_t;
345 inline const_key_reference
346 extract_key_imp(const_reference r_val, int_to_type<true>)
348 return (r_val.first);
351 inline const_key_reference
352 extract_key_imp(const_reference r_val, int_to_type<false>)
358 extract_key_imp(typename iterator::const_reference r_val, int_to_type<true>)
360 return (r_val.first);
364 extract_key_imp(typename iterator::const_reference r_val, int_to_type<false>)
374 return (std::distance(begin(), end()));
380 return (my_base::max_size());
386 return (size() == 0);
389 inline static const_key_reference
390 extract_key(const_reference r_val)
392 return (extract_key_imp(
394 int_to_type<has_data>()));
398 extract_key(typename iterator::const_reference r_val)
400 return (extract_key_imp(
402 int_to_type<has_data>()));
408 insert(const_reference r_val)
410 typedef std::pair< typename my_base::find_iterator, bool> base_ins_ret;
415 inline data_reference
416 operator[](const_key_reference r_key)
418 return (subscript_imp(r_key));
421 inline const_find_iterator
422 find(const_key_reference r_key) const
424 typename my_base::const_find_iterator it = my_base::find(r_key.first);
426 if (it == my_base::end())
429 typename my_base_data_type::const_find_iterator sec_it =
430 it->second.find(r_key.second);
432 if (sec_it == it->second.end())
435 return (const_find_iterator(it, sec_it));
439 find(const_key_reference r_key)
441 typename my_base::find_iterator it = my_base::find(r_key.first);
443 if (it == my_base::end())
446 typename my_base_data_type::find_iterator sec_it =
447 it->second.find(r_key.second);
449 if (sec_it == it->second.end())
452 return (find_iterator(it, my_base::end(), sec_it));
455 inline const_data_reference
456 operator[](const_key_reference r_key) const
458 return (my_base::operator[](r_key.first).operator[](r_key.second));
462 erase(const_key_reference r_key)
464 typename my_base::find_iterator it =
465 my_base::find(r_key.first);
467 if (it == my_base::end())
470 if (it->second.find(r_key.second) == it->second.end())
473 it->second.erase(r_key.second);
478 #include <ext/pb_assoc/detail/value_type_adapter/erase_if_pred.hpp>
484 typename my_base::iterator it = my_base::begin();
486 typename my_base::iterator end_it = my_base::end();
490 // Tmp Ami check erase can throw
494 if (it->second.empty() == false)
496 erase_if_pred<Pred> p(prd, it);
498 ersd += it->second.erase_if(p);
510 typename my_base::iterator it = my_base::begin();
512 typename my_base::iterator end_it = my_base::end();
518 inline const_iterator
521 typename my_base::const_iterator it = my_base::begin();
523 while (it != my_base::end()&& it->second.size() == 0)
526 if (it == my_base::end())
529 return (const_iterator(it, my_base::end(), it->second.begin()));
535 typename my_base::iterator it = my_base::begin();
537 while (it != my_base::end()&& it->second.size() == 0)
540 if (it == my_base::end())
543 return (iterator(it, my_base::end(), it->second.begin()));
546 inline const_iterator
549 return (const_iterator(my_base::end(), my_base::end()));
555 return (iterator(my_base::end(), my_base::end()));
561 ~value_type_adapter()
564 #define PB_ASSOC_CLASS_NAME value_type_adapter
566 #define PB_ASSOC_DIRECT_BASE_C_DEC PB_ASSOC_BASE_C_DEC
568 #define PB_ASSOC_DIRECT_BASE_CAST_C_DEC \
569 typename PB_ASSOC_DIRECT_BASE_C_DEC
571 #include <ext/pb_assoc/detail/constructors_destructor_fn_imps.hpp>
573 #undef PB_ASSOC_CLASS_NAME
575 #undef PB_ASSOC_DIRECT_BASE_C_DEC
577 #undef PB_ASSOC_DIRECT_BASE_CAST_C_DEC
580 subscript_imp(const_key_reference r_key)
582 return (my_base::subscript_imp(r_key.first)[r_key.second]);
587 operator=(const value_type_adapter& r_other);
590 #undef PB_ASSOC_CLASS_T_DEC
592 #undef PB_ASSOC_CLASS_C_DEC
594 #undef PB_ASSOC_BASE_C_DEC
596 } // namespace detail
598 } // namespace pb_assoc
600 #endif // #ifndef VALUE_TYPE_ADAPTER_HPP