1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- //
- #pragma once
- #include "TmplBlockBuffer.tlh"
- //-----------------------------------------------------------------------------
- // TmplBuffer_Base
- // 基本类
- //-----------------------------------------------------------------------------
- template <typename T, typename PTR>
- T * ECOM::Utility::TmplBuffer_Base <T, PTR>::_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 <typename T, typename PTR>
- void ECOM::Utility::TmplBuffer_Base <T, PTR>::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 <typename T, typename PTR>
- void ECOM::Utility::TmplBuffer_Base <T, PTR>::MemCopyTo (T * to, int count) const
- {
- if (count <= 0) return;
- if (! to) return;
- if (IsEmpty ()) return;
- memcpy (to, As (), sizeof (T) * min (count, m_nCount));
- }
- // make a real copy
- template <typename T, typename PTR>
- void ECOM::Utility::TmplBuffer_Base <T, PTR>::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 <typename T, typename PTR>
- void ECOM::Utility::TmplBuffer_Base <T, PTR>::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;
- }
- // 如果要的太多了, 重新分配, 然后 memcpy
- {
- auto pDst = To._GetBufferSetCount (count);
- auto pSrc = As ();
- memcpy (pDst, pSrc, sizeof (T) * m_nCount);
- }
- }
|