C#의 DataTable 형식을 본따서 만들어보려고 했는데 linq가 없으니 죄다 for문으로 검색해야함.
object 타입이 없어서 포인터 가지고 데이터를 핸들링하는데 데이터 입력시 캐스팅 바꿔줘야함. 너무 구림.. ㅋㅋ
그래도 만든 시간이 아까워서 올려놓음.
DataTable.h
#include<vector>
#include <string>
#include "DataType.h"
#include <map>
///Row행에 모든 형태의 데이터를 담기위해 포인터로 핸들링합니다.
///NewRow()로 동적 할당합니다.
///NewRow로 row 생성하고 Add 하지 않는 경우 delete를 꼭 해주세요.
///
using namespace std;
using namespace DataType;
namespace DataTable
{
struct DataColumn
{
string key;
string type;
DataColumn(string key, string type)
{
this->key = key;
this->type = type;
}
};
class CDataColumnCollection
{
private :
std::vector<DataColumn> columns;
public:
void Add(string name, string type);
void Add(DataColumn column);
void Clear();
int Size()
{
return columns.size();
}
DataColumn& operator[](int _idx)
{
return columns[_idx];
}
DataColumn& operator[](string _columnName)
{
for(int i=0; i<columns.size(); i++)
{
if(columns[i].key == _columnName)
{
return columns[i];
}
}
}
};
class RowBase
{
public:
RowBase(){}
~RowBase(){}
};
template <typename T>
class RowField : public RowBase
{
public:
~RowField<T>(){};
RowField(T val): m_Value(val){}
T m_Value;
T& operator = (T value)
{
return m_Value = value;
}
};
class CDataRow
{
private :
CDataColumnCollection columnCollection;
std::map<string, RowBase*> fields;
public:
~CDataRow()
{
if(fields.size() > 0)
{
auto iter = fields.begin();
while(iter !=fields.end())
{
delete iter->second;
fields.erase(iter++);
}
}
}
CDataRow(CDataColumnCollection columns)
{
this->columnCollection = columns;
for(int i=0; i< this->columnCollection.Size(); i++)
{
if(columnCollection[i].type == typeid(int).name())
{
fields[columnCollection[i].key] = new RowField<int>(0);
}
else if(columnCollection[i].type == typeid(double).name())
{
fields[columnCollection[i].key] = new RowField<double>(0);
}
else if(columnCollection[i].type == typeid(long).name())
{
fields[columnCollection[i].key] = new RowField<long>(0);
}
else if(columnCollection[i].type == typeid(string).name())
{
fields[columnCollection[i].key] = new RowField<string>("");
}
}
}
public:
RowBase* operator[](string key)
{
for(auto iter = fields.begin(); iter !=fields.end(); ++iter)
{
if(iter->first == key)
{
return iter->second;
}
}
return NULL;
}
};
class CRowCollection
{
private :
std::vector<CDataRow*> rows;
public:
~CRowCollection()
{
for (auto it = rows.begin() ; it != rows.end(); ++it)
{
delete (*it);
}
rows.clear();
}
CDataRow* operator[](int _idx)
{
return rows[_idx];
}
void Add(CDataRow *row)
{
rows.push_back(row);
}
void Remove(int index);
void Clear();
int Count();
};
class CDataTable
{
public:
CDataTable(void):name(""){};
CDataTable(string name):name(name){};
~CDataTable(void)
{
columns.Clear();
}
private:
string name;
CDataColumnCollection columns;
CRowCollection rows;
public :
void Clear();
CDataColumnCollection& GetColumns();
CRowCollection& GetRows();
CDataRow* NewRow()
{
return new CDataRow(columns);
}
};
}
'개발관련 > C&C++' 카테고리의 다른 글
Convert Json To Xml (0) | 2018.01.17 |
---|---|
Xmllite SAX(Simple Api for Xml) TreeNode 구현 및 LIB (XMLParser) (0) | 2018.01.11 |
IOCP ThreadPool 구현 Lib 및 사용 방법 (0) | 2018.01.11 |
임계영역(Critical Section) (0) | 2018.01.05 |
아두이노>초음파 센서를 이용한 물 높이 측정 (0) | 2017.07.27 |