콘솔 프로젝트 하나 만드셔서 그대로 replace 하시고 런~ 해보시면
금방 이해가 되실 겁니다~
차후 설명 버전을 ^^''''
그럼 즐빌하세요~
//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#pragma hdrstop
#include <queue>
#include <boost/any.hpp>
#include <boost/shared_ptr.hpp>
using boost::any_cast;
using boost::shared_ptr;
typedef std::deque<boost::any> td_deque_any;
typedef std::deque<boost::any>::iterator td_deque_any_iter;
//---------------------------------------------------------------------------
class TZPacket
{
public:
TZPacket(AnsiString szHello) { FHello = szHello; }
AnsiString FHello;
AnsiString sayHello() { return FHello; }
};
typedef boost::shared_ptr<TZPacket> ZPacket;
typedef boost::any ANY_PARAM;
#pragma argsused
td_deque_any mylist;
void addElement( ANY_PARAM pElement )
{
mylist.push_back( pElement );
}
ANY_PARAM getElementAt( int iIndex )
{
if( mylist.size() <= iIndex )
{
#ifdef APP_DOUBT
zout.trace("%s%s\n", APP_DOUBT, "TZNormalQueue::getElementAt | FList->Count <= iIndex");
#endif
return NULL;
}
return mylist[iIndex];
}
void insertElementAt( int iIndex, ANY_PARAM pElement )
{
td_deque_any_iter a = mylist.begin() + iIndex;
mylist.insert(a, pElement);
}
ANY_PARAM removeElementAt( int iIndex )
{
if( mylist.size() <= iIndex )
{
#ifdef APP_DOUBT
zout.trace("%s%s\n", APP_DOUBT, "removeElementAt | FList->Count <= iIndex");
#endif
return NULL;
}
ANY_PARAM p = mylist[iIndex];
td_deque_any_iter a = mylist.begin() + iIndex;
mylist.erase(a);
return p;
}
ANY_PARAM removeFirst()
{
ANY_PARAM p = mylist.front();
mylist.pop_front();
return p;
}
ANY_PARAM removeEnd()
{
ANY_PARAM p = mylist[mylist.size()-1];
mylist.pop_back();
return p;
}
int main(int argc, char* argv[])
{
ZPacket packet1( new TZPacket("Hello") );
ZPacket packet2( new TZPacket("Hi!") );
printf("packet 1 use count : %d\n", packet1.use_count());
printf("packet 2 use count : %d\n", packet2.use_count());
printf("-----\n");
addElement( packet1 );
printf("packet 1 use count : %d\n", packet1.use_count());
printf("packet 2 use count : %d\n", packet2.use_count());
printf("-----\n");
addElement( packet2 );
printf("packet 1 use count : %d\n", packet1.use_count());
printf("packet 2 use count : %d\n", packet2.use_count());
printf("-----\n");
addElement( packet1 );
printf("packet 1 use count : %d\n", packet1.use_count());
printf("packet 2 use count : %d\n", packet2.use_count());
printf("-----\n");
insertElementAt( 0, ZPacket(new TZPacket("One")) );
addElement( ZPacket(new TZPacket("Two")) );
addElement( ZPacket(new TZPacket("Three")) );
addElement( ZPacket(new TZPacket("Four")) );
printf("vlist count = %d\n", mylist.size());
for( int i = 0; i < mylist.size(); i ++ )
{
printf( "%s\n", any_cast<ZPacket>(getElementAt(i))->sayHello() );
}
printf("-----\n");
ZPacket end = any_cast<ZPacket>(removeEnd());
ZPacket front = any_cast<ZPacket>(removeFirst());
printf("%s\n", front->sayHello());
printf("%s\n", end->sayHello());
printf("-----\n");
ZPacket cur;
while( mylist.size() )
{
//cur = any_cast<ZPacket>(removeFirst());
//cur = any_cast<ZPacket>(removeEnd());
cur = any_cast<ZPacket>(removeElementAt(0));
printf("cur say : %s\n", cur->sayHello());
printf("cur use count : %d\n", cur.use_count());
}
printf("vlist count = %d\n", mylist.size());
printf("packet1 use count : %d\n", packet1.use_count());
printf("packet2 use count : %d\n", packet2.use_count());
system("pause");
return 0;
}
|
//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#pragma hdrstop
#include <queue>
#include <boost/any.hpp>
#include <boost/shared_ptr.hpp>
using boost::any_cast;
using boost::shared_ptr;
typedef std::deque<boost::any> td_deque_any;
typedef std::deque<boost::any>::iterator td_deque_any_iter;
//---------------------------------------------------------------------------
class TZPacket
{
public:
TZPacket(AnsiString szHello) { FHello = szHello; }
AnsiString FHello;
AnsiString sayHello() { return FHello; }
};
typedef boost::shared_ptr<TZPacket> ZPacket;
typedef boost::any ANY_PARAM;
#pragma argsused
class TZQueue
{
public:
TZQueue();
virtual ~TZQueue();
td_deque_any FList;
td_deque_any getList() { return FList; }
void addElement( ANY_PARAM pElement );
ANY_PARAM getElementAt( int iIndex );
void insertElementAt( int iIndex, ANY_PARAM pElement );
ANY_PARAM removeElementAt( int iIndex );
ANY_PARAM removeFirst();
ANY_PARAM removeEnd();
};
TZQueue::TZQueue()
{
}
TZQueue::~TZQueue()
{
}
void TZQueue::addElement( ANY_PARAM pElement )
{
FList.push_back( pElement );
}
ANY_PARAM TZQueue::getElementAt( int iIndex )
{
if( FList.size() <= iIndex )
{
#ifdef APP_DOUBT
zout.trace("%s%s\n", APP_DOUBT, "TZNormalQueue::getElementAt | FList->Count <= iIndex");
#endif
return NULL;
}
return FList[iIndex];
}
void TZQueue::insertElementAt( int iIndex, ANY_PARAM pElement )
{
td_deque_any_iter a = FList.begin() + iIndex;
FList.insert(a, pElement);
}
ANY_PARAM TZQueue::removeElementAt( int iIndex )
{
if( FList.size() <= iIndex )
{
#ifdef APP_DOUBT
zout.trace("%s%s\n", APP_DOUBT, "removeElementAt | FList->Count <= iIndex");
#endif
return NULL;
}
ANY_PARAM p = FList[iIndex];
td_deque_any_iter a = FList.begin() + iIndex;
FList.erase(a);
return p;
}
ANY_PARAM TZQueue::removeFirst()
{
ANY_PARAM p = FList.front();
FList.pop_front();
return p;
}
ANY_PARAM TZQueue::removeEnd()
{
ANY_PARAM p = FList[FList.size()-1];
FList.pop_back();
return p;
}
int main(int argc, char* argv[])
{
{
TZQueue q;
printf("list caps %d\n", q.getList().max_size());
ZPacket packet1( new TZPacket("Hello") );
ZPacket packet2( new TZPacket("Hi!") );
printf("list caps %d\n", q.getList().max_size());
printf("list current size %d\n", q.getList().size());
printf("packet 1 use count : %d\n", packet1.use_count());
printf("packet 2 use count : %d\n", packet2.use_count());
printf("-----\n");
q.addElement( packet1 );
printf("packet 1 use count : %d\n", packet1.use_count());
printf("packet 2 use count : %d\n", packet2.use_count());
printf("-----\n");
q.addElement( packet2 );
printf("packet 1 use count : %d\n", packet1.use_count());
printf("packet 2 use count : %d\n", packet2.use_count());
printf("-----\n");
q.addElement( packet1 );
printf("packet 1 use count : %d\n", packet1.use_count());
printf("packet 2 use count : %d\n", packet2.use_count());
printf("-----\n");
q.insertElementAt( 0, ZPacket(new TZPacket("One")) );
q.addElement( ZPacket(new TZPacket("Two")) );
q.addElement( ZPacket(new TZPacket("Three")) );
q.addElement( ZPacket(new TZPacket("Four")) );
printf("list current size %d\n", q.getList().size());
system("pause");
for( int i = 0; i < 300; i++ )
{
//q.insertElementAt( 0, ZPacket(new TZPacket(AnsiString("Gen ") + IntToStr(i))) );
q.addElement( ZPacket(new TZPacket(AnsiString("Gen ") + IntToStr(i))) );
}
printf("list current size %d\n", q.getList().size());
system("pause");
printf("vlist count = %d\n", q.getList().size());
for( int i = 0; i < q.getList().size(); i ++ )
{
printf( "%s\n", any_cast<ZPacket>(q.getElementAt(i))->sayHello() );
}
printf("-----\n");
ZPacket end = any_cast<ZPacket>(q.removeEnd());
ZPacket front = any_cast<ZPacket>(q.removeFirst());
printf("%s\n", front->sayHello());
printf("%s\n", end->sayHello());
printf("list current size %d\n", q.getList().size());
system("pause");
printf("-----\n");
ZPacket cur;
while( q.getList().size() )
{
cur = any_cast<ZPacket>(q.removeFirst());
//cur = any_cast<ZPacket>(removeEnd());
//cur = any_cast<ZPacket>(removeElementAt(0));
printf("cur say : %s\n", cur->sayHello());
printf("cur use count : %d\n", cur.use_count());
}
printf("list current size %d\n", q.getList().size());
system("pause");
printf("vlist count = %d\n", q.getList().size());
printf("packet1 use count : %d\n", packet1.use_count());
printf("packet2 use count : %d\n", packet2.use_count());
system("pause");
q.getList().clear();
system("pause");
}
system("pause");
return 0;
}