summaryrefslogtreecommitdiff
path: root/source/core/slang-lazy-castable-list.cpp
blob: 1832a67f42cf8e3f9bf9d455276a298aafda43fa (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// slang-lazy-castable-list.cpp
#include "slang-lazy-castable-list.h"

#include "slang-castable-list-impl.h"

namespace Slang {

void LazyCastableList::removeAt(Index index)
{
    SLANG_ASSERT(index >= 0 && index < getCount());

    switch (m_state)
    {
        case State::None:   break;
        case State::One:    
        {
            m_state = State::None;
            m_castable.setNull();
            break;
        }
        case State::List:   
        {
            static_cast<ICastableList*>(m_castable.get())->removeAt(index);
            break;
        }
    }
}

void LazyCastableList::clear()
{
    if (m_state == State::List)
    {
        auto list = static_cast<ICastableList*>(m_castable.get());
        list->clear();
    }
    else
    {
        m_state = State::None;
        m_castable.setNull();
    }
}

void LazyCastableList::clearAndDeallocate()
{
    m_state = State::None;
    m_castable.setNull();
}

Count LazyCastableList::getCount() const
{
    switch (m_state)
    {
        case State::None:   return 0;
        case State::One:    return 1;
        default:
        case State::List:   return static_cast<ICastableList*>(m_castable.get())->getCount();
    }
}

void LazyCastableList::add(ICastable* castable)
{
    SLANG_ASSERT(castable);
    if (m_state == State::None)
    {
        m_castable = castable;
        m_state = State::One;
    }
    else
    {
        requireList()->add(castable);
    }
}

ICastableList* LazyCastableList::requireList()
{
    switch (m_state)
    {
        case State::None:
        {
            m_castable = new CastableList;
            m_state = State::List;
            break;
        }
        case State::One:
        {
            // Turn into a list
            auto list = new CastableList;
            list->add(m_castable);
            m_castable = list;
            m_state = State::List;
            break;
        }
        default: break;
    }
    SLANG_ASSERT(m_state == State::List);
    return static_cast<ICastableList*>(m_castable.get());
}

ICastableList* LazyCastableList::getList()
{
    return (m_state == State::None) ? nullptr : requireList();
}

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

ICastable* LazyCastableList::findWithPredicate(ICastableList::FindFunc func, void* data)
{
    for (auto castable : getView())
    {
        if (func(castable, data))
        {
            return castable;
        }
    }
    return nullptr;
}

ConstArrayView<ICastable*> LazyCastableList::getView() const
{
    switch (m_state)
    {
        case State::None: return ConstArrayView<ICastable*>();
        case State::One:  return ConstArrayView<ICastable*>((ICastable*const*)&m_castable, 1);
        default:
        case State::List:
        {
            auto list = static_cast<ICastableList*>(m_castable.get());
            return ConstArrayView<ICastable*>(list->getBuffer(), list->getCount());
        }
    }
}

Index LazyCastableList::indexOf(ICastable* castable) const
{
    return getView().indexOf(castable);
}

Index LazyCastableList::indexOfUnknown(ISlangUnknown* unk) const
{
    // Try as a ICastable first
    {
        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 auto view = getView();

    const Count count = view.getCount();
    for (Index i = 0; i < count; ++i)
    {
        auto adapter = as<IUnknownCastableAdapter>(view[i]);
        if (adapter && adapter->getContained() == unk)
        {
            return i;
        }
    }

    return -1;
}

} // namespace Slang