summaryrefslogtreecommitdiffstats
path: root/source/core/slang-castable-list-impl.cpp
blob: 1cc5dc2d4780ffab5f40c21e1cd3ed2a50d0cbbc (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// slang-castable-list-impl.cpp
#include "slang-castable-list-impl.h"

#include "slang-castable-util.h"

namespace Slang {

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! CastableList !!!!!!!!!!!!!!!!!!!!!!!!!!! */

CastableList::~CastableList()
{
    for (auto castable : m_list)
    {
        castable->release();
    }
}

void* CastableList::castAs(const Guid& guid)
{
    if (auto intf = getInterface(guid))
    {
        return intf;
    }
    return getObject(guid);
}

void* CastableList::getInterface(const Guid& guid)
{
    if (guid == ISlangUnknown::getTypeGuid() ||
        guid == ICastable::getTypeGuid() ||
        guid == ICastableList::getTypeGuid())
    {
        return static_cast<ICastableList*>(this);
    }
    return nullptr;
}

void* CastableList::getObject(const Guid& guid)
{
    SLANG_UNUSED(guid);
    return nullptr;
}

void* CastableList::find(const Guid& guid)
{
    for (ICastable* castable : m_list)
    {
        if (auto ptr = castable->castAs(guid))
        {
            return ptr;
        }
    }
    return nullptr;
}

ICastable* SLANG_MCALL CastableList::findWithPredicate(FindFunc func, void* data)
{
    for (ICastable* castable : m_list)
    {
        if (func(castable, data))
        {
            return castable;
        }
    }
    return nullptr;
}

Index CastableList::indexOf(ICastable* castable)
{
    const Count count = m_list.getCount();
    for (Index i = 0; i < count; ++i)
    {
        ICastable* cur = m_list[i];
        if (cur == castable)
        {
            return i;
        }
    }
    return -1;
}

void CastableList::add(ICastable* castable) 
{ 
    SLANG_ASSERT(castable);
    castable->addRef();
    m_list.add(castable);
}

void CastableList::removeAt(Index i) 
{ 
    auto castable = m_list[i];
    m_list.removeAt(i);
    castable->release();
}

void CastableList::clear() 
{ 
    for (auto castable : m_list)
    {
        castable->release();
    }
    m_list.clear(); 
}

void CastableList::addUnknown(ISlangUnknown* unk)
{
    add(CastableUtil::getCastable(unk));
}

Index CastableList::indexOfUnknown(ISlangUnknown* unk)
{
    SLANG_ASSERT(unk);
    // If it has a castable interface we can just look for that
    {
        ComPtr<ICastable> castable;
        if (SLANG_SUCCEEDED(unk->queryInterface(ICastable::getTypeGuid(), (void**)castable.writeRef())) && castable)
        {
            return indexOf(castable);
        }
    }

    // It's not derived from ICastable, so can only be in list via an adapter
    const Count count = m_list.getCount();
    for (Index i = 0; i < count; ++i)
    {
        auto adapter = as<IUnknownCastableAdapter>(m_list[i]);
        if (adapter && adapter->getContained() == unk)
        {
            return i;
        }
    }

    return -1;
}

} // namespace Slang