diff options
| author | Christian Glöckner <cgloeckner@freenet.de> | 2016-03-31 08:10:44 +0200 | 
|---|---|---|
| committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2016-03-31 21:15:00 +0200 | 
| commit | 0d2668121d2b3e188dc60cb7bdb5d9dcc39beab4 (patch) | |
| tree | 18022fbc02a22cd57de4c9348a8dfdc52f9745f1 | |
| parent | d1d1e525a579ab55e1ffb8acc1b269360df5b1e9 (diff) | |
Fixed API of class Atomic for POD
| -rw-r--r-- | src/atomic.h | 5 | ||||
| -rw-r--r-- | test/atomictest.cc | 38 | 
2 files changed, 43 insertions, 0 deletions
diff --git a/src/atomic.h b/src/atomic.h index f800f68..1b92257 100644 --- a/src/atomic.h +++ b/src/atomic.h @@ -37,6 +37,11 @@ class Atomic;  template <typename T>  class Atomic<T, typename std::enable_if<std::is_pod<T>::value>::type>  	: public std::atomic<T> { +	 +	public: +		// inherit methods +		using std::atomic<T>::atomic; +		using std::atomic<T>::operator=;  };  // else work around it using a mutex diff --git a/test/atomictest.cc b/test/atomictest.cc index d4cacf0..d73695e 100644 --- a/test/atomictest.cc +++ b/test/atomictest.cc @@ -36,6 +36,12 @@ class AtomicTest  	CPPUNIT_TEST(atomicFloatUsesStandardImpl);  	CPPUNIT_TEST(atomicBoolUsesStandardImpl);  	CPPUNIT_TEST(atomicStringCanBeUsed); +	CPPUNIT_TEST(podAtomicCanBeDefaultInitialized); +	CPPUNIT_TEST(nonPodAtomicCanBeDefaultInitialized); +	CPPUNIT_TEST(podAtomicCanBeValueInitialized); +	CPPUNIT_TEST(nonPodAtomicCanBeValueInitialized); +	CPPUNIT_TEST(podAtomicCanBeValueAssigned); +	CPPUNIT_TEST(nonPodAtomicCanBeValueAssigned);  	CPPUNIT_TEST_SUITE_END();  	public: @@ -63,6 +69,38 @@ class AtomicTest  			Atomic<std::string> tmp;  		} +		void podAtomicCanBeDefaultInitialized() { +			Atomic<int> i; +			// note: i is initialized with garbage +		} +		 +		void nonPodAtomicCanBeDefaultInitialized() { +			Atomic<std::string> s; +			CPPUNIT_ASSERT_EQUAL(s.load(), std::string{}); +		} +		 +		void podAtomicCanBeValueInitialized() { +			Atomic<int> i{5}; +			CPPUNIT_ASSERT_EQUAL(i.load(), 5); +		} +		 +		void nonPodAtomicCanBeValueInitialized() { +			Atomic<std::string> s{"hello world"}; +			CPPUNIT_ASSERT_EQUAL(s.load(), std::string{"hello world"}); +		} +		 +		void podAtomicCanBeValueAssigned() { +			Atomic<int> i; +			i = 5; +			CPPUNIT_ASSERT_EQUAL(i.load(), 5); +		} +		 +		void nonPodAtomicCanBeValueAssigned() { +			Atomic<std::string> s; +			s = "hello world"; +			CPPUNIT_ASSERT_EQUAL(s.load(), std::string{"hello world"}); +		} +		  		// todo: further testing  	private:  | 
