From 357bfe71e62970752bf32029f506b2e96967b1d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Gl=C3=B6ckner?= Date: Thu, 31 Mar 2016 08:10:44 +0200 Subject: Fixed API of class Atomic for POD --- src/atomic.h | 5 +++++ test/atomictest.cc | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) 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 class Atomic::value>::type> : public std::atomic { + + public: + // inherit methods + using std::atomic::atomic; + using std::atomic::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 tmp; } + void podAtomicCanBeDefaultInitialized() { + Atomic i; + // note: i is initialized with garbage + } + + void nonPodAtomicCanBeDefaultInitialized() { + Atomic s; + CPPUNIT_ASSERT_EQUAL(s.load(), std::string{}); + } + + void podAtomicCanBeValueInitialized() { + Atomic i{5}; + CPPUNIT_ASSERT_EQUAL(i.load(), 5); + } + + void nonPodAtomicCanBeValueInitialized() { + Atomic s{"hello world"}; + CPPUNIT_ASSERT_EQUAL(s.load(), std::string{"hello world"}); + } + + void podAtomicCanBeValueAssigned() { + Atomic i; + i = 5; + CPPUNIT_ASSERT_EQUAL(i.load(), 5); + } + + void nonPodAtomicCanBeValueAssigned() { + Atomic s; + s = "hello world"; + CPPUNIT_ASSERT_EQUAL(s.load(), std::string{"hello world"}); + } + // todo: further testing private: -- cgit v1.2.3