Sylvain Rouquette avatar Sylvain Rouquette committed 503e8a1

fixed intrusive list

Comments (0)

Files changed (5)

game/inc/common/list.h

 namespace common {
 
 
-template <class T>
 class ListNode {
 public:
         ListNode();
         ~ListNode();
 
-        ListNode<T>* next() { return next_; }
-        ListNode<T>* prev() { return prev_; }
+        ListNode* next() { return next_; }
+        ListNode* prev() { return prev_; }
 
         bool is_linked() const { return next_ != prev_; }
         void unlink();
 
-        void insert_after(ListNode<T>* node, ListNode<T>* prev_node);
-        void insert_before(ListNode<T>* node, ListNode<T>* next_node);
+        void insert_after(ListNode* node);
+        void insert_before(ListNode* node);
 
 private:
     ListNode(const ListNode&);
-    ListNode<T>& operator= (const ListNode&);
+    ListNode& operator= (const ListNode&);
 
-    ListNode<T>*   prev_;
-    ListNode<T>*   next_;
+    ListNode*   prev_;
+    ListNode*   next_;
 };
 
 
+
+// -----------------------------------------------------------------------------
+
 template <class T>
 class List {
 public:
     ~List();
 
     T* first();
+    T* last();
     T* next(T* object);
-    ListNode<T>* get_node(T* object) const;
-    T* get_object(ListNode<T>* node) const;
+    ListNode* get_node(T* object) const;
+    T* get_object(ListNode* node) const;
 
     bool is_empty() const;
     void push_back(T* object);
     void push_front(T* object);
 
+    ListNode    root_;
+
 private:
     List(size_t offset) : offset_(offset) {}
 
-    ListNode<T>     root_;
-    size_t          offset_;
+    size_t      offset_;
 
     template<class, size_t> friend class ListDeclare;
 };
 
 
+template <class T>
+List<T>::~List() {
+    while (root_.is_linked())
+        root_.next()->unlink();
+}
+
+
+template <class T>
+T* List<T>::first() {
+    return root_.is_linked() ? get_object(root_.next()) : NULL;
+}
+
+
+template <class T>
+T* List<T>::last() {
+    return root_.is_linked() ? get_object(root_.prev()) : NULL;
+}
+
+
+template <class T>
+T* List<T>::next(T* object) {
+    ListNode* node = get_node(object);
+    if (node->next() == &root_)
+        return NULL;
+    else
+        return get_object(node->next());
+}
+
+
+template <class T>
+ListNode* List<T>::get_node(T* object) const {
+    return (ListNode*)((size_t)object + offset_);
+}
+
+
+template <class T>
+T* List<T>::get_object(ListNode* node) const {
+    return (T*)((size_t)node - offset_);
+}
+
+
+template <class T>
+void List<T>::push_back(T* object) {
+    get_node(object)->insert_before(&root_);
+}
+
+
+template <class T>
+void List<T>::push_front(T* object) {
+    get_node(object)->insert_after(&root_);
+}
+
+
+// -----------------------------------------------------------------------------
+
 
 template <class T, size_t offset>
 class ListDeclare : public List<T> {
 };
 
 
-// -----------------------------------------------------------------------------
-
-
-template <class T>
-ListNode<T>::ListNode() {
-    prev_ = this;
-    next_ = this;
-}
-
-
-template <class T>
-ListNode<T>::~ListNode()
-{
-    unlink();
-}
-
-
-template <class T>
-void ListNode<T>::unlink() {
-    if (!is_linked())
-        return;
-
-    prev_->next_ = next_;
-    next_->prev_ = prev_;
-    prev_ = next_ = this;
-}
-
-
-template <class T>
-void ListNode<T>::insert_after(ListNode<T>* node, ListNode<T>* prev_node) {
-    unlink();
-
-    prev_ = prev_node;
-    next_ = prev_node->next_;
-
-    prev_node->next_->prev_ = this;
-    prev_node->next_ = node;
-}
-
-
-template <class T>
-void ListNode<T>::insert_before(ListNode<T>* node, ListNode<T>* next_node) {
-    unlink();
-
-    prev_ = next_node->prev_;
-    next_ = prev_->next_;
-
-    next_node->prev_->next_ = node;
-    next_node->prev_ = this;
-}
-
-
-// -----------------------------------------------------------------------------
-
-
-template <class T>
-T* List<T>::first() {
-    return root_->is_linked() ? get_object(root_->next()) : NULL;
-}
-
-
-template <class T>
-List<T>::~List() {
-    while (root_.is_linked())
-        root_.next()->unlink();
-}
-
-
-template <class T>
-T* List<T>::next(T* object) {
-    ListNode<T>* node = get_node(object);
-    return node->next() == root_ ? NULL : node->next();
-}
-
-
-template <class T>
-ListNode<T>* List<T>::get_node(T* object) const {
-    return (ListNode<T>*)((size_t)object + offset_);
-}
-
-
-template <class T>
-T* List<T>::get_object(ListNode<T>* node) const {
-    return (T*)((size_t)node - offset_);
-}
-
-
-template <class T>
-void List<T>::push_back(T* object) {
-    ListNode<T>* node = get_node(object);
-    root_.insert_before(node, node->next());
-}
-
-
-template <class T>
-void List<T>::push_front(T* object) {
-    ListNode<T>* node = get_node(object);
-    root_.insert_after(node, node->prev());
-}
-
-
-// -----------------------------------------------------------------------------
-
-
 template <class T, size_t offset>
 ListDeclare<T, offset>::ListDeclare() : List<T>(offset)
 {}

game/src/common/list.cpp

+#include "common/list.h"
+
+namespace common {
+
+
+ListNode::ListNode() {
+    prev_ = this;
+    next_ = this;
+}
+
+
+ListNode::~ListNode()
+{
+    unlink();
+}
+
+
+void ListNode::unlink() {
+    if (!is_linked())
+        return;
+
+    prev_->next_ = next_;
+    next_->prev_ = prev_;
+    prev_ = next_ = this;
+}
+
+
+void ListNode::insert_after(ListNode* node) {
+    unlink();
+
+    prev_ = node;
+    next_ = node->next_;
+
+    prev_->next_ = this;
+    next_->prev_ = this;
+}
+
+
+void ListNode::insert_before(ListNode* node) {
+    unlink();
+
+    next_ = node;
+    prev_ = node->prev_;
+
+    next_->prev_ = this;
+    prev_->next_ = this;
+}
+
+
+} /* namespace common */

game/src/main/main_win32.cpp

 #include "game/game_loop_sample.h"
 #include "common/log.h"
 
-//#include "common/list.h"
-//class Test {
-//    public:
-//    common::ListNode<Test>  node_;
-//};
+#include "common/list.h"
+class Test {
+public:
+    Test(int value) : value_(value) {}
+
+    int value_;
+    common::ListNode    node_;
+};
 
 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
     LPSTR lpCmdLine, int nCmdShow)
 #endif
     LOG("starting game...\n");
 
