ne_ziggy

Component Object Model (c++)

May 23rd, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #ifndef COM_INIT_H
  2. #define COM_INIT_H
  3.  
  4. #include <cstdio>
  5. #include <map>
  6.  
  7. typedef unsigned long uuid;
  8.  
  9. template <class T>
  10. class Component {
  11. public:
  12.     static std::map<uuid, T*> $;
  13.     static typename std::map<uuid, T*>::iterator it$;
  14.  
  15.     static T* Create(uuid fID);
  16.     static void Iterate(void (T::*pFunc)() );
  17.     static bool Exists(uuid fID);
  18. };
  19.  
  20. template <typename T>
  21. std::map<uuid, T*> Component<T>::$;
  22.  
  23. template <typename T>
  24. typename std::map<uuid, T*>::iterator Component<T>::it$;
  25.  
  26. template <typename T>
  27. T* Component<T>::Create(uuid fID) {
  28.     Component<T>::$[fID] = new T(fID);
  29.     return Component<T>::$[fID];
  30. }
  31.  
  32. template <typename T>
  33. void Component<T>::Iterate(void (T::*pFunc)() ) {
  34.     for(it$ = $.begin(); it$ != $.end(); it$++) {
  35.         //Can only iterate functions with no arguments :/
  36.         (it$->second->*pFunc)();
  37.     }
  38. }
  39.  
  40. template <typename T>
  41. bool Component<T>::Exists(uuid fID) {
  42.     // This might be expensive.
  43.     // Is there a better alternative or a way to optimize?
  44.     if($.find(fID) != $.end()) return true;
  45.     else return false;
  46. }
  47.  
  48.  
  49.  
  50.  
  51. class COM {
  52. protected:
  53.     COM(uuid fID) : ID(fID) {}
  54.     uuid ID;
  55. };
  56.  
  57.  
  58. #endif //COM_INIT_H
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. #ifndef COM_POSITION_H_
  68. #define COM_POSITION_H_
  69.  
  70. class COM_Position : public COM {
  71.     float x, y, z;
  72. public:
  73.     COM_Position(uuid fID);
  74.  
  75.     void Set(float fX, float fY, float fZ);
  76.     void Move(float fX, float fY, float fZ);
  77.     void Display(void);
  78. };
  79.  
  80. COM_Position::COM_Position(uuid fID) : COM(fID) { x = 0; y = 0; z = 0; }
  81. void COM_Position::Set(float fX, float fY, float fZ) { x = fX; y = fY; z = fZ; }
  82. void COM_Position::Move(float fX, float fY, float fZ) { x += fX; y += fY; z += fZ; }
  83. void COM_Position::Display() {
  84.     printf("COM_Position\[%lu\]: (%02f, %02f, %02f)\n", ID, x, y, z);
  85. }
  86.  
  87. typedef Component<COM_Position> Position;
  88.  
  89. #endif //COM_POSITION_H
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. int main() {
  97.     for(int ii = 0; ii < 4; ii++) { Position::Create(ii); }
  98.     Position::$[1]->Set(1, 2, 3);
  99.     Position::$[1]->Move(1, 2, 3);
  100.     Position::$[2]->Set(3, 2, 1);
  101.     Position::$[3]->Set(4, 5, 6);
  102.  
  103.     Position::Iterate(&COM_Position::Display);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment