Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5791 in orxonox.OLD for trunk


Ignore:
Timestamp:
Nov 27, 2005, 3:32:42 AM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: stl::list used in ClassList

Location:
trunk/src/lib
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/lang/base_object.cc

    r5671 r5791  
    7171 * @param className the class name
    7272*/
    73 void BaseObject::setClassID(long classID, const char* className)
     73void BaseObject::setClassID(ClassID classID, const char* className)
    7474{
    75   this->classID |= classID;
     75  this->classID |= (long)classID;
    7676  this->className = className;
    7777
     
    142142 * @returns true on match, false otherwise.
    143143 */
    144 bool BaseObject::operator ==(const char* objectName)
     144bool BaseObject::operator==(const char* objectName)
    145145{
    146146  if (likely(this->objectName != NULL && objectName != NULL))
  • trunk/src/lib/lang/base_object.h

    r5626 r5791  
    3939  void whatIs() const;
    4040
    41   //
    42   bool operator== (const char* objectName);
     41  bool operator==(const char* objectName);
    4342
    4443 protected:
    45   void setClassID(long classID, const char* className);
     44  void setClassID(ClassID classID, const char* className);
    4645
    4746  private:
  • trunk/src/lib/lang/class_list.cc

    r5783 r5791  
    1919#include "base_object.h"
    2020
    21 #include "list.h"
    2221#include "compiler.h"
    2322#include "debug.h"
    2423#include <string.h>
    2524#include <math.h>
     25#include <algorithm>
    2626#include "shell_command.h"
    2727
     
    3737ClassList::ClassList(const long& classID, const char* className)
    3838{
    39   this->next = NULL;
    4039  this->className = className;
    4140  this->classID = classID;
    42 
    43   ++ClassList::classCount;
    4441}
    4542
     
    4946ClassList::~ClassList ()
    5047{
    51 //   ClassList::classList.clear());
    52    --ClassList::classCount;
    53 }
    54 
    55 //! the first class that is registered
    56 ClassList*  ClassList::first = NULL;
    57 
    58 //! the Count of classes
    59 unsigned int ClassList::classCount = 0;
     48//   ClassList::classList->clear());
     49}
     50
     51//! a List of all known Classes.
     52std::list<ClassList>* ClassList::classList = new std::list<ClassList>();
    6053
    6154//! a List of all strings of all classes, that have registered so far.
    62 std::list<const char*> ClassList::classList;
     55std::list<const char*> ClassList::classNames;
    6356
    6457/**
     
    6760 * @param classID ID of the Given ObjectType \see ClassID
    6861 * @param className name of the Class to add
    69  */
    70 void ClassList::addToClassList(BaseObject* objectPointer, const long& classID, const char* className)
    71 {
    72   ClassList* regClass;
    73   PRINTF(5)("subscribe a %s\n", className );
    74 
    75   if(ClassList::first == NULL)
    76     ClassList::first = regClass = new ClassList(classID, className);
     62 *
     63 * !! FIRST YOU HAVE TO CALL THIS FUNCTION ONCE
     64 * !! Before unsing the ClassList, as it creates the ClassLits
     65 */
     66void ClassList::addToClassList(BaseObject* objectPointer, ClassID classID, const char* className)
     67{
     68  if (unlikely(classList == NULL))
     69    ClassList::classList = new list<ClassList>();
     70
     71  PRINTF(5)("subscribe a '%s'\n", className );
     72
     73  ClassList* regClass = ClassList::getClassList(classID);
     74  if (regClass != NULL)
     75    regClass->objectList.push_back(objectPointer);
    7776  else
    7877  {
    79     ClassList* tmp = ClassList::first;
    80     while (likely(tmp != NULL))
    81     {
    82       if (tmp->classID == classID)
    83       {
    84         regClass = tmp;
    85         break;
    86       }
    87 
    88       if (unlikely(tmp->next == NULL))
    89       {
    90         tmp->next = regClass = new ClassList(classID, className);
    91         break;
    92       }
    93       tmp = tmp->next;
    94     }
    95   }
    96   regClass->objectList.push_back(objectPointer);
     78    ClassList::classList->push_back(ClassList(classID, className));
     79    ClassList::classList->back().objectList.push_back(objectPointer);
     80  }
    9781}
    9882
     
    10387void ClassList::removeFromClassList(BaseObject* objectPointer)
    10488{
    105   ClassList* tmp = ClassList::first;
    106   while (likely(tmp != NULL))
    107   {
    108     if (objectPointer->isA(tmp->classID))
    109     {
    110       tmp->objectList.remove(objectPointer);
    111     }
    112     tmp = tmp->next;
    113   }
     89  list<ClassList>::iterator cl;
     90  for(cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++)
     91    if (objectPointer->isA((*cl).classID))
     92      (*cl).objectList.remove(objectPointer);
    11493}
    11594
     
    121100 * befor it changes anything.
    122101 */
    123 const std::list<const char*>* ClassList::getClassList()
    124 {
    125   if (ClassList::classList.size() != ClassList::classCount)
    126     ClassList::classList.clear();
    127 
    128   if(likely(ClassList::first != NULL))
    129   {
    130     ClassList* tmpCL = ClassList::first;
    131     while (likely(tmpCL != NULL))
    132     {
    133       ClassList::classList.push_back(tmpCL->className);
    134       tmpCL = tmpCL->next;
    135     }
    136   }
    137   return &ClassList::classList;
     102const std::list<const char*>* ClassList::getClassNames()
     103{
     104  if (ClassList::classNames.size() != ClassList::classList->size())
     105  {
     106      ClassList::classNames.clear();
     107
     108      list<ClassList>::const_iterator cl;
     109      for (cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++)
     110        ClassList::classNames.push_back((*cl).className);
     111  }
     112
     113  return &ClassList::classNames;
    138114}
    139115
     
    143119 * @return the List accessed by classID, or NULL if not found
    144120 */
    145 std::list<BaseObject*>* ClassList::getList(long classID)
    146 {
    147   if(unlikely(ClassList::first == NULL))
    148     return NULL;
    149   else
    150   {
    151     ClassList* tmpCL = ClassList::first;
    152     while (likely(tmpCL != NULL))
    153     {
    154       if (unlikely(tmpCL->classID == classID))
    155         return &tmpCL->objectList;
    156       tmpCL = tmpCL->next;
    157     }
    158   }
    159   return NULL;
     121std::list<BaseObject*>* ClassList::getList(ClassID classID)
     122{
     123  ClassList* fl;
     124  return ((fl = ClassList::getClassList(classID)) != NULL)?
     125       &(fl->objectList) : NULL;
     126
     127/*
     128  std::list<ClassList>::iterator classIT = find (classList->begin(), classList->end(), classID);
     129  return (likely(classIT != classList->end()))? &(*classIT).objectList : NULL;*/
     130
     131/*  for (classIT = ClassList::classList->begin(); classIT != ClassList::classList->end(); classIT++)
     132  {
     133    if ((*classIT) == classID )
     134      return &(*classIT).objectList;
     135  }
     136  return NULL;*/
    160137}
    161138
     
    167144std::list<BaseObject*>* ClassList::getList(const char* className)
    168145{
    169   if(unlikely(ClassList::first == NULL))
     146  ClassList* fl;
     147  return ((fl = ClassList::getClassList(className)) != NULL)?
     148      &(fl->objectList) : NULL;
     149
     150  /*
     151  std::list<ClassList>::iterator classIT = find (classList->begin(), classList->end(), className);
     152  return (likely(classIT != classList->end()))? &(*classIT).objectList : NULL;*/
     153
     154
     155/*  for (classIT = ClassList::classList->begin(); classIT != ClassList::classList->end(); classIT++)
     156  {
     157    if ((*classIT) == className )
     158      return &(*classIT).objectList;
     159  }
     160  return NULL;*/
     161}
     162
     163
     164ClassList* ClassList::getClassList(ClassID classID)
     165{
     166  std::list<ClassList>::iterator classIT = find (ClassList::classList->begin(), ClassList::classList->end(), classID);
     167  return (likely(classIT != classList->end()))? &(*classIT) : NULL;
     168}
     169
     170
     171ClassList* ClassList::getClassList(const char* className)
     172{
     173  if (className == NULL)
    170174    return NULL;
    171   else
    172   {
    173     ClassList* tmpCL = ClassList::first;
    174     while (likely(tmpCL != NULL))
    175     {
    176       if (unlikely(!strcmp(tmpCL->className, className)))
    177         return &tmpCL->objectList;
    178       tmpCL = tmpCL->next;
    179     }
    180   }
    181   return NULL;
    182 }
     175  std::list<ClassList>::iterator classIT = find (classList->begin(), classList->end(), className);
     176  return (likely(classIT != classList->end()))? &(*classIT) : NULL;
     177}
     178
    183179
    184180/**
    185181 * checks if the BaseObject* object exists.
    186  * @param name the name of the BaseObject to look for
     182 * @param objectName the name of the BaseObject to look for
    187183 * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere.
    188184 * @return true, if the Object Exists in the specified ClassID, false otherwise
    189185 * @todo: speed this up!!
    190186 */
    191 BaseObject* ClassList::getObject(const char* name, long classID)
    192 {
    193   if(unlikely(ClassList::first == NULL) || name == NULL)
    194     return NULL;
     187BaseObject* ClassList::getObject(const char* objectName, ClassID classID)
     188{
     189  if (classID != CL_NULL)
     190  {
     191    ClassList* cl = ClassList::getClassList(classID);
     192    if (cl != NULL)
     193    {
     194      std::list<BaseObject*>::iterator bo;
     195      for (bo = cl->objectList.begin(); bo != cl->objectList.end(); bo++)
     196        if ((*bo)->getName() != NULL && !strcmp((*bo)->getName(), objectName))
     197          return (*bo);
     198    }
     199  }
    195200  else
    196201  {
    197     ClassList* tmp = ClassList::first;
    198     while (likely(tmp != NULL))
    199     {
    200       if (tmp->classID == classID || classID == CL_NULL)
    201       {
    202         list<BaseObject*>::iterator bo;
    203         for (bo = tmp->objectList.begin(); bo != tmp->objectList.end(); bo++)
    204           if ((*bo)->getName() && !strcmp((*bo)->getName(), name))
    205             return (*bo);
    206         break;
    207       }
    208       tmp = tmp->next;
     202    list<ClassList>::iterator cl;
     203    for (cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++)
     204    {
     205      std::list<BaseObject*>::iterator bo;
     206      for (bo = (*cl).objectList.begin(); bo != (*cl).objectList.end(); bo++)
     207        if ((*bo)->getName() != NULL && !strcmp((*bo)->getName(), objectName))
     208          return (*bo);
    209209    }
    210210  }
     
    220220 * @todo: speed this up!!
    221221 */
    222 bool ClassList::exists(const BaseObject* object, long classID)
    223 {
    224   if(unlikely(ClassList::first == NULL) || object == NULL)
    225     return false;
     222bool ClassList::exists(const BaseObject* object, ClassID classID)
     223{
     224  if (classID != CL_NULL)
     225  {
     226    ClassList* cl = ClassList::getClassList(classID);
     227    if (cl != NULL)
     228    {
     229      std::list<BaseObject*>::const_iterator bo = find (cl->objectList.begin(), cl->objectList.end(), object);
     230      return (bo != cl->objectList.end());
     231    }
     232  }
    226233  else
    227234  {
    228     ClassList* tmp = ClassList::first;
    229     while (likely(tmp != NULL))
    230     {
    231       if (tmp->classID == classID || classID == CL_NULL)
    232       {
    233         list<BaseObject*>::iterator bo;
    234         for (bo = tmp->objectList.begin(); bo != tmp->objectList.end(); bo++)
    235           if ((*bo) == object)
    236             return true;
    237         if (likely(tmp->classID == classID))
    238           break;
    239       }
    240       tmp = tmp->next;
     235    list<ClassList>::iterator cl;
     236    for (cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++)
     237    {
     238      std::list<BaseObject*>::const_iterator bo = find ((*cl).objectList.begin(), (*cl).objectList.end(), object);
     239      if (bo != (*cl).objectList.end())
     240        return true;
    241241    }
    242242  }
     
    250250void ClassList::whatIs(const BaseObject* object)
    251251{
    252   ClassList* tmp = ClassList::first;
    253   while (likely(tmp != NULL))
    254   {
    255     if (object->isA(tmp->classID))
    256     {
    257       PRINT(0)("=%s=-", tmp->className);
    258     }
    259     tmp = tmp->next;
     252  list<ClassList>::iterator cl;
     253  for (cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++)
     254    if (object->isA((*cl).classID))
     255  {
     256    PRINT(0)("=%s=-", (*cl).className);
    260257  }
    261258}
     
    266263 * @return a String containing the name of the Class, NULL if the Class was not found
    267264 */
    268 const char* ClassList::IDToString(long classID)
    269 {
    270   if(likely(ClassList::first != NULL))
    271   {
    272     ClassList* tmpCL = ClassList::first;
    273     while (likely(tmpCL != NULL))
    274     {
    275       if (tmpCL->classID == classID)
    276         return tmpCL->className;
    277       tmpCL = tmpCL->next;
    278     }
    279   }
    280   return NULL;
     265const char* ClassList::IDToString(ClassID classID)
     266{
     267  ClassList* cl = ClassList::getClassList(classID);
     268  return (cl != NULL) ? cl->className : NULL;
    281269}
    282270
     
    288276long ClassList::StringToID(const char* className)
    289277{
    290   if (className == NULL)
    291     return CL_NULL;
    292   if(likely(ClassList::first != NULL))
    293   {
    294     ClassList* tmpCL = ClassList::first;
    295     while (likely(tmpCL != NULL))
    296     {
    297       if (!strcasecmp(tmpCL->className, className))
    298         return tmpCL->classID;
    299       tmpCL = tmpCL->next;
    300     }
    301   }
    302   return CL_NULL;
     278  ClassList* cl = ClassList::getClassList(className);
     279  return (cl != NULL) ? cl->classID : CL_NULL;
     280}
     281
     282/**
     283 * checks if this ClassList is named className
     284 * @param className the Name to check this ClassList's ClassName against
     285 * @returns true on match, false otherwise
     286 */
     287bool ClassList::operator==(const char* className)
     288{
     289  if (likely( className != NULL && this->className != NULL))
     290    return (!strcmp(this->className, className));
     291  else
     292    return false;
    303293}
    304294
     
    317307  PRINT(0)("=  CLASS_LIST (level %d)  =\n", debugLevel);
    318308  PRINT(0)("==========================\n");
    319   PRINT(0)("| knows %d Classes\n|\n", ClassList::classCount);
    320   ClassList* tmp = ClassList::first;
     309  PRINT(0)("| knows %d Classes\n|\n", ClassList::classList->size());
    321310  char niceString[100];
    322311  unsigned int lenCount = 0;
    323312
    324   while (likely(tmp != NULL))
    325   {
    326     if ((debugLevel >= 1 || tmp->objectList.size() > 0 ) &&
    327          (classID == CL_NULL || unlikely (classID == tmp->classID)))
     313  list<ClassList>::iterator cl;
     314  for (cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++)
     315  {
     316    if ((debugLevel >= 1 || (*cl).objectList.size() > 0 ) &&
     317         (classID == CL_NULL || unlikely (classID == (*cl).classID)))
    328318    {
    329319      lenCount = 1;
    330       while (pow(10, lenCount) <= tmp->objectList.size())
     320      while (pow(10, lenCount) <= (*cl).objectList.size())
    331321        ++lenCount;
    332       for (int i=0; i < 30-strlen(tmp->className) - lenCount; i++)
     322      for (int i=0; i < 30-strlen((*cl).className) - lenCount; i++)
    333323        (niceString[i]) = ' ';
    334       niceString[30-strlen(tmp->className) - lenCount] = '\0';
    335 
    336       PRINT(0)("| CLASS %s:%s %d\n", tmp->className, niceString, tmp->objectList.size());
    337 
    338       if (debugLevel >=2 && tmp->objectList.size() > 0)
     324      niceString[30-strlen((*cl).className) - lenCount] = '\0';
     325
     326      PRINT(0)("| CLASS %s:%s %d\n", (*cl).className, niceString, (*cl).objectList.size());
     327
     328      if (debugLevel >=2 && (*cl).objectList.size() > 0)
    339329      {
    340330        PRINT(0)("|  Listing Instances:\n");
    341331        list<BaseObject*>::const_iterator bo;
    342         for (bo = tmp->objectList.begin(); bo != tmp->objectList.end(); bo++)
     332        for (bo = (*cl).objectList.begin(); bo != (*cl).objectList.end(); bo++)
    343333        {
    344334          PRINT(0)("|   (class %s): NAME(%s)->%p ", (*bo)->getClassName(), (*bo)->getName(), (*bo));
     
    349339      }
    350340    }
    351     tmp = tmp->next;
    352341  }
    353342  PRINT(0)("=======================CL=\n");
  • trunk/src/lib/lang/class_list.h

    r5779 r5791  
    1010#include "class_id.h"
    1111#include <list>
     12#ifndef NULL
     13#define NULL     0    //!< NULL
     14#endif
    1215
    1316// FORWARD DECLARATION
     
    3437
    3538    // STATIC FUNCTIONS
    36     static void                          addToClassList(BaseObject* objectPointer, const long& classID, const char* className);
    37     static void                          removeFromClassList(BaseObject* objectPointer);
     39    static void                           addToClassList(BaseObject* objectPointer, ClassID classID, const char* className);
     40    static void                           removeFromClassList(BaseObject* objectPointer);
    3841
    39     static std::list<BaseObject*>*       getList(long classID = CL_NULL);
    40     static std::list<BaseObject*>*       getList(const char* className);
    41     static const std::list<const char*>* getClassList();
    42     static BaseObject*                   getObject(const char* name, long classID = CL_NULL);
    43     static bool                          exists(const BaseObject* object, long classID = CL_NULL);
     42    static std::list<BaseObject*>*        getList(ClassID classID = CL_NULL);// { return (ClassList* fl = ClassList::getClassList(classID) != NULL)? &(fl->objectList) : NULL; };
     43    static std::list<BaseObject*>*        getList(const char* className); // { return (ClassList* fl = ClassList::getClassList(className) != NULL)? &(fl->objectList) : NULL;  };
     44    static const std::list<const char*>*  getClassNames();
     45    static BaseObject*                    getObject(const char* name, ClassID classID = CL_NULL);
     46    static bool                           exists(const BaseObject* object, ClassID classID = CL_NULL);
    4447
    45     static void                          whatIs(const BaseObject* object);
     48    static void                           whatIs(const BaseObject* object);
    4649
    47     static const char*                   IDToString(long classID = CL_NULL);
    48     static long                          StringToID(const char* className);
    49     static void                          debug(unsigned int debugLevel = 0, long classID = CL_NULL);
    50     static void                          debugS(const char* className = 0x0, unsigned int debugLevel = 0);
     50    static const char*                    IDToString(ClassID classID = CL_NULL);
     51    static long                           StringToID(const char* className);
     52    static void                           debug(unsigned int debugLevel = 0, long classID = CL_NULL);
     53    static void                           debugS(const char* className = 0x0, unsigned int debugLevel = 0);
    5154
    52 
     55    bool                                  operator==(ClassID classID) { return (this->classID == classID); };
     56    bool                                  operator==(const char* className);
    5357
    5458  private:
    55     std::list<BaseObject*>          objectList;             //!< A list of Objects belonging to this Class
     59    static ClassList*                     getClassList(ClassID classID);
     60    static ClassList*                     getClassList(const char* className);
     61
     62  private:
    5663
    5764    long                            classID;                //!< ClassID stored in this ClassList \see ClassID
    5865    const char*                     className;              //!< Name of the Class Stored here
    5966
    60     ClassList*                      next;                   //!< Pointer to the next class in the List
     67    std::list<BaseObject*>          objectList;             //!< A list of Objects belonging to this Class
    6168
    6269    // STATIC MEMBERS
    63     static ClassList*               first;                  //!< The first Class in the List
    64     static std::list<const char*>   classList;              //!< a List of all Names of all classes, that have registered so far.
    65     static unsigned int             classCount;             //!< The Count of classes that have been registered (should match the lower description)
     70    static std::list<ClassList>*     classList;              //!< The first Class in the List
     71    static std::list<const char*>   classNames;             //!< a List of all Names of all classes, that have registered so far.
    6672};
    6773
  • trunk/src/lib/shell/shell_command.cc

    r5779 r5791  
    202202          classID = ClassList::StringToID((*commandClassIT)->getName());
    203203          commandClass = (*commandClassIT);
    204           objectList = ClassList::getList(classID);
     204          objectList = ClassList::getList((ClassID)classID);
    205205          break;
    206206        }
  • trunk/src/lib/shell/shell_completion.cc

    r5783 r5791  
    112112  {
    113113    classID = ClassList::StringToID(inputSplits.getString(0));
    114     objectList = ClassList::getList(classID);
     114    objectList = ClassList::getList((ClassID)classID);
    115115    if (classID != CL_NULL)
    116116      completeType |= SHELLC_OBJECT;
     
    151151  if (unlikely(classBegin == NULL))
    152152    return false;
    153   const std::list<const char*>* clList = ClassList::getClassList();
     153  const std::list<const char*>* clList = ClassList::getClassNames();
    154154  if (clList != NULL)
    155155  {
     
    172172  if (unlikely(objectBegin == NULL))
    173173    return false;
    174   const std::list<BaseObject*>* boList = ClassList::getList(classID);
     174  const std::list<BaseObject*>* boList = ClassList::getList((ClassID)classID);
    175175  if (boList != NULL)
    176176  {
Note: See TracChangeset for help on using the changeset viewer.