| 1 | /*! |
|---|
| 2 | * <File comment goes here!!> |
|---|
| 3 | * |
|---|
| 4 | * Copyright (c) 2005 by <your name/ organization here> |
|---|
| 5 | */ |
|---|
| 6 | #ifndef _OGREODEMAINTAINEDLIST_H_ |
|---|
| 7 | #define _OGREODEMAINTAINEDLIST_H_ |
|---|
| 8 | |
|---|
| 9 | #include "OgreOdePreReqs.h" |
|---|
| 10 | |
|---|
| 11 | /*! |
|---|
| 12 | * \brief |
|---|
| 13 | * Write brief comment for OgreOde here. |
|---|
| 14 | * |
|---|
| 15 | * Write detailed description for OgreOde here. |
|---|
| 16 | * |
|---|
| 17 | * \remarks |
|---|
| 18 | * Write remarks for OgreOde here. |
|---|
| 19 | * |
|---|
| 20 | * \see |
|---|
| 21 | * Separate items with the '|' character. |
|---|
| 22 | */ |
|---|
| 23 | namespace OgreOde |
|---|
| 24 | { |
|---|
| 25 | |
|---|
| 26 | /** Convenience class to make it easy to step through all MaintainedItem in a MaintainedList. |
|---|
| 27 | */ |
|---|
| 28 | template <class T> |
|---|
| 29 | class _OgreOdeExport MaintainedItemIterator |
|---|
| 30 | { |
|---|
| 31 | |
|---|
| 32 | public: |
|---|
| 33 | typedef std::deque<T*> MaintainedItemList; |
|---|
| 34 | typedef std::map<unsigned long,T*> MaintainedItemMap; |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | protected: |
|---|
| 38 | typename MaintainedItemList::iterator mPos; |
|---|
| 39 | typename MaintainedItemList::iterator mStart; |
|---|
| 40 | typename MaintainedItemList::iterator mEnd; |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | public: |
|---|
| 44 | /// constructor, only available for MaintainedList::getIterator |
|---|
| 45 | MaintainedItemIterator(typename MaintainedItemList::iterator start, typename MaintainedItemList::iterator end) |
|---|
| 46 | { |
|---|
| 47 | mStart = mPos = start; |
|---|
| 48 | mEnd = end; |
|---|
| 49 | } |
|---|
| 50 | // Returns true when at the end of the MaintainedItem list |
|---|
| 51 | inline bool end(void) const |
|---|
| 52 | { |
|---|
| 53 | return (mPos == mEnd); |
|---|
| 54 | } |
|---|
| 55 | /** Returns a pointer to the next MaintainedItem, and moves the iterator on by 1 element. */ |
|---|
| 56 | inline T* getNext(void) |
|---|
| 57 | { |
|---|
| 58 | return static_cast<T*>(*mPos++); |
|---|
| 59 | } |
|---|
| 60 | }; |
|---|
| 61 | |
|---|
| 62 | template <class T> |
|---|
| 63 | class _OgreOdeExport MaintainedList |
|---|
| 64 | { |
|---|
| 65 | public: |
|---|
| 66 | typedef std::deque<T*> MaintainedItemList; |
|---|
| 67 | typedef std::map<unsigned long,T*> MaintainedItemMap; |
|---|
| 68 | |
|---|
| 69 | public: |
|---|
| 70 | MaintainedList(){}; |
|---|
| 71 | ~MaintainedList(){}; |
|---|
| 72 | |
|---|
| 73 | void registerItem(T* ptr) |
|---|
| 74 | { |
|---|
| 75 | assert(ptr->getID()); |
|---|
| 76 | _map[ptr->getID()] = ptr; |
|---|
| 77 | _list.push_back (ptr); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | T* findItem(unsigned long id) |
|---|
| 81 | { |
|---|
| 82 | assert (_map.find(id) != _map.end()); |
|---|
| 83 | return _map[id]; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | void unregisterItem(unsigned long id) |
|---|
| 87 | { |
|---|
| 88 | assert (_map.find(id) != _map.end()); |
|---|
| 89 | T* m = _map[id]; |
|---|
| 90 | _map.erase(id); |
|---|
| 91 | |
|---|
| 92 | typename MaintainedItemList::iterator i = std::find(_list.begin (), |
|---|
| 93 | _list.end (), |
|---|
| 94 | m); |
|---|
| 95 | if(i != _list.end()) |
|---|
| 96 | { |
|---|
| 97 | assert(*i == m); |
|---|
| 98 | _list.erase(i); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | void setDebug(const bool debug) |
|---|
| 103 | { |
|---|
| 104 | typename MaintainedItemList::iterator i = _list.begin(); |
|---|
| 105 | typename MaintainedItemList::iterator end = _list.end(); |
|---|
| 106 | for(;i != end;++i) |
|---|
| 107 | { |
|---|
| 108 | (*i)->setDebug(debug); |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | void setDebugContact(const bool debug) |
|---|
| 113 | { |
|---|
| 114 | typename MaintainedItemList::iterator i = _list.begin(); |
|---|
| 115 | typename MaintainedItemList::iterator end = _list.end(); |
|---|
| 116 | for(;i != end;++i) |
|---|
| 117 | { |
|---|
| 118 | (*i)->setDebugContact(debug); |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | void updateDebugContact() |
|---|
| 123 | { |
|---|
| 124 | std::for_each(_list.begin (), |
|---|
| 125 | _list.end (), |
|---|
| 126 | std::mem_fun(&T::updateDebugContact)); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | void synchronise() |
|---|
| 132 | { |
|---|
| 133 | std::for_each(_list.begin (), |
|---|
| 134 | _list.end (), |
|---|
| 135 | std::mem_fun(&T::synchronise)); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | void updateDrawState() |
|---|
| 139 | { |
|---|
| 140 | std::for_each(_list.begin (), |
|---|
| 141 | _list.end (), |
|---|
| 142 | std::mem_fun(&T::updateDrawState)); |
|---|
| 143 | } |
|---|
| 144 | void updatePreviousState() |
|---|
| 145 | { |
|---|
| 146 | std::for_each(_list.begin (), |
|---|
| 147 | _list.end (), |
|---|
| 148 | std::mem_fun(&T::updatePreviousState)); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | void updateCurrentState() |
|---|
| 152 | { |
|---|
| 153 | std::for_each(_list.begin (), |
|---|
| 154 | _list.end (), |
|---|
| 155 | std::mem_fun(&T::updateCurrentState)); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | void interpolateDrawState(const Ogre::Real alpha) |
|---|
| 159 | { |
|---|
| 160 | typename MaintainedItemList::iterator i = _list.begin(); |
|---|
| 161 | typename MaintainedItemList::iterator end = _list.end(); |
|---|
| 162 | for(;i != end;++i) |
|---|
| 163 | { |
|---|
| 164 | (*i)->interpolateDrawState(alpha); |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | void notify(Body* body) |
|---|
| 169 | { |
|---|
| 170 | typename MaintainedItemList::iterator i = _list.begin(); |
|---|
| 171 | typename MaintainedItemList::iterator end = _list.end(); |
|---|
| 172 | for(;i != end;++i) |
|---|
| 173 | { |
|---|
| 174 | (*i)->notify(body); |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | MaintainedItemIterator<T> getIterator(void) |
|---|
| 179 | { |
|---|
| 180 | return MaintainedItemIterator<T>(_list.begin(), _list.end()); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | protected: |
|---|
| 184 | |
|---|
| 185 | MaintainedItemMap _map; |
|---|
| 186 | MaintainedItemList _list; |
|---|
| 187 | }; |
|---|
| 188 | |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | #endif |
|---|