// #pragma once #include #include "TmplBlockBuffer.tlh" //----------------------------------------------------------------------------- // TmplBuffer_Base // 基本类 //----------------------------------------------------------------------------- template T * ECOM::Utility::TmplBuffer_Base ::_GetBufferSetCount (int count) { if (m_nCount >= count) return m_Data.get (); T * pOld = m_Data.get (); T * pNew = new T [count]; memcpy (pNew, pOld, sizeof (T) * m_nCount); Attach (pNew, count); return m_Data.get (); } template void ECOM::Utility::TmplBuffer_Base ::MemCopyFrom (const T * from, int count) { Release (); if (count <= 0) return; if (! from) return; T * toData = new T [count]; ASSERT (toData); memcpy (toData, from, sizeof (T) * count); Attach (toData, count); } template void ECOM::Utility::TmplBuffer_Base ::MemCopyTo (T * to, int count) const { if (count <= 0) return; if (! to) return; if (IsEmpty ()) return; memcpy (to, As (), sizeof (T) * (std::min) (count, m_nCount)); } // make a real copy template void ECOM::Utility::TmplBuffer_Base ::MemCopyTo (TmplBuffer_Base & To) const { if (IsEmpty ()) return; if (m_nCount <= 0) return; T * toData = new T [m_nCount]; ASSERT (toData); memcpy (toData, m_Data.get (), sizeof (T) * m_nCount); To.Attach (toData, m_nCount); } template void ECOM::Utility::TmplBuffer_Base ::ResizeTo (int count, TmplBuffer_Base & To) const { // 如果啥都不想要 if (count <= 0) return; // 如果我自己本来就是 null if (IsEmpty ()) { To._GetBufferSetCount (count); return; } // 如果要的很少, 只需要修改 m_nCount if (count <= m_nCount) { To.m_nCount = count; To.m_Data = this->m_Data; } else // 如果要的太多了, 重新分配, 然后 memcpy { auto pDst = To._GetBufferSetCount (count); auto pSrc = As (); memcpy (pDst, pSrc, sizeof (T) * m_nCount); } } template ECOM::Utility::TmplSharedBuffer ::TmplSharedBuffer (TmplUniqueBuffer && from) { this->Release (); if (from) { auto count = from.GetCount (); auto pc = from.Detach (); this->Attach (pc, count); } } template ECOM::Utility::TmplSharedBuffer & ECOM::Utility::TmplSharedBuffer ::operator = (ECOM::Utility::TmplUniqueBuffer && from) { this->Release (); if (from) { auto count = from.GetCount (); auto pc = from.Detach (); this->Attach (pc, count); } return (*this); }