⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mytype.h

📁 电力故障信息采集,主要是针对南自的保护装置,这个程序用在Linux操作系统下
💻 H
字号:
#ifndef MYTYPE_H#define MYTYPE_H// #include <qstring.h>#include "baseclass/YsString.h"#include <time.h>typedef unsigned char BYTE;typedef ushort WORD;typedef unsigned int DWORD;typedef bool BOOL;typedef QString CString;template<class TYPE>inline void ConstructElement(TYPE* pElements, int nCount){    //	ASSERT(nCount == 0 || AfxIsValidAddress(pElements, nCount * sizeof(TYPE)));    if (nCount == 0)    {        return;    }    // first do bit-wise zero initialization    memset((void*)pElements, 0, nCount * sizeof(TYPE));    // then call the constructor(s)    for (; nCount--; pElements++)        ::new((void*)pElements) TYPE;}template<class TYPE>inline void DestructElement(TYPE* pElements, int nCount){    //	ASSERT(nCount == 0 || AfxIsValidAddress(pElements, nCount * sizeof(TYPE)));    if (nCount == 0)    {        return;    }    // call the destructor(s)    for (; nCount--; pElements++)        pElements->~TYPE();}template<class TYPE>inline void CopyElement(TYPE* pDest, const TYPE* pSrc, int nCount){    //	ASSERT(nCount == 0 || AfxIsValidAddress(pDest, nCount * sizeof(TYPE)));    //	ASSERT(nCount == 0 || AfxIsValidAddress(pSrc, nCount * sizeof(TYPE)));    if (nCount == 0)    {        return;    }    // default is element-copy using assignment    while (nCount--)        *pDest++ = *pSrc++;}/////////////////////////////////////////////////////////////////////////////// CMyArray<TYPE, ARG_TYPE>template<class TYPE, class ARG_TYPE>class CMyArray{public:    // Construction    CMyArray();    ~CMyArray();    // Attributes    int GetSize() const;    int GetUpperBound() const;    void SetSize(int nNewSize, int nGrowBy = -1);    // Operations    // Clean up    void FreeExtra();    void RemoveAll();    // Accessing elements    TYPE GetAt(int nIndex) const;    void SetAt(int nIndex, ARG_TYPE newElement);    TYPE& ElementAt(int nIndex);    // Direct Access to the element data (may return NULL)    const TYPE* GetData() const;    TYPE* GetData();    // Potentially growing the array    void SetAtGrow(int nIndex, ARG_TYPE newElement);    int Add(ARG_TYPE newElement);    int Append(const CMyArray& src);    void Copy(const CMyArray& src);    // overloaded operator helpers    TYPE operator[](int nIndex) const;    TYPE& operator[](int nIndex);    // Operations that move elements around    void InsertAt(int nIndex, ARG_TYPE newElement, int nCount = 1);    void RemoveAt(int nIndex, int nCount = 1);    void InsertAt(int nStartIndex, CMyArray* pNewArray);    // Implementationprotected:    TYPE* m_pData;   // the actual array of data    int m_nSize;     // # of elements (upperBound - 1)    int m_nMaxSize;  // max allocated    int m_nGrowBy;   // grow amount};/////////////////////////////////////////////////////////////////////////////// CMyArray<TYPE, ARG_TYPE> inline functionstemplate<class TYPE, class ARG_TYPE>inline int CMyArray<TYPE, ARG_TYPE>::GetSize() const{    return m_nSize;}template<class TYPE, class ARG_TYPE>inline int CMyArray<TYPE, ARG_TYPE>::GetUpperBound() const{    return m_nSize-1;}template<class TYPE, class ARG_TYPE>inline void CMyArray<TYPE, ARG_TYPE>::RemoveAll(){    SetSize(0, -1);}template<class TYPE, class ARG_TYPE>inline TYPE CMyArray<TYPE, ARG_TYPE>::GetAt(int nIndex) const{    if (nIndex >= 0 && nIndex < m_nSize)    {        return m_pData[nIndex];    }}template<class TYPE, class ARG_TYPE>inline void CMyArray<TYPE, ARG_TYPE>::SetAt(int nIndex, ARG_TYPE newElement){    if (nIndex >= 0 && nIndex < m_nSize)    {        m_pData[nIndex] = newElement;    }}template<class TYPE, class ARG_TYPE>inline TYPE& CMyArray<TYPE, ARG_TYPE>::ElementAt(int nIndex){    if (nIndex >= 0 && nIndex < m_nSize)    {        return m_pData[nIndex];    }}template<class TYPE, class ARG_TYPE>inline const TYPE* CMyArray<TYPE, ARG_TYPE>::GetData() const{    return (const TYPE*)m_pData;}template<class TYPE, class ARG_TYPE>inline TYPE* CMyArray<TYPE, ARG_TYPE>::GetData(){    return (TYPE*)m_pData;}template<class TYPE, class ARG_TYPE>inline int CMyArray<TYPE, ARG_TYPE>::Add(ARG_TYPE newElement){    int nIndex = m_nSize;    SetAtGrow(nIndex, newElement);    return nIndex;}template<class TYPE, class ARG_TYPE>inline TYPE CMyArray<TYPE, ARG_TYPE>::operator[](int nIndex) const{    return GetAt(nIndex);}template<class TYPE, class ARG_TYPE>inline TYPE& CMyArray<TYPE, ARG_TYPE>::operator[](int nIndex){    return ElementAt(nIndex);}/////////////////////////////////////////////////////////////////////////////// CMyArray<TYPE, ARG_TYPE> out-of-line functionstemplate<class TYPE, class ARG_TYPE>CMyArray<TYPE, ARG_TYPE>::CMyArray(){    m_pData = NULL;    m_nSize = m_nMaxSize = m_nGrowBy = 0;}template<class TYPE, class ARG_TYPE>CMyArray<TYPE, ARG_TYPE>::~CMyArray(){    if (this == NULL)    {        return;    }    if (m_pData != NULL)    {        DestructElement<TYPE>(m_pData, m_nSize);        delete[] (BYTE*)m_pData;    }}template<class TYPE, class ARG_TYPE>void CMyArray<TYPE, ARG_TYPE>::SetSize(int nNewSize, int nGrowBy){    if (this == NULL || nNewSize < 0)    {        return;    }    if (nGrowBy != -1)        m_nGrowBy = nGrowBy;  // set new size    if (nNewSize == 0)    {        // shrink to nothing        if (m_pData != NULL)        {            DestructElement<TYPE>(m_pData, m_nSize);            delete[] (BYTE*)m_pData;            m_pData = NULL;        }        m_nSize = m_nMaxSize = 0;    }    else if (m_pData == NULL)    {        // create one with exact size        m_pData = (TYPE*) new BYTE[nNewSize * sizeof(TYPE)];        ConstructElement<TYPE>(m_pData, nNewSize);        m_nSize = m_nMaxSize = nNewSize;    }    else if (nNewSize <= m_nMaxSize)    {        // it fits        if (nNewSize > m_nSize)        {            // initialize the new elements            ConstructElement<TYPE>(&m_pData[m_nSize], nNewSize-m_nSize);        }        else if (m_nSize > nNewSize)        {            // destroy the old elements            DestructElement<TYPE>(&m_pData[nNewSize], m_nSize-nNewSize);        }        m_nSize = nNewSize;    }    else    {        // otherwise, grow array        int nGrowBy = m_nGrowBy;        if (nGrowBy == 0)        {            // heuristically determine growth when nGrowBy == 0            //  (this avoids heap fragmentation in many situations)            nGrowBy = m_nSize / 8;            nGrowBy = (nGrowBy < 4) ? 4 : ((nGrowBy > 1024) ? 1024 : nGrowBy);        }        int nNewMax;        if (nNewSize < m_nMaxSize + nGrowBy)            nNewMax = m_nMaxSize + nGrowBy;  // granularity        else            nNewMax = nNewSize;  // no slush        if (nNewMax < m_nMaxSize)        {            return;        }        TYPE* pNewData = (TYPE*) new BYTE[nNewMax * sizeof(TYPE)];        // copy new data from old        memcpy(pNewData, m_pData, m_nSize * sizeof(TYPE));        // construct remaining elements        if (nNewSize <= m_nSize)        {            return;        }        ConstructElement<TYPE>(&pNewData[m_nSize], nNewSize-m_nSize);        // get rid of old stuff (note: no destructors called)        delete[] (BYTE*)m_pData;        m_pData = pNewData;        m_nSize = nNewSize;        m_nMaxSize = nNewMax;    }}template<class TYPE, class ARG_TYPE>int CMyArray<TYPE, ARG_TYPE>::Append(const CMyArray& src){    if (this == NULL || this == &src)    {        return -1;    }    int nOldSize = m_nSize;    SetSize(m_nSize + src.m_nSize);    CopyElement<TYPE>(m_pData + nOldSize, src.m_pData, src.m_nSize);    return nOldSize;}template<class TYPE, class ARG_TYPE>void CMyArray<TYPE, ARG_TYPE>::Copy(const CMyArray& src){    if (this == NULL || this == &src)    {        return;    }    SetSize(src.m_nSize);    CopyElement<TYPE>(m_pData, src.m_pData, src.m_nSize);}template<class TYPE, class ARG_TYPE>void CMyArray<TYPE, ARG_TYPE>::FreeExtra(){    if (this == NULL)    {        return;    }    if (m_nSize != m_nMaxSize)    {        // shrink to desired size        TYPE* pNewData = NULL;        if (m_nSize != 0)        {            pNewData = (TYPE*) new BYTE[m_nSize * sizeof(TYPE)];            // copy new data from old            memcpy(pNewData, m_pData, m_nSize * sizeof(TYPE));        }        // get rid of old stuff (note: no destructors called)        delete[] (BYTE*)m_pData;        m_pData = pNewData;        m_nMaxSize = m_nSize;    }}template<class TYPE, class ARG_TYPE>void CMyArray<TYPE, ARG_TYPE>::SetAtGrow(int nIndex, ARG_TYPE newElement){    if (this == NULL || nIndex < 0)    {        return;    }    if (nIndex >= m_nSize)        SetSize(nIndex+1, -1);    m_pData[nIndex] = newElement;}template<class TYPE, class ARG_TYPE>void CMyArray<TYPE, ARG_TYPE>::InsertAt(int nIndex, ARG_TYPE newElement, int nCount /*=1*/){    if (this == NULL || nIndex < 0 || nCount <= 0 || nIndex+nCount > m_nSize)    {        return;    }    if (nIndex >= m_nSize)    {        // adding after the end of the array        SetSize(nIndex + nCount, -1);   // grow so nIndex is valid    }    else    {        // inserting in the middle of the array        int nOldSize = m_nSize;        SetSize(m_nSize + nCount, -1);  // grow it to new size        // destroy intial data before copying over it        DestructElement<TYPE>(&m_pData[nOldSize], nCount);        // shift old data up to fill gap        memmove(&m_pData[nIndex+nCount], &m_pData[nIndex],                (nOldSize-nIndex) * sizeof(TYPE));        // re-init slots we copied from        ConstructElement<TYPE>(&m_pData[nIndex], nCount);    }    // insert new value in the gap    while (nCount--)        m_pData[nIndex++] = newElement;}template<class TYPE, class ARG_TYPE>void CMyArray<TYPE, ARG_TYPE>::RemoveAt(int nIndex, int nCount){    if (this == NULL || nIndex < 0 || nCount < 0 || nIndex+nCount > m_nSize)    {        return;    }    // just remove a range    int nMoveCount = m_nSize - (nIndex + nCount);    DestructElement<TYPE>(&m_pData[nIndex], nCount);    if (nMoveCount)        memmove(&m_pData[nIndex], &m_pData[nIndex + nCount], nMoveCount * sizeof(TYPE));    m_nSize -= nCount;}template<class TYPE, class ARG_TYPE>void CMyArray<TYPE, ARG_TYPE>::InsertAt(int nStartIndex, CMyArray* pNewArray){    if (this == NULL || nStartIndex < 0 || pNewArray == NULL)    {        return;    }    if (pNewArray->GetSize() > 0)    {        InsertAt(nStartIndex, pNewArray->GetAt(0), pNewArray->GetSize());        for (int i = 0; i < pNewArray->GetSize(); i++)            SetAt(nStartIndex + i, pNewArray->GetAt(i));    }}typedef CMyArray<BYTE, BYTE> CByteArray;typedef CMyArray<WORD, WORD> CWordArray;//typedef CMyArray CArray;#endif/////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -