1 //Author: Daniel Zeligman

    2 //GameObject.h : Header for component container

    3 #pragma once

    4 #include <string>

    5 #include <vector>

    6 #include <map>

    7 

    8 class Component;

    9 

   10 //Base GameObject Component holder

   11 class GameObject

   12 {

   13 public:

   14     /**

   15     * Base constructor

   16     * @param id the GO id

   17     */

   18     GameObject(const std::string& id);

   19 

   20     /**

   21     * Base destructor

   22     */

   23     ~GameObject(void);

   24 

   25     /**

   26     * Get the string ID of this Game Object

   27     * @return the std::string ID

   28     */

   29     const std::string& getID();

   30 

   31     /**

   32     * set the string ID of this Game Object

   33     * @param id std::string

   34     * @throws IllegalParameterException if id is empty

   35     */

   36     void setID( const std::string& id );

   37 

   38     /**

   39     * Add a new component to this game object

   40     * Will replace a old component of the same family if that family already exists

   41     * @param component the new component to be added

   42     * @throws IllegalParameterException if component is NULL

   43     */

   44     void addComponent(Component* component);

   45 

   46     /**

   47     * Remove the component of the corresponding family

   48     * @param componentFamily the family type

   49     * @throws IllegalParameterException if componentFamily is empty

   50     */

   51     void removeComponent(const std::string& componentFamily,const std::string& compID);

   52 

   53 

   54     /**

   55     * Used for a few components that need a member pointer to another for efficient use

   56     * Get a component based off its family ID

   57     * This will return the component with its actual type

   58     * thus not requiring a dynamic downcast

   59     * @param id the component ID

   60     * @return the component index

   61     */

   62     template<class T>

   63     T* GetInterface(const std::string& familyID,const std::string& compID )

   64     {

   65         if(mComponents[familyID][compID] == 0)

   66         {

   67             return reinterpret_cast<T*>(NULL);

   68         }

   69         return reinterpret_cast<T*>( mComponents[familyID][compID] );

   70     }

   71 

   72     /**

   73     * Register a component of this object based off its ID

   74     * @param id the family ID

   75     * @param compID

   76     * @param p the component

   77     */

   78     template<class T>

   79     void RegisterInterface(const std::string& id,const std::string& compID, T* p )

   80     {

   81         mComponents[id][compID]  = p;

   82     }

   83     /**

   84     * clear and delete all Components

   85     */

   86     void clearGOCs();

   87 

   88 

   89 private:

   90     //uncopyable

   91     explicit GameObject(const GameObject& go);

   92     GameObject& operator=(const GameObject& go);

   93 

   94     std::string mOID;            //unique identifier for this object

   95     //Key 1: Family ID, Key 2:Component ID

   96     typedef std::map<const std::string, std::map<const std::string,Component*> > ComponentMap;

   97     ComponentMap mComponents;

   98 

   99 

  100 };

  101 

  102