summaryrefslogtreecommitdiffstats
path: root/source/core/slang-blob.cpp
blob: 19a4281bd2ee51dc48a7bc0fcb7128fa0987b1fc (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "slang-blob.h"

namespace Slang {

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! BlobBase !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

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

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

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

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! StringBlob !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

void StringBlob::_setUniqueRep(StringRepresentation* uniqueRep)
{
    SLANG_ASSERT(uniqueRep == nullptr || uniqueRep->isUniquelyReferenced());

    m_uniqueRep = uniqueRep;

    m_slice = uniqueRep ? 
        UnownedTerminatedStringSlice(uniqueRep->getData(), uniqueRep->getLength()) :
        UnownedTerminatedStringSlice();
}

/* static */StringRepresentation* StringBlob::_createUniqueCopy(StringRepresentation* rep)
{
    if (rep)
    {
        // If the length is 0, we can just init as empty
        auto length = rep->getLength();
        if (length == 0)
        {
            return nullptr;
        }
        else
        {
            const UnownedStringSlice slice(rep->getData(), length);
            return StringRepresentation::createWithReference(slice);
        }
    }
    return nullptr;
}

void StringBlob::_setWithCopy(StringRepresentation* rep)
{
    _setUniqueRep(_createUniqueCopy(rep));
}

void StringBlob::_setWithMove(StringRepresentation* rep)
{
    if (rep && !rep->isUniquelyReferenced())
    {
        _setUniqueRep(_createUniqueCopy(rep));
        // We need to release a ref as rep is passed in with the 'current' ref count 
        rep->releaseReference();
    }
    else
    {
        _setUniqueRep(rep);
    }
}

/* static */ComPtr<ISlangBlob> StringBlob::create(const UnownedStringSlice& slice)
{
    StringRepresentation* rep = nullptr;
    if (slice.getLength())
    {
        rep = StringRepresentation::createWithReference(slice);
    }

    auto blob = new StringBlob;
    
    // rep must be unique at this point
    blob->_setUniqueRep(rep);
    return ComPtr<ISlangBlob>(blob);
}

/* static */ComPtr<ISlangBlob> StringBlob::create(const String& in)
{
    auto blob = new StringBlob;
    blob->_setWithCopy(in.getStringRepresentation());
    return ComPtr<ISlangBlob>(blob);
}

/* static */ComPtr<ISlangBlob> StringBlob::moveCreate(String& in)
{
    auto blob = new StringBlob;
    blob->_setWithMove(in.detachStringRepresentation());
    return ComPtr<ISlangBlob>(blob);
}

/* static */ComPtr<ISlangBlob> StringBlob::moveCreate(String&& in)
{
    auto blob = new StringBlob;
    blob->_setWithMove(in.detachStringRepresentation());
    return ComPtr<ISlangBlob>(blob);
}

StringBlob::~StringBlob()
{
    if (m_uniqueRep)
    {
        delete m_uniqueRep;
    }
}
void* StringBlob::castAs(const SlangUUID& guid)
{
    if (auto intf = getInterface(guid))
    {
        return intf;
    }
    return getObject(guid);
}

void* StringBlob::getObject(const Guid& guid)
{
    // Can allow accessing the contained String
    if (guid == getTypeGuid())
    {
        return this;
    }
    // Can always be accessed as terminated char*
    if (guid == SlangTerminatedChars::getTypeGuid())
    {
        return const_cast<char*>(m_slice.begin());
    }
    return nullptr;
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! RawBlob !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

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

void* RawBlob::getObject(const Guid& guid)
{
    // If the data has 0 termination, we can return the pointer
    if (guid == SlangTerminatedChars::getTypeGuid() && m_data.isTerminated())
    {
        return (char*)m_data.getData(); 
    }
    return nullptr;
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ScopeBlob !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

void* ScopeBlob::castAs(const SlangUUID& guid)
{
    if (auto intf = getInterface(guid))
    {
        return intf;
    }
    if (auto obj = getObject(guid))
    {
        return obj;
    }

    // If the contained thing is castable, ask it 
    if (m_castable)
    {
        return m_castable->castAs(guid);
    }

    return nullptr;
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ListBlob !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

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

void* ListBlob::getObject(const Guid& guid)
{
    // If the data is terminated return the pointer
    if (guid == SlangTerminatedChars::getTypeGuid())
    {
        const auto count = m_data.getCount();
        if (m_data.getCapacity() > count)
        {
            auto buf = m_data.getBuffer();
            if (buf[count] == 0)
            {
                return (char*)buf;
            }
        }
    }
    return nullptr;
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! StaticBlob !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

SlangResult StaticBlob::queryInterface(SlangUUID const& guid, void** outObject) 
{
    if (auto intf = getInterface(guid))
    {
        *outObject = intf;
        return SLANG_OK;
    }
    return SLANG_E_NO_INTERFACE;
}

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

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

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

} // namespace Slang