Skip to content

Commit

Permalink
feat: 修复只能指针的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
tangruo committed Sep 12, 2016
1 parent 51a8f9d commit dae3bdb
Show file tree
Hide file tree
Showing 18 changed files with 920 additions and 86 deletions.
64 changes: 61 additions & 3 deletions Ballet/Common/BalHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace Ballet
BaseT::useCount_?BaseT::useCount_->AddUseCount(true):(void)0;
}

template<typename U>explicit BalHandle(T* object)
template<typename U> explicit BalHandle(T* object)
{
if (nullptr_() == object) return;
BaseT::useCount_ = new(std::nothrow)U(object);
Expand All @@ -63,20 +63,44 @@ namespace Ballet
throw std::runtime_error("BalHandle Construct Failed");
}

BalHandle(const BalHandle& handle) throw()
BalHandle(const BalHandle<T>& handle) throw()
{
BaseT::object_ = handle.object_;
BaseT::useCount_ = handle.useCount_;
BaseT::useCount_?BaseT::useCount_->AddUseCount(true):(void)0;
}

template <typename U>
explicit BalHandle(const BalHandle<U>& handle) throw()
{
if (!handle) return;
BaseT::object_ = dynamic_cast<T*>(handle.GetBasePtr());
if (nullptr_() != BaseT::object_)
{
BaseT::useCount_ = handle.useCount_;
BaseT::useCount_?BaseT::useCount_->AddUseCount(true):(void)0;
}
}

BalHandle(const BalWeakHandle<T>& handle) throw()
{
BaseT::object_ = handle.object_;
BaseT::useCount_ = handle.useCount_;
BaseT::useCount_?BaseT::useCount_->AddUseCount(true):(void)0;
}

template <typename U>
BalHandle(const BalWeakHandle<U>& handle) throw()
{
if (!handle) return;
BaseT::object_ = dynamic_cast<T*>(handle.GetBasePtr());
if (nullptr_() != BaseT::object_)
{
BaseT::useCount_ = handle.useCount_;
BaseT::useCount_?BaseT::useCount_->AddUseCount(true):(void)0;
}
}

inline ~BalHandle()
{
this->Clear();
Expand All @@ -99,7 +123,7 @@ namespace Ballet
{
bool del = false;
BaseT::useCount_->DelUseCount(true, del);
if (del) BaseT::useCount_ = nullptr_();
BaseT::object_ = nullptr_(); BaseT::useCount_ = nullptr_();
}
}

Expand All @@ -122,6 +146,23 @@ namespace Ballet
return *this;
}

template <typename U>
inline BalHandle& operator=(const BalHandle<U>& handle) throw()
{
if (BaseT::HashCode() == handle.HashCode())
{
return *this;
}

this->Clear();
BaseT::object_ = dynamic_cast<T*>(handle.GetBasePtr());
if (nullptr_() != BaseT::object_)
{
BaseT::useCount_ = handle.useCount_;
BaseT::useCount_?BaseT::useCount_->AddUseCount(true):(void)0;
}
}

inline BalHandle& operator=(const BalWeakHandle<T>& handle) throw()
{
if (BaseT::HashCode() == handle.HashCode())
Expand All @@ -135,6 +176,23 @@ namespace Ballet
BaseT::useCount_?BaseT::useCount_->AddUseCount(true):(void)0;
return *this;
}

template <typename U>
inline BalHandle& operator=(const BalWeakHandle<T>& handle) throw()
{
if (BaseT::HashCode() == handle.HashCode())
{
return *this;
}

this->Clear();
BaseT::object_ = dynamic_cast<T*>(handle.GetBasePtr());
if (nullptr_() != BaseT::object_)
{
BaseT::useCount_ = handle.useCount_;
BaseT::useCount_?BaseT::useCount_->AddUseCount(true):(void)0;
}
}
};
}
#endif
9 changes: 9 additions & 0 deletions Ballet/Common/BalHandleBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ namespace Ballet
return 0;
}

inline long UseHoldCount() const throw()
{
if (useCount_)
{
return useCount_->useCount_;
}
return 0;
}

