OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_CONTAINERS_SMALL_MAP_H_ | 5 #ifndef BASE_CONTAINERS_SMALL_MAP_H_ |
6 #define BASE_CONTAINERS_SMALL_MAP_H_ | 6 #define BASE_CONTAINERS_SMALL_MAP_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 return map_.get(); | 568 return map_.get(); |
569 } | 569 } |
570 | 570 |
571 private: | 571 private: |
572 int size_; // negative = using hash_map | 572 int size_; // negative = using hash_map |
573 | 573 |
574 MapInit functor_; | 574 MapInit functor_; |
575 | 575 |
576 // We want to call constructors and destructors manually, but we don't | 576 // We want to call constructors and destructors manually, but we don't |
577 // want to allocate and deallocate the memory used for them separately. | 577 // want to allocate and deallocate the memory used for them separately. |
578 // So, we use this crazy ManualConstructor class. | 578 // So, we use this crazy ManualConstructor class. Since C++11 it's possible |
| 579 // to use objects in unions like this, but the ManualDestructor syntax is |
| 580 // a bit better and doesn't have limitations on object type. |
579 // | 581 // |
580 // Since array_ and map_ are mutually exclusive, we'll put them in a | 582 // Since array_ and map_ are mutually exclusive, we'll put them in a |
581 // union, too. We add in a dummy_ value which quiets MSVC from otherwise | 583 // union. |
582 // giving an erroneous "union member has copy constructor" error message | |
583 // (C2621). This dummy member has to come before array_ to quiet the | |
584 // compiler. | |
585 // | |
586 // TODO(brettw) remove this and use C++11 unions when we require C++11. | |
587 union { | 584 union { |
588 ManualConstructor<value_type> dummy_; | |
589 ManualConstructor<value_type> array_[kArraySize]; | 585 ManualConstructor<value_type> array_[kArraySize]; |
590 ManualConstructor<NormalMap> map_; | 586 ManualConstructor<NormalMap> map_; |
591 }; | 587 }; |
592 | 588 |
593 void ConvertToRealMap() { | 589 void ConvertToRealMap() { |
594 // Move the current elements into a temporary array. | 590 // Move the current elements into a temporary array. |
595 ManualConstructor<value_type> temp_array[kArraySize]; | 591 ManualConstructor<value_type> temp_array[kArraySize]; |
596 | 592 |
597 for (int i = 0; i < kArraySize; i++) { | 593 for (int i = 0; i < kArraySize; i++) { |
598 temp_array[i].InitFromMove(std::move(array_[i])); | 594 temp_array[i].InitFromMove(std::move(array_[i])); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
645 typename Functor> | 641 typename Functor> |
646 inline bool SmallMap<NormalMap, kArraySize, EqualKey, | 642 inline bool SmallMap<NormalMap, kArraySize, EqualKey, |
647 Functor>::iterator::operator!=( | 643 Functor>::iterator::operator!=( |
648 const const_iterator& other) const { | 644 const const_iterator& other) const { |
649 return other != *this; | 645 return other != *this; |
650 } | 646 } |
651 | 647 |
652 } // namespace base | 648 } // namespace base |
653 | 649 |
654 #endif // BASE_CONTAINERS_SMALL_MAP_H_ | 650 #endif // BASE_CONTAINERS_SMALL_MAP_H_ |
OLD | NEW |