source: cpp/frams/util/callbacks.cpp @ 980

Last change on this file since 980 was 793, checked in by Maciej Komosinski, 7 years ago

Code formatting

  • Property svn:eol-style set to native
File size: 2.1 KB
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
[109]4
5#include "callbacks.h"
6#include <stdio.h>
7
8#ifdef USEMEMBERCALLBACK
9int MemberCallbackNode::equals(CallbackNode*n)
10{
[793]11        if (n == this) return 1;
12        MemberCallbackNode *classok = dynamic_cast<MemberCallbackNode*>(n);
13        if (!classok) return 0;
14        return ((userdata == classok->userdata) && (object == classok->object) && (member == classok->member));
[109]15}
16#endif
17
18int FunctionCallbackNode::equals(CallbackNode*n)
19{
[793]20        if (n == this) return 1;
21        FunctionCallbackNode *classok = dynamic_cast<FunctionCallbackNode*>(n);
22        if (!classok) return 0;
23        return ((userdata == classok->userdata) && (fun == classok->fun));
[109]24}
25
26int StatrickCallbackNode::equals(CallbackNode*n)
27{
[793]28        if (n == this) return 1;
29        StatrickCallbackNode *classok = dynamic_cast<StatrickCallbackNode*>(n);
30        if (!classok) return 0;
31        return ((object == classok->object) && (userdata == classok->userdata) && (fun == classok->fun));
[109]32}
33
34/////////////////
35
36CallbackNode* Callback::add(CallbackNode*n)
37{
[793]38        SList::operator+=(n);
39        return n;
[109]40}
41
42void Callback::removeNode(CallbackNode*n)
43{
[793]44        SList::operator-=(n);
45        delete n;
[109]46}
47
48void Callback::remove(CallbackNode*node)
49{
[793]50        CallbackNode *n;
51        //printf("Hint: removing callbacks (former 'DuoList') is more efficient using removeNode(). (refer to 'callbacks.h')\n");
52        for (int i = 0; n = (CallbackNode *)operator()(i); i++)
53                if (node->equals(n))
[109]54                {
55                SList::operator-=(i);
56                delete node;
[793]57                if (n != node) delete n;
[109]58                return;
59                }
[793]60        delete node; // tu nie wiem czy na pewno...
[109]61}
62
[247]63void Callback::action(intptr_t data)
[109]64{
[793]65        if (size() == 0) return;
66        SList copy = *this;
67        FOREACH(CallbackNode*, n, copy)
68                n->action(data);
[109]69}
70
71Callback::~Callback()
72{
[793]73        CallbackNode *n;
74        for (int i = size() - 1; i >= 0; i--)
[109]75        {
[793]76                n = (CallbackNode *)operator()(i);
77                delete n;
78                // todo: zrobic zeby kolejnosc delete callbacknode <-> delete callback nie wplywala na poprawne dzialania
79                // blad odkryty 24.01 pokazal, ze deletowanie callbacknodow w ~callback
80                // moze powodowac problemy, jezeli obiekty sa usuwane w "zlej" kolejnosci
81                // ale na razie tak zostanie
[109]82        }
83}
84
Note: See TracBrowser for help on using the repository browser.