-    //Test* test = new Test();
-    //common::List<Test>* list_test = new INTRUSIVE_LIST(Test, node_);
-    //list_test->push_back(test);
-    //delete test;
-    //delete list_test;
+    Test* test[10];
+    for (int i = 0; i < 10; ++i)
+        test[i] = new Test(i);
 
-    game::GameLoopSample game_loop;
-    game::GameWin32 *game;
-    game = new game::GameWin32(&game_loop);
+    common::List<Test>* list = new INTRUSIVE_LIST(Test, node_);
 
-    game->run();
+    for (int i = 0; i < 10; ++i)
+        list->push_back(test[i]);
 
-    delete game;
+    delete test[5]; test[5] = 0;
+    delete test[8]; test[8] = 0;
+
+    Test* current = list->first();
+    if (current)
+    do {
+        if (current)
+            LOG("%X : %d\n", current, current->value_);
+    } while (current = list->next(current));
+
+    for (int i = 0; i < 10; ++i)
+        if (test[i])
+            delete test[i];
+    delete list;
+
+    //game::GameLoopSample game_loop;
+    //game::GameWin32 *game;
+    //game = new game::GameWin32(&game_loop);
+    //
+    //game->run();
+    //
+    //delete game;
 
 #if defined(DEBUG)
     common::Log::clear();

game/src/platform/win32/application_win32.cpp

         }
     }
 
-    if (reinterpret_cast< PlatformWin32* >(get_platform())->register_input_devices())
-        return 1;
+    //if (reinterpret_cast< PlatformWin32* >(get_platform())->register_input_devices())
+    //    return 1;
 
     return window_->init(width, height, fullscreen);
 }
 
 
 void ApplicationWin32::update() {
-    reinterpret_cast< PlatformWin32* >(get_platform())->update_inputs();
+    //reinterpret_cast< PlatformWin32* >(get_platform())->update_inputs();
 
     window_->manage_events();
 

game/src/platform/win32/platform_input.cpp

     devices[0].dwFlags = RIDEV_NOLEGACY;
     devices[0].hwndTarget = 0;
 
-    devices[1].usUsagePage = 0x01;
-    devices[1].usUsage = 0x02; // mouse
-    devices[1].dwFlags = 0; //RIDEV_NOLEGACY;
-    devices[1].hwndTarget = 0;
-
-    devices[2].usUsagePage = 0x05;
-    devices[2].usUsage = 0x05; // pad
-    devices[2].dwFlags = 0;
-    devices[2].hwndTarget = 0;
+    //devices[1].usUsagePage = 0x01;
+    //devices[1].usUsage = 0x02; // mouse
+    //devices[1].dwFlags = 0; //RIDEV_NOLEGACY;
+    //devices[1].hwndTarget = 0;
+    //
+    //devices[2].usUsagePage = 0x05;
+    //devices[2].usUsage = 0x05; // pad
+    //devices[2].dwFlags = 0;
+    //devices[2].hwndTarget = 0;
 
     //TODO: move device registration in platform ?
-    if (::RegisterRawInputDevices(devices, 3, sizeof(::RAWINPUTDEVICE)) == FALSE) {
-        printf("Failed to register raw input devices\n");
+    if (::RegisterRawInputDevices(devices, 1, sizeof(::RAWINPUTDEVICE)) == FALSE) {
+        TCHAR   buffer[256];
+        ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,                 // It�s a system error
+                         NULL,                                      // No string to be formatted needed
+                         GetLastError(),                               // Hey Windows: Please explain this error!
+                         MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),  // Do it in the standard language
+                         buffer,              // Put the message here
+                         256,                     // Number of bytes to store the message
+                         NULL);
+        printf("Failed to register raw input devices %s\n", buffer);
         return 1;
     }
     return 0;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.