This commit is contained in:
邹晓航
2015-03-13 19:06:41 +08:00
parent d31d3df72e
commit ab9b09225a
6 changed files with 68 additions and 3 deletions

View File

@@ -10,9 +10,12 @@ namespace TinySTL{
std::atomic<size_t> 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<class T>
bool operator ==(const ref_t<T>& lhs, const ref_t<T>& rhs){
return lhs.get_data() == rhs.get_data();
}
template<class T>
bool operator !=(const ref_t<T>& lhs, const ref_t<T>& rhs){
return !(lhs == rhs);
}
}
}

26
TinySTL/Test/RefTest.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include "RefTest.h"
namespace TinySTL{
namespace RefTest{
void testCaseRef(){
ref_t<int> r1;
assert(r1.count() == 0);
assert(r1.get_data() == nullptr);
int n = 0;
ref_t<int> r2(&n);
assert(r2.count() == 1);
assert(r2.get_data() != nullptr);
++r2;
assert(r2.count() == 2);
--r2;
assert(r2.count() == 1);
}
void testAllCases(){
testCaseRef();
}
}
}

18
TinySTL/Test/RefTest.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef _REF_TEST_H_
#define _REF_TEST_H_
#include "../Detail/Ref.h"
#include <cassert>
namespace TinySTL{
namespace RefTest{
template<class T>
using ref_t = TinySTL::Detail::ref_t < T > ;
void testCaseRef();
void testAllCases();
}
}
#endif

View File

@@ -96,6 +96,7 @@
<ClCompile Include="Test\PairTest.cpp" />
<ClCompile Include="Test\PriorityQueueTest.cpp" />
<ClCompile Include="Test\QueueTest.cpp" />
<ClCompile Include="Test\RefTest.cpp" />
<ClCompile Include="Test\StackTest.cpp" />
<ClCompile Include="Test\StringTest.cpp" />
<ClCompile Include="Test\SuffixArrayTest.cpp" />
@@ -146,6 +147,7 @@
<ClInclude Include="Test\PairTest.h" />
<ClInclude Include="Test\PriorityQueueTest.h" />
<ClInclude Include="Test\QueueTest.h" />
<ClInclude Include="Test\RefTest.h" />
<ClInclude Include="Test\StackTest.h" />
<ClInclude Include="Test\StringTest.h" />
<ClInclude Include="Test\SuffixArrayTest.h" />

View File

@@ -99,6 +99,9 @@
<ClCompile Include="Test\UniquePtrTest.cpp">
<Filter>Test</Filter>
</ClCompile>
<ClCompile Include="Test\RefTest.cpp">
<Filter>Test</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="TypeTraits.h">
@@ -266,6 +269,9 @@
<ClInclude Include="Detail\Ref.h">
<Filter>Detail</Filter>
</ClInclude>
<ClInclude Include="Test\RefTest.h">
<Filter>Test</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\README.md" />

View File

@@ -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();