/* * policy-test.cc version 4. * * Ownership, storage, conversion and checking policies implemented. */ #include #include "smart_pointer.h" #include "testutils.icc" using namespace std; #define REJECT_NULL #ifdef NO_COPY using Smart_Pointer = smart_pointer; #elif DEEP_COPY using Smart_Pointer = smart_pointer; #elif DESTRUCT_COPY using Smart_Pointer = smart_pointer; #elif REFERENCE_COUNTED using Smart_Pointer = smart_pointer; #endif int main() try { #ifndef REJECT_NULL Smart_Pointer sp1(); #else Smart_Pointer sp1{ new X{ 1 } }; // One X is created here. #endif try { cout << sp1->get_m() << endl; } catch (const exception& e) { cout << e.what() << " for 'sp1->get_m()'" << endl; } Smart_Pointer sp2{ new X{ 2 } }; // Array with five X is created here. #ifndef NO_COPY Smart_Pointer sp3{ sp2 }; // An X is copied here. #else Smart_Pointer sp3{ new X{ 3 } }; // An X is created here. #endif // Smart_Pointer x{ sp2 }; // Compile error if NO_COPY try { cout << "sp2->get_m(): " << sp2->get_m() << endl; } catch (const exception& e) { cout << e.what() << " for 'sp2->get_m()'" << endl; } try { cout << "sp3->get_m(): " << sp3->get_m() << endl; } catch (const exception& e) { cout << e.what() << " for 'sp3->get_m()'" << endl; } #ifndef NO_COPY sp1 = sp3; // An X is copied here. #endif try { cout << "sp3->get_m(): " << sp3->get_m() << endl; } catch (const exception& e) { cout << e.what() << " for 'sp3->get_m()'" << endl; } try { cout << "sp1->get_m(): " << sp1->get_m() << endl; } catch (const exception& e) { cout << e.what() << " for 'sp1->get_m()'" << endl; } try { cout << "sp2->get_m(): " << sp2->get_m() << endl; } catch (const exception& e) { cout << e.what() << " for 'sp2->get_m()'" << endl; } #ifndef NO_COPY X* xp = sp1; cout << "xp->get_m(): " << xp->get_m() << endl; #endif return 0; // 2-4 X-es are now to be destructed. } catch (const exception& e) { cout << e.what() << endl; } catch (...) { cout << "Caught an exception not of type 'exception'" << endl; }