inline T* GetBasePtr() const throw()
{
return object_;
Expand Down
73 changes: 64 additions & 9 deletions Ballet/Common/BalWeakHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,53 @@ namespace Ballet
{
}

BalWeakHandle(const BalWeakHandle& handle) throw()
template <typename U>
BalWeakHandle(const BalWeakHandle<U>& handle, T* object) throw()
{
BaseT::object_ = handle.object_;
if (nullptr_() == object) return;
BaseT::object_ = object;
BaseT::useCount_ = handle.useCount_;
BaseT::useCount_?BaseT::useCount_->AddUseCount(false):(void)0;
}

template<typename U>
BalWeakHandle(const BalWeakHandle<U>& handle, T* object) throw()
BalWeakHandle(const BalWeakHandle& handle) throw()
{
if (nullptr_() == object) return;
BaseT::object_ = object;
BaseT::object_ = handle.object_;
BaseT::useCount_ = handle.useCount_;
BaseT::useCount_?BaseT::useCount_->AddUseCount(false):(void)0;
}

template <typename U>
BalWeakHandle(const BalWeakHandle<U>& handle) throw()
{
if (!handle) return;
BaseT::object_ = dynamic_cast<T*>(handle.GetBasePtr());
if (nullptr_() != BaseT::object_)
{
BaseT::useCount_ = handle.useCount_;
BaseT::useCount_?BaseT::useCount_->AddUseCount(false):(void)0;
}
}

BalWeakHandle(const BalHandle<T>& handle) throw()
{
BaseT::object_ = handle.object_;
BaseT::useCount_ = handle.useCount_;
BaseT::useCount_?BaseT::useCount_->AddUseCount(false):(void)0;
}

template <typename U>
BalWeakHandle(const BalHandle<U>& handle) throw()
{
if (!handle) return;
BaseT::object_ = dynamic_cast<T*>(handle.GetBasePtr());
if (nullptr_() != BaseT::object_)
{
BaseT::useCount_ = handle.useCount_;
BaseT::useCount_?BaseT::useCount_->AddUseCount(false):(void)0;
}
}

inline ~BalWeakHandle()
{
this->Clear();
Expand All @@ -60,16 +84,29 @@ namespace Ballet
}

inline BalWeakHandle& operator=(const BalWeakHandle& handle) throw()
{
T* object = handle.object_;
BalUseCount* count = handle.useCount_;
count? count->AddUseCount(false) :(void)0; this->Clear();
BaseT::object_ = object; BaseT::useCount_ = count;
return *this;
}

template <typename U>
inline BalWeakHandle& operator=(const BalWeakHandle<U>& handle) throw()
{
if (BaseT::HashCode() == handle.HashCode())
{
return *this;
}

this->Clear();
BaseT::object_ = handle.object_;
BaseT::useCount_ = handle.useCount_;
BaseT::useCount_?BaseT::useCount_->AddUseCount(false):(void)0;
BaseT::object_ = dynamic_cast<T*>(handle.GetBasePtr());
if (nullptr_() != BaseT::object_)
{
BaseT::useCount_ = handle.useCount_;
BaseT::useCount_?BaseT::useCount_->AddUseCount(false):(void)0;
}
return *this;
}

Expand All @@ -86,6 +123,24 @@ namespace Ballet
BaseT::useCount_?BaseT::useCount_->AddUseCount(false):(void)0;
return *this;
}

template <typename U>
inline BalWeakHandle& operator=(const BalHandle<T>& handle) throw()
{
if (BaseT::HashCode() == handle.HashCode())
{
return *this;
}

this->Clear();
BaseT::object_ = dynamic_cast<T*>(handle.GetBasePtr());
if (nullptr_() != BaseT::object_)
{
BaseT::useCount_ = handle.useCount_;
BaseT::useCount_?BaseT::useCount_->AddUseCount(false):(void)0;
}
return *this;
}
};
}
#endif
3 changes: 2 additions & 1 deletion Ballet/Network/BalBufferStream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ size_t BalBufferStream::Capacity() const

bool BalBufferStream::AppendBuffer(const char* buffer, size_t len)
{
if (nullptr_() == buffer || 0 == len) return false;
if (nullptr_() == buffer || 0 >= len) return false;
if (capacity_ - end_ > len)
{
memcpy(buffer_ + end_, buffer, len);
Expand Down Expand Up @@ -77,6 +77,7 @@ bool BalBufferStream::AppendBuffer(const char* buffer, size_t len)

void BalBufferStream::ConsumeBuffer(size_t len)
{
if (0 >= len) return;
start_ += len;
if (start_ > end_) start_ = end_;
}
Expand Down
33 changes: 20 additions & 13 deletions Ballet/Network/BalEventLoop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ bool BalEventLoop::AddHoldSomeElement(int id, BalHandle<BalElement>& element)
mapHoldPoolT::iterator iter = holdElementPool_.find(id);
if (iter != holdElementPool_.end()) return false;
holdElementPool_[id] = element;
return true;
}

bool BalEventLoop::RemoveHoldElement(int id)
Expand Down Expand Up @@ -95,6 +96,7 @@ bool BalEventLoop::DeleteEventListener(int id, BalEventEnum event)

mapEventPoolT::iterator iter = eventPool_.find(id);
if (eventPool_.end() == iter) false;

struct epoll_event ev;
memset(&ev, 0, sizeof(epoll_event));
ev.data.fd = id; ev.events = iter->second.status_;
Expand Down Expand Up @@ -148,7 +150,6 @@ bool BalEventLoop::DoEventSelect(int time)
const static int MAX_READ_FD = 10240;
struct epoll_event events[MAX_READ_FD];
int nfds = ::epoll_wait(efd_, events, MAX_READ_FD, time);

timer_->Tick();

for (int i = 0; i < nfds; ++i)
Expand Down Expand Up @@ -191,17 +192,17 @@ bool BalEventLoop::DoExitEventLoop()

bool BalEventLoop::DoReadyPool(BalHandle<BalEventLoop> eventLoop)
{
if (!eventLoop) return false;
if (!eventLoop || 0 == readyPool_.size()) return false;

doReadyPoolProtected_ = true;
size_t len = readyPool_.size() -1;
for (size_t i = 0; i <= len;)
bool resize = false;
int32_t len = (uint32_t)readyPool_.size();
for (int32_t i = 0; i < len;)
{
bool closed = false;
BalEventReadyItem& item = readyPool_[i];
if (!item.callback_ || !item.callback_->IsCallable()) continue;
if (!item.callback_ || !(item.callback_->IsCallable())) continue;
BalEventCallbackEnum ret = EventRetNone;

if (0 != item.read_)
{
ret = item.callback_->ShouldRead(item.fd_, eventLoop);
Expand All @@ -215,7 +216,7 @@ bool BalEventLoop::DoReadyPool(BalHandle<BalEventLoop> eventLoop)
}
}

if (false == close && 0 != item.write_)
if (false == closed && 0 != item.write_)
{
ret = item.callback_->ShouldWrite(item.fd_, eventLoop);
if (EventRetClose == ret)
Expand All @@ -228,26 +229,32 @@ bool BalEventLoop::DoReadyPool(BalHandle<BalEventLoop> eventLoop)
}
}

if (close || (0 == item.write_ && 0 == item.read_))
if (closed || (0 == item.write_ && 0 == item.read_))
{
if (i < len)
int lastItemIndex = len -1;
if (i < lastItemIndex)
{
readyPool_[i].iter_->second.index_ = -1;
readyPool_[i] = readyPool_[len];
readyPool_[i] = readyPool_[lastItemIndex];
readyPool_[i].iter_->second.index_ = i;
}
else
{
readyPool_[len].iter_->second.index_ = -1;
readyPool_[i].iter_->second.index_ = -1;
}
readyPool_[len].callback_.Clear();
readyPool_.resize(len); --len;
readyPool_[lastItemIndex].callback_.Clear();
--len; resize = true;
}
else
{
++i;
}
}

if (true == resize)
{
readyPool_.resize(len);
}
doReadyPoolProtected_ = false;
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions Ballet/Network/BalSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ namespace Ballet
public:
int GetFd() const {return fd_;}
bool IsV6Socket() const;
bool SetReuseAddr(bool set) throw();
bool SetReusePort(bool set) throw();
virtual bool SetReuseAddr(bool set) throw();
virtual bool SetReusePort(bool set) throw();

protected:
uint32_t ReadBuffer(char* buffer, uint32_t) const;
Expand Down
Loading

0 comments on commit dae3bdb

Please sign in to comment.