/*********************************************************************
Copyright (C) 2000 Smaller Animals Software, Inc.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
http://www.smalleranimals.com
smallest@smalleranimals.com
----------------------------------------------------------------
ISArray - a simple array template.
void myFunc()
{
// allocate an array of 200 chars
// the array is deleted when its destructor is called
// ie. when it goes out of scope
//
ISArray <char> my_array(200);
// clear to char 0's
my_array.Fill((char)0);
// get a pointer
char * pArray = (char *)my_array;
// use the array
pArray[2] = 'c';
// no need to do any clean-up: the array deletes itself
return;
}
**********************************************************************/
#ifndef ISARRAYH
#define ISARRAYH
template <class T> class ISArray
{
public:
/*******************************************************
default ctor
if you use this, you must allocate data with SetSize
*******************************************************/
ISArray()
{
m_ptr = NULL;
m_iCount = 0;
}
/*******************************************************
allocating ctor
*******************************************************/
ISArray(int iCount)
{
m_ptr = NULL;
m_iCount = 0;
SetSize(iCount);
}
/*******************************************************
dtor
*******************************************************/
~ISArray()
{
Clear();
}
/*******************************************************
(re)allocate buffer
*******************************************************/
void SetSize(int iCount)
{
if (iCount!=m_iCount)
{
Clear();
if (iCount > 0)
{
m_ptr = new T [iCount];
if (m_ptr)
{
m_iCount = iCount;
}
}
}
}
/*******************************************************
delete the buffer
*******************************************************/
void Clear()
{
if (m_ptr)
{
delete [] m_ptr;
}
m_ptr = NULL;
m_iCount = 0;
}
/*******************************************************
set all items in the array to a value
*******************************************************/
void Fill(T val)
{
for (int i=0;i<m_iCount;i++)
{
m_ptr[i] = val;
}
}
/*******************************************************
copy input array to this
*******************************************************/
ISArray & operator = (const ISArray <T> & input)
{
SetSize(input.GetCount());
if (m_ptr!=NULL)
{
memcpy(m_ptr, input.GetPtr(), m_iCount * sizeof(T));
}
return *this;
}
/*******************************************************
get a pointer
ISArray<char> bob(200);
char * pBob = bob.GetPtr();
*******************************************************/
_inline T * GetPtr() const
{
return m_ptr;
}
/*******************************************************
get a pointer
ISArray<char> bob(200);
const char * pBob = (const char *)bob;
*******************************************************/
_inline operator const T *() const
{
return m_ptr;
}
/*******************************************************
get a pointer
ISArray<char> bob(200);
char * pBob = (char *)bob;
*******************************************************/
_inline operator T *()
{
return m_ptr;
}
/*******************************************************
number of items in the array
*******************************************************/
_inline int GetCount() const
{
return m_iCount;
}
protected:
int m_iCount;
T * m_ptr;
};
/*******************************************************
some frequently-used flavors
*******************************************************/
typedef ISArray < BYTE > ISByteArray;
typedef ISArray < double > ISDoubleArray;
typedef ISArray < char > ISCharArray;
#endif
isarray_src.zip_visual c
版权申诉
155 浏览量
2022-09-22
17:15:08
上传
评论
收藏 2KB ZIP 举报
Kinonoyomeo
- 粉丝: 77
- 资源: 1万+
最新资源
- 编译原理:正规式转NFA(有穷自动机)
- 通过 Navicat 备份文件读取连接信息和解析密码
- Linux下开箱即用的C++单元测试demo示例,public成员函数单元测试
- 连接Redis服务器 在使用Redis之前,首先需要使用redis-cli工具连接到Redis服务器 redis-cli是Re
- 连接Redis服务器 在使用Redis之前,首先需要使用redis-cli工具连接到Redis服务器 redis-cli是Red
- 连接Redis服务器 在使用Redis之前,首先需要使用redis-cli工具连接到Redis服务器 redis-cli是Red
- redis命令实践 详细教程
- redis命令实践 详细教程
- redis命令实践 详细教程
- redis命令实践 详细教程
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