From ab9b09225a72045368f2a18bd48c5531bc2e8726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Fri, 13 Mar 2015 19:06:41 +0800 Subject: [PATCH] ref --- TinySTL/Detail/Ref.h | 17 ++++++++++++++--- TinySTL/Test/RefTest.cpp | 26 ++++++++++++++++++++++++++ TinySTL/Test/RefTest.h | 18 ++++++++++++++++++ TinySTL/TinySTL.vcxproj | 2 ++ TinySTL/TinySTL.vcxproj.filters | 6 ++++++ TinySTL/main.cpp | 2 ++ 6 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 TinySTL/Test/RefTest.cpp create mode 100644 TinySTL/Test/RefTest.h diff --git a/TinySTL/Detail/Ref.h b/TinySTL/Detail/Ref.h index 9f22598..18b0de5 100644 --- a/TinySTL/Detail/Ref.h +++ b/TinySTL/Detail/Ref.h @@ -10,9 +10,12 @@ namespace TinySTL{ std::atomic ncount_; T *data_; - explicit ref_t(size_t n = 0, T *p = nullptr) :ncount_(n), data_(p){} - ref_t(const ref_t&) = default; - ref_t& operator = (const ref_t&) = default; + explicit ref_t(T *p = nullptr): ncount_(0), data_(p){ + if (data_) + ncount_ = 1; + } + ref_t(const ref_t&) = delete; + ref_t& operator = (const ref_t&) = delete; size_t count()const{ return ncount_.load(); } T *get_data()const{ return data_; } @@ -36,6 +39,14 @@ namespace TinySTL{ return t; } }; + template + bool operator ==(const ref_t& lhs, const ref_t& rhs){ + return lhs.get_data() == rhs.get_data(); + } + template + bool operator !=(const ref_t& lhs, const ref_t& rhs){ + return !(lhs == rhs); + } } } diff --git a/TinySTL/Test/RefTest.cpp b/TinySTL/Test/RefTest.cpp new file mode 100644 index 0000000..d009526 --- /dev/null +++ b/TinySTL/Test/RefTest.cpp @@ -0,0 +1,26 @@ +#include "RefTest.h" + +namespace TinySTL{ + namespace RefTest{ + void testCaseRef(){ + ref_t r1; + assert(r1.count() == 0); + assert(r1.get_data() == nullptr); + + int n = 0; + ref_t r2(&n); + assert(r2.count() == 1); + assert(r2.get_data() != nullptr); + + ++r2; + assert(r2.count() == 2); + + --r2; + assert(r2.count() == 1); + } + + void testAllCases(){ + testCaseRef(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/RefTest.h b/TinySTL/Test/RefTest.h new file mode 100644 index 0000000..85a0eb4 --- /dev/null +++ b/TinySTL/Test/RefTest.h @@ -0,0 +1,18 @@ +#ifndef _REF_TEST_H_ +#define _REF_TEST_H_ + +#include "../Detail/Ref.h" + +#include + +namespace TinySTL{ + namespace RefTest{ + template + using ref_t = TinySTL::Detail::ref_t < T > ; + + void testCaseRef(); + void testAllCases(); + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/TinySTL.vcxproj b/TinySTL/TinySTL.vcxproj index 8ed7924..7bd7aef 100644 --- a/TinySTL/TinySTL.vcxproj +++ b/TinySTL/TinySTL.vcxproj @@ -96,6 +96,7 @@ + @@ -146,6 +147,7 @@ + diff --git a/TinySTL/TinySTL.vcxproj.filters b/TinySTL/TinySTL.vcxproj.filters index a452549..3c2d0a6 100644 --- a/TinySTL/TinySTL.vcxproj.filters +++ b/TinySTL/TinySTL.vcxproj.filters @@ -99,6 +99,9 @@ Test + + Test + @@ -266,6 +269,9 @@ Detail + + Test + diff --git a/TinySTL/main.cpp b/TinySTL/main.cpp index 26c4292..f58bc0d 100644 --- a/TinySTL/main.cpp +++ b/TinySTL/main.cpp @@ -14,6 +14,7 @@ #include "Test\PairTest.h" #include "Test\PriorityQueueTest.h" #include "Test\QueueTest.h" +#include "Test\RefTest.h" #include "Test\StackTest.h" #include "Test\StringTest.h" #include "Test\SuffixArrayTest.h" @@ -36,6 +37,7 @@ int main(){ TinySTL::PairTest::testAllCases(); TinySTL::PriorityQueueTest::testAllCases(); TinySTL::QueueTest::testAllCases(); + TinySTL::RefTest::testAllCases(); TinySTL::StackTest::testAllCases(); TinySTL::StringTest::testAllCases(); TinySTL::SuffixArrayTest::testAllCases();