Skip to content

Commit

Permalink
fix Mooophy#351 and refacor to c++14
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooophy committed Nov 2, 2015
1 parent 14878f4 commit 8c9c621
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 52 deletions.
5 changes: 3 additions & 2 deletions ch16/ex16.28/delete.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @author Yue Wang
* @date 04 Feb 2014
* Jul 2015
* Oct 2015
* @remark This code is for the exercises from C++ Primer 5th Edition
* @note
***************************************************************************/
Expand All @@ -14,9 +15,9 @@ namespace cp5
struct Delete
{
template<typename T>
void operator() (T* p) const
auto operator() (T* p) const
{
delete p;
}
};
}
}
15 changes: 8 additions & 7 deletions ch16/ex16.28/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* @file main.cpp
* @author Yue Wang
* @date 04 Feb 2014
* Jul 2015
* Jul 2015
* Oct 2015
* @remark This code is for the exercises from C++ Primer 5th Edition
* @note
***************************************************************************/
Expand All @@ -16,14 +17,14 @@

int main()
{
auto sp = cp5::SharedPointer<int>{ new int(42) };
auto sp2 = sp;
std::cout << *sp << std::endl;
std::cout << sp.use_count() << std::endl;
auto foo = cp5::SharedPointer<int>{ new int(42) };
auto bar(foo) ;
std::cout << *foo << std::endl;
std::cout << foo.use_count() << std::endl;

auto string_ptr = cp5::SharedPointer<std::string>{ new std::string{ "Yue" } };
std::cout << *string_ptr << std::endl;
std::cout << string_ptr->size() << std::endl;

return 0;
}
}
84 changes: 41 additions & 43 deletions ch16/ex16.28/shared_pointer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @author Yue Wang
* @date 04 Feb 2014
* Jul 2015
* Oct 2015
* @remark This code is for the exercises from C++ Primer 5th Edition
* @note
***************************************************************************/
Expand All @@ -16,8 +17,8 @@ namespace cp5
template<typename T>
class SharedPointer;

template<typename T>
void swap(SharedPointer<T>& lhs, SharedPointer<T>& rhs)
template<typename T>
auto swap(SharedPointer<T>& lhs, SharedPointer<T>& rhs)
{
using std::swap;
swap(lhs.ptr, rhs.ptr);
Expand All @@ -33,13 +34,13 @@ namespace cp5
// Default Ctor
//
SharedPointer()
: ptr{ nullptr }, ref_count{ new std::size_t(1) }, deleter{ cp5::Delete{ } }
: ptr{ nullptr }, ref_count{ new std::size_t(1) }, deleter{ cp5::Delete{} }
{ }
//
// Ctor that takes raw pointer
//
explicit SharedPointer(T* raw_ptr)
: ptr{ raw_ptr }, ref_count{ new std::size_t(1) }, deleter{ cp5::Delete{ } }
: ptr{ raw_ptr }, ref_count{ new std::size_t(1) }, deleter{ cp5::Delete{} }
{ }
//
// Copy Ctor
Expand All @@ -53,7 +54,7 @@ namespace cp5
// Move Ctor
//
SharedPointer(SharedPointer && other) noexcept
: ptr{ other.ptr }, ref_count{ other.ref_count }, deleter{ std::move (other.deleter) }
: ptr{ other.ptr }, ref_count{ other.ref_count }, deleter{ std::move(other.deleter) }
{
other.ptr = nullptr;
other.ref_count = nullptr;
Expand All @@ -63,8 +64,8 @@ namespace cp5
//
SharedPointer& operator=(SharedPointer const& rhs)
{
//increment first to ensure safty when self-assignment
++*rhs.ref_count;
//increment first to ensure safty for self-assignment
++*rhs.ref_count;
decrement_and_destroy();
ptr = rhs.ptr, ref_count = rhs.ref_count, deleter = rhs.deleter;
return *this;
Expand All @@ -81,63 +82,63 @@ namespace cp5
//
// Conversion operator
//
operator bool() const
{
return ptr ? true : false;
operator bool() const
{
return ptr ? true : false;
}
//
// Dereference
//
T& operator* () const
{
return *ptr;
T& operator* () const
{
return *ptr;
}
//
// Arrow
//
T* operator->() const
{
return &this->operator *();
T* operator->() const
{
return &*ptr;
}
//
// Use count
//
std::size_t use_count() const
{
return *ref_count;
auto use_count() const
{
return *ref_count;
}
//
// Get underlying pointer
//
T* get() const noexcept
{
return ptr;
auto get() const
{
return ptr;
}
//
// Check if the unique user
//
bool unique() const noexcept
{
return 1 == *refCount;
auto unique() const
{
return 1 == *refCount;
}
//
// Swap
//
void swap(SharedPointer& rhs)
{
::swap(*this, rhs);
auto swap(SharedPointer& rhs)
{
::swap(*this, rhs);
}
//
// Free the object pointed to, if unique
//
void reset() noexcept
{
decrement_and_destroy();
auto reset()
{
decrement_and_destroy();
}
//
// Reset with the new raw pointer
//
void reset(T* pointer)
auto reset(T* pointer)
{
if (ptr != pointer)
{
Expand All @@ -149,7 +150,7 @@ namespace cp5
//
// Reset with raw pointer and deleter
//
void reset(T *pointer, const std::function<void(T*)>& d)
auto reset(T *pointer, const std::function<void(T*)>& d)
{
reset(pointer);
deleter = d;
Expand All @@ -166,18 +167,15 @@ namespace cp5
std::size_t* ref_count;
std::function<void(T*)> deleter;

void decrement_and_destroy()
auto decrement_and_destroy()
{
if (ptr)
{
if (0 == --*ref_count)
{
delete ref_count;
deleter(ptr);
}
}
if (ptr && 0 == --*ref_count)
delete ref_count,
deleter(ptr);
else if (!ptr)
delete ref_count;
ref_count = nullptr;
ptr = nullptr;
}
};
}//namespace
}//namespace

0 comments on commit 8c9c621

Please sign in to comment.