Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <deque> |
| 11 | |
| 12 | // void push_back(const value_type& x); |
| 13 | |
| 14 | #include <deque> |
Marshall Clow | d07fcd6 | 2015-03-09 17:08:51 +0000 | [diff] [blame] | 15 | #include "test_allocator.h" |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 16 | #include <cassert> |
| 17 | |
| 18 | // Flag that makes the copy constructor for CMyClass throw an exception |
| 19 | static bool gCopyConstructorShouldThow = false; |
| 20 | |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 21 | class CMyClass { |
Marshall Clow | cbb9305 | 2014-03-04 20:31:21 +0000 | [diff] [blame] | 22 | public: CMyClass(int tag); |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame] | 23 | public: CMyClass(const CMyClass& iOther); |
| 24 | public: ~CMyClass(); |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 25 | |
Marshall Clow | cbb9305 | 2014-03-04 20:31:21 +0000 | [diff] [blame] | 26 | bool equal(const CMyClass &rhs) const |
| 27 | { return fTag == rhs.fTag && fMagicValue == rhs.fMagicValue; } |
Marshall Clow | d07fcd6 | 2015-03-09 17:08:51 +0000 | [diff] [blame] | 28 | |
Marshall Clow | cbb9305 | 2014-03-04 20:31:21 +0000 | [diff] [blame] | 29 | private: |
| 30 | int fMagicValue; |
| 31 | int fTag; |
| 32 | |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame] | 33 | private: static int kStartedConstructionMagicValue; |
| 34 | private: static int kFinishedConstructionMagicValue; |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | // Value for fMagicValue when the constructor has started running, but not yet finished |
| 38 | int CMyClass::kStartedConstructionMagicValue = 0; |
| 39 | // Value for fMagicValue when the constructor has finished running |
| 40 | int CMyClass::kFinishedConstructionMagicValue = 12345; |
| 41 | |
Marshall Clow | cbb9305 | 2014-03-04 20:31:21 +0000 | [diff] [blame] | 42 | CMyClass::CMyClass(int tag) : |
| 43 | fMagicValue(kStartedConstructionMagicValue), fTag(tag) |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 44 | { |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame] | 45 | // Signal that the constructor has finished running |
| 46 | fMagicValue = kFinishedConstructionMagicValue; |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Marshall Clow | cbb9305 | 2014-03-04 20:31:21 +0000 | [diff] [blame] | 49 | CMyClass::CMyClass(const CMyClass& iOther) : |
| 50 | fMagicValue(kStartedConstructionMagicValue), fTag(iOther.fTag) |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 51 | { |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame] | 52 | // If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue |
| 53 | if (gCopyConstructorShouldThow) { |
| 54 | throw std::exception(); |
| 55 | } |
| 56 | // Signal that the constructor has finished running |
| 57 | fMagicValue = kFinishedConstructionMagicValue; |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | CMyClass::~CMyClass() { |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame] | 61 | // Only instances for which the constructor has finished running should be destructed |
| 62 | assert(fMagicValue == kFinishedConstructionMagicValue); |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Marshall Clow | cbb9305 | 2014-03-04 20:31:21 +0000 | [diff] [blame] | 65 | bool operator==(const CMyClass &lhs, const CMyClass &rhs) { return lhs.equal(rhs); } |
| 66 | |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 67 | int main() |
| 68 | { |
Marshall Clow | cbb9305 | 2014-03-04 20:31:21 +0000 | [diff] [blame] | 69 | CMyClass instance(42); |
Marshall Clow | d07fcd6 | 2015-03-09 17:08:51 +0000 | [diff] [blame] | 70 | { |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame] | 71 | std::deque<CMyClass> vec; |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 72 | |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame] | 73 | vec.push_back(instance); |
Marshall Clow | cbb9305 | 2014-03-04 20:31:21 +0000 | [diff] [blame] | 74 | std::deque<CMyClass> vec2(vec); |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 75 | |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame] | 76 | gCopyConstructorShouldThow = true; |
| 77 | try { |
| 78 | vec.push_back(instance); |
Marshall Clow | d07fcd6 | 2015-03-09 17:08:51 +0000 | [diff] [blame] | 79 | assert(false); |
| 80 | } |
| 81 | catch (...) { |
| 82 | gCopyConstructorShouldThow = false; |
| 83 | assert(vec==vec2); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | { |
| 88 | typedef std::deque<CMyClass, test_allocator<CMyClass> > C; |
| 89 | C vec; |
| 90 | C vec2(vec); |
| 91 | |
| 92 | C::allocator_type::throw_after = 1; |
| 93 | try { |
| 94 | vec.push_back(instance); |
| 95 | assert(false); |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame] | 96 | } |
| 97 | catch (...) { |
Marshall Clow | cbb9305 | 2014-03-04 20:31:21 +0000 | [diff] [blame] | 98 | assert(vec==vec2); |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame] | 99 | } |
Marshall Clow | d07fcd6 | 2015-03-09 17:08:51 +0000 | [diff] [blame] | 100 | } |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 101 | } |