2011년 3월 19일 토요일

GLnode.cpp


// GLnode.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include 
#include 
#include 
#include 

#ifndef SAFE_DELETE_VECTOR
#define SAFE_DELETE_VECTOR(x) { for(size_t __i__(0); __i__ < x.size(); ++__i__) delete(x.at(__i__)); x.clear(); }
#endif

template 
class GLcontainer {
public:
 virtual ~GLcontainer()
 {
  SAFE_DELETE_VECTOR(_vec);
 }

 void push_back(T* t)
 {
  _vec.push_back(t);
 }
 void remove(T* t)
 {
  std::vector::iterator pos = std::find(_vec.begin(), _vec.end(), t);
  if (pos != _vec.end())
   _vec.erase(pos);
 }

 const T* at(int pos) const
 {
  return _vec.at(pos);
 }

 const std::vector& get() const
 {
  return _vec;
 }
protected:
 std::vector _vec;
};

template 
class GLcontainerF : public GLcontainer {
 class _find_if_pred {
 private:
  std::string _name;

 public:
  _find_if_pred(const std::string& name)
   : _name(name)
  {
  }
  template 
  bool operator()(const TT* tt)
  {
   return ((_name.compare(tt->_name)) == 0);
  }
 };

public:
 const int find(const std::string& name) const
 {
  std::vector::const_iterator pos = std::find_if(_vec.begin(), _vec.end(), _find_if_pred(name));
  if (pos != _vec.end())
   return (pos - _vec.begin());

  return -1;
 }
 const T* find_ptr(const std::string& name) const
 {
  std::vector::const_iterator pos = std::find_if(_vec.begin(), _vec.end(), _find_if_pred(name));
  if (pos != _vec.end())
   return (*pos);

  return NULL;
 }
};

class GLnode : public GLcontainerF {
 class _for_each_pred {
 public:
  _for_each_pred(float time)
   : _time(time)
  {
  }

  template 
  void operator()(T * t) const
  {
   t->_update(_time);
  }
  float _time;
 };

public:
 GLnode()
 {
 }
 GLnode(const std::string& name)
  : _name(name)
 {
 }
 virtual ~GLnode()
 {
 }

 virtual void update(float time)
 {
  std::for_each(_vec.begin(), _vec.end(), _for_each_pred(time));
 }

protected:
 virtual void _update(float time) const
 {

 }

 /// attribute
public:
 std::string _name;

 /// operation
public:
};

int _tmain(int argc, _TCHAR* argv[])
{
 GLnode _node;

 for (int i=0; i<10; i++)
 {
  char ib[10];
  itoa(i, ib, 10);

  _node.push_back(new GLnode(ib));
 }

 int index = _node.find("9");
 const GLnode * ptr = _node.find_ptr("9");

 const GLnode * res = _node.at(index);

 return 0;
}