Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef COM_INIT_H
- #define COM_INIT_H
- #include <cstdio>
- #include <map>
- typedef unsigned long uuid;
- template <class T>
- class Component {
- public:
- static std::map<uuid, T*> $;
- static typename std::map<uuid, T*>::iterator it$;
- static T* Create(uuid fID);
- static void Iterate(void (T::*pFunc)() );
- static bool Exists(uuid fID);
- };
- template <typename T>
- std::map<uuid, T*> Component<T>::$;
- template <typename T>
- typename std::map<uuid, T*>::iterator Component<T>::it$;
- template <typename T>
- T* Component<T>::Create(uuid fID) {
- Component<T>::$[fID] = new T(fID);
- return Component<T>::$[fID];
- }
- template <typename T>
- void Component<T>::Iterate(void (T::*pFunc)() ) {
- for(it$ = $.begin(); it$ != $.end(); it$++) {
- //Can only iterate functions with no arguments :/
- (it$->second->*pFunc)();
- }
- }
- template <typename T>
- bool Component<T>::Exists(uuid fID) {
- // This might be expensive.
- // Is there a better alternative or a way to optimize?
- if($.find(fID) != $.end()) return true;
- else return false;
- }
- class COM {
- protected:
- COM(uuid fID) : ID(fID) {}
- uuid ID;
- };
- #endif //COM_INIT_H
- #ifndef COM_POSITION_H_
- #define COM_POSITION_H_
- class COM_Position : public COM {
- float x, y, z;
- public:
- COM_Position(uuid fID);
- void Set(float fX, float fY, float fZ);
- void Move(float fX, float fY, float fZ);
- void Display(void);
- };
- COM_Position::COM_Position(uuid fID) : COM(fID) { x = 0; y = 0; z = 0; }
- void COM_Position::Set(float fX, float fY, float fZ) { x = fX; y = fY; z = fZ; }
- void COM_Position::Move(float fX, float fY, float fZ) { x += fX; y += fY; z += fZ; }
- void COM_Position::Display() {
- printf("COM_Position\[%lu\]: (%02f, %02f, %02f)\n", ID, x, y, z);
- }
- typedef Component<COM_Position> Position;
- #endif //COM_POSITION_H
- int main() {
- for(int ii = 0; ii < 4; ii++) { Position::Create(ii); }
- Position::$[1]->Set(1, 2, 3);
- Position::$[1]->Move(1, 2, 3);
- Position::$[2]->Set(3, 2, 1);
- Position::$[3]->Set(4, 5, 6);
- Position::Iterate(&COM_Position::Display);
- }
Advertisement
Add Comment
Please, Sign In to add comment