Line data Source code
1 : // Copyright 2024 Accenture. 2 : 3 : #include "estd/uncopyable.h" 4 : 5 : #include <gtest/gtest.h> 6 : 7 : #ifdef __clang__ 8 : #pragma clang diagnostic ignored "-Wunused-private-field" 9 : #endif 10 : 11 : /*Below code shows how to make a class uncopyable that cannot inherit 12 : from the uncopyable base class. 13 : */ 14 : // [EXAMPLE_BEGIN uncopyable macro] 15 : class MyClass 16 : { 17 : UNCOPYABLE(MyClass); 18 : 19 : public: 20 1 : MyClass(int n) : n(n) {} 21 : 22 : private: 23 : int n; 24 : }; 25 : 26 1 : void example_uncopyable_macro() 27 : { 28 1 : MyClass obj(10); 29 : // Usage of copy construction calls will result in compilation error. 30 : // MyClass obj1 = obj; 31 : // MyClass obj2(obj); 32 0 : } 33 : 34 : // [EXAMPLE_END uncopyable macro] 35 : // Below code shows how to make a class uncopyable by inheriting from uncopyable 36 : // [EXAMPLE_BEGIN uncopyable class] 37 : class MyClass1 : public ::estd::uncopyable 38 : { 39 : public: 40 1 : MyClass1(int n) : n(n) {} 41 : 42 : private: 43 : int n; 44 : }; 45 : 46 1 : void example_uncopyable_inheritance() 47 : { 48 1 : MyClass1 obj(10); 49 : // Usage of copy construction calls will result in compilation error. 50 : // MyClass1 obj1 = obj; 51 : // MyClass1 obj2(obj); 52 0 : } 53 : 54 : // [EXAMPLE_END uncopyable class] 55 : 56 3 : TEST(uncopyable, run_examples) 57 : { 58 1 : example_uncopyable_inheritance(); 59 1 : example_uncopyable_macro(); 60 1 : }