aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/flext/source/flitem.cpp
blob: 8970e1c3a149cafdab42d8c918d03039de566801 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* 

flext - C++ layer for Max/MSP and pd (pure data) externals

Copyright (c) 2001-2004 Thomas Grill (xovo@gmx.net)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  

*/

/*! \file flitem.cpp
    \brief Processing of method and attribute lists.
*/
 
#include "flext.h"
#include <string.h>


flext_base::Item::~Item()
{
    if(nxt) delete nxt;
}

flext_base::ItemSet::ItemSet() {}

flext_base::ItemSet::~ItemSet()
{
    for(iterator it = begin(); it != end(); ++it)
        if(it.data()) delete it.data();
}

flext_base::ItemCont::ItemCont(): 
    memsize(0),size(0),members(0),cont(NULL)
{}

flext_base::ItemCont::~ItemCont()
{
    if(cont) {
        for(int i = 0; i < size; ++i) delete cont[i];
        delete[] cont;
    }
}

void flext_base::ItemCont::Resize(int nsz)
{
    if(nsz > memsize) {
        int nmemsz = nsz+10;  // increment maximum allocation size
        ItemSet **ncont = new ItemSet *[nmemsz]; // make new array
        if(cont) {
            memcpy(ncont,cont,size*sizeof(*cont)); // copy existing entries
            delete[] cont; 
        }
        cont = ncont;  // set current array
        memsize = nmemsz;  // set new allocation size
    }

    // make new items
    while(size < nsz) cont[size++] = new ItemSet;
}

void flext_base::ItemCont::Add(Item *item,const t_symbol *tag,int inlet)
{
    if(!Contained(inlet)) Resize(inlet+2);
    ItemSet &set = GetInlet(inlet);
    Item *&lst = set[tag];
    if(!lst) 
        lst = item;
    else
        for(;;)
            if(!lst->nxt) { lst->nxt = item; break; }
            else lst = lst->nxt;
    members++;
}

bool flext_base::ItemCont::Remove(Item *item,const t_symbol *tag,int inlet)
{
    if(Contained(inlet)) {
        ItemSet &set = GetInlet(inlet);
        ItemSet::iterator it = set.find(tag);
        if(it != set.end()) {
            for(Item *lit = it.data(),*prv = NULL; lit; prv = lit,lit = lit->nxt) {
                if(lit == item) {
                    if(prv) prv->nxt = lit->nxt;
                    else it.data() = lit->nxt;
                
                    lit->nxt = NULL; delete lit;
                    return true;
                }
            }
        }
    }
    return false;
}

flext_base::Item *flext_base::ItemCont::FindList(const t_symbol *tag,int inlet)
{
    if(Contained(inlet)) {
        ItemSet &ai = GetInlet(inlet);
        ItemSet::iterator as = ai.find(tag); 
        if(as != ai.end()) return as.data();
    }
    return NULL;
}

// --- class item lists (methods and attributes) ----------------

typedef DataMap<flext_base::t_classid,flext_base::ItemCont *> ClassMap;

static ClassMap classarr[2];

flext_base::ItemCont *flext_base::GetClassArr(t_classid c,int ix) 
{
    ClassMap &map = classarr[ix];
    ClassMap::iterator it = map.find(c);
    if(it == map.end()) {
        ItemCont *cont = new ItemCont;
        map[c] = cont;
        return cont;
    }
    else
        return it.data();
}