cd81e6028e2d39a160370dda267594c857a17e3f
[platform/framework/web/crosswalk.git] / src / third_party / libc++ / trunk / test / containers / sequences / vector / vector.modifiers / push_back_exception_safety.pass.cpp
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 // <vector>
11
12 // void push_back(const value_type& x);
13
14 #include <vector>
15 #include <cassert>
16
17 // Flag that makes the copy constructor for CMyClass throw an exception
18 static bool gCopyConstructorShouldThow = false;
19
20
21 class CMyClass {
22     public: CMyClass();
23     public: CMyClass(const CMyClass& iOther);
24     public: ~CMyClass();
25
26     private: int fMagicValue;
27
28     private: static int kStartedConstructionMagicValue;
29     private: static int kFinishedConstructionMagicValue;
30 };
31
32 // Value for fMagicValue when the constructor has started running, but not yet finished
33 int CMyClass::kStartedConstructionMagicValue = 0;
34 // Value for fMagicValue when the constructor has finished running
35 int CMyClass::kFinishedConstructionMagicValue = 12345;
36
37 CMyClass::CMyClass() :
38     fMagicValue(kStartedConstructionMagicValue)
39 {
40     // Signal that the constructor has finished running
41     fMagicValue = kFinishedConstructionMagicValue;
42 }
43
44 CMyClass::CMyClass(const CMyClass& /*iOther*/) :
45     fMagicValue(kStartedConstructionMagicValue)
46 {
47     // If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue
48     if (gCopyConstructorShouldThow) {
49         throw std::exception();
50     }
51     // Signal that the constructor has finished running
52     fMagicValue = kFinishedConstructionMagicValue;
53 }
54
55 CMyClass::~CMyClass() {
56     // Only instances for which the constructor has finished running should be destructed
57     assert(fMagicValue == kFinishedConstructionMagicValue);
58 }
59
60 int main()
61 {
62     CMyClass instance;
63     std::vector<CMyClass> vec;
64
65     vec.push_back(instance);
66
67     gCopyConstructorShouldThow = true;
68     try {
69         vec.push_back(instance);
70     }
71     catch (...) {
72     }
73 }