aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/objects/identifiable.cpp
blob: 067600e00bb5467d9fe9d44fd2643d9d42036258 (plain) (blame)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "identifiable.hpp"
#include <cassert>
#include <vespa/vespalib/util/stringfmt.h>
#include <stdexcept>
#include <algorithm>
#include <vespa/vespalib/objects/nbostream.h>
#include "objectdumper.h"
#include "visit.h"
#include "objectpredicate.h"
#include "objectoperation.h"
#include <vespa/vespalib/util/classname.h>
#include <vespa/vespalib/stllike/hash_set.hpp>


namespace vespalib {

namespace {

class Register {
public:
    using RuntimeClass = Identifiable::RuntimeClass;
    Register();
    ~Register();
    bool append(RuntimeClass * c);
    bool erase(RuntimeClass * c);
    const RuntimeClass * classFromId(unsigned id) const;
    const RuntimeClass * classFromName(const char * name) const;
    bool empty() const { return _listById.empty(); }
private:
    struct HashId  {
        uint32_t operator() (const RuntimeClass * f) const { return f->id(); }
        uint32_t operator() (uint32_t id) const { return id; }
    };
    struct EqualId {
        bool operator() (const RuntimeClass * a, const RuntimeClass * b) const { return a->id() == b->id(); }
        bool operator() (const RuntimeClass * a, uint32_t b) const { return a->id() == b; }
        bool operator() (uint32_t a, const RuntimeClass * b) const { return a == b->id(); }
    };
    struct HashName  {
        uint32_t operator() (const RuntimeClass * f) const { return hashValue(f->name()); }
        uint32_t operator() (const char * name) const { return hashValue(name); }
    };
    struct EqualName {
        bool operator() (const RuntimeClass * a, const RuntimeClass * b) const { return strcmp(a->name(), b->name()) == 0; }
        bool operator() (const RuntimeClass * a, const char * b) const { return strcmp(a->name(), b) == 0; }
        bool operator() (const char * a, const RuntimeClass * b) const { return strcmp(a, b->name()) == 0; }
    };
    using IdList =  hash_set<RuntimeClass *, HashId, EqualId>;
    using NameList = hash_set<RuntimeClass *, HashName, EqualName>;
    IdList   _listById;
    NameList _listByName;
};

Register::Register()
    : _listById(),
      _listByName()
{ }

Register::~Register() = default;

bool
Register::erase(Identifiable::RuntimeClass * c)
{
    _listById.erase(c);
    _listByName.erase(c);
    return true;
}

bool
Register::append(Identifiable::RuntimeClass * c)
{
    bool ok((_listById.find(c) == _listById.end()) && ((_listByName.find(c) == _listByName.end())));
    if (ok) {
        _listById.insert(c);
        _listByName.insert(c);
    }
    return ok;
}

const Identifiable::RuntimeClass *
Register::classFromId(unsigned id) const
{
    IdList::const_iterator it(_listById.find<uint32_t>(id));
    return  (it != _listById.end()) ? *it : nullptr;
}

const Identifiable::RuntimeClass *
Register::classFromName(const char *name) const
{
    NameList::const_iterator it(_listByName.find<const char *>(name));
    return  (it != _listByName.end()) ? *it : nullptr;
}

Register * _register = nullptr;

}

Identifiable::ILoader  * Identifiable::_classLoader = nullptr;

IMPLEMENT_IDENTIFIABLE(Identifiable, Identifiable);

const Identifiable::RuntimeClass *
Identifiable::classFromId(unsigned id) {
    return _register->classFromId(id);
}

const Identifiable::RuntimeClass *
Identifiable::classFromName(const char * name) {
    return _register->classFromName(name);
}

Identifiable::RuntimeClass::RuntimeClass(RuntimeInfo * info_)
    : _rt(info_)
{
    if (_rt->_factory) {
        Identifiable::UP tmp(create());
        Identifiable &tmpref = *tmp;
        assert(id() == tmp->getClass().id());
        //printf("Class %s has typeinfo %s\n", name(), typeid(*tmp).name());
        for (const RuntimeInfo * curr = _rt; curr && curr != curr->_base; curr = curr->_base) {
            //printf("\tinherits %s : typeinfo = %s\n", curr->_name, curr->_typeId().name());
            if ( ! curr->_tryCast(tmp.get()) ) {
                throw std::runtime_error(make_string("(%s, %s) is not a baseclass of (%s, %s)", curr->_name, curr->_typeId().name(), name(), typeid(tmpref).name()));
            }
        }
    }
    if (_register == nullptr) {
        _register = new Register();
    }
    if (! _register->append(this)) {
        const RuntimeClass * old = _register->classFromId(id());
        throw std::runtime_error(make_string("Duplicate Identifiable object(%s, %s, %d) being registered. Choose a unique id. Object (%s, %s, %d) is using it.",
                                             name(), info(), id(), old->name(), old->info(), old->id()));
    }
}

Identifiable::RuntimeClass::~RuntimeClass()
{
    if ( ! _register->erase(this) ) {
        assert(0);
    }
    if (_register->empty()) {
        delete _register;
        _register = nullptr;
    }
}

bool
Identifiable::RuntimeClass::inherits(unsigned cid) const
{
    const RuntimeInfo *cur;
    for (cur = _rt; (cur != &Identifiable::_RTInfo) && cid != cur->_id; cur = cur->_base) { }
    return (cid == cur->_id);
}

Serializer &
operator << (Serializer & os, const Identifiable & obj)
{
    os.put(obj.getClass().id());
    obj.serialize(os);
    return os;
}

nbostream &
operator << (nbostream & os, const Identifiable & obj)
{
    NBOSerializer nos(os);
    nos << obj;
    return os;
}

nbostream &
operator >> (nbostream & is, Identifiable & obj)
{
    NBOSerializer nis(is);
    nis >> obj;
    return is;
}

Deserializer &
operator >> (Deserializer & os, Identifiable & obj)
{
    uint32_t cid(0);
    os.get(cid);
    if (cid == obj.getClass().id()) {
        obj.deserialize(os);
    } else {
        throw std::runtime_error(make_string("Failed deserializing %s : Received cid %d(%0x) != %d(%0x)",
                                             obj.getClass().name(), cid, cid, obj.getClass().id(), obj.getClass().id()));
        //Should mark as failed
    }
    return os;
}

Identifiable::UP
Identifiable::create(Deserializer & is)
{
    uint32_t cid(0);
    is.get(cid);
    UP obj;
    const Identifiable::RuntimeClass *rtc = Identifiable::classFromId(cid);
    if (rtc == nullptr) {
        if ((_classLoader != nullptr) && _classLoader->hasClass(cid)) {
            _classLoader->loadClass(cid);
            rtc = Identifiable::classFromId(cid);
            if (rtc == nullptr) {
                throw std::runtime_error(make_string("Failed loading class for Identifiable with classId %d(%0x)", cid, cid));
            }
        }
    }
    if (rtc != nullptr) {
        obj.reset(rtc->create());
        if (obj.get()) {
            obj->deserialize(is);
        } else {
            throw std::runtime_error(
                    make_string("Failed deserializing an Identifiable for classId %d(%0x). "
                                "It is abstract, so it can not be instantiated. Does it need to be abstract ?",
                                cid, cid));
        }
    } else {
        throw std::runtime_error(make_string("Failed deserializing an Identifiable with unknown classId %d(%0x)", cid, cid));
    }
    return obj;
}

string
Identifiable::getNativeClassName() const
{
    return vespalib::getClassName(*this);
}

string
Identifiable::asString() const
{
    ObjectDumper dumper;
    visit(dumper, "", this);
    return dumper.toString();
}

int
Identifiable::onCmp(const Identifiable& b) const
{
    int diff(0);
    nbostream as, bs;
    NBOSerializer nas(as), nbs(bs);
    nas << *this;
    nbs << b;
    size_t minLength(std::min(as.size(), bs.size()));
    if (minLength > 0) {
        diff = memcmp(as.data(), bs.data(), minLength);
    }
    if (diff == 0) {
        diff = as.size() - bs.size();
    }
    return diff;
}

void
Identifiable::visitMembers(ObjectVisitor &visitor) const
{
    visitor.visitNotImplemented();
}

void
Identifiable::select(const ObjectPredicate &predicate, ObjectOperation &operation)
{
    if (predicate.check(*this)) {
        operation.execute(*this);
    } else {
        selectMembers(predicate, operation);
    }
}

void
Identifiable::selectMembers(const ObjectPredicate &predicate, ObjectOperation &operation)
{
    (void) predicate;
    (void) operation;
}

Serializer &
Identifiable::serialize(Serializer & os) const
{
    return os.put(*this);
}

Deserializer &
Identifiable::deserialize(Deserializer & is)
{
    return is.get(*this);
}

Serializer &
Identifiable::onSerialize(Serializer & os) const
{
    return os;
}

Deserializer &
Identifiable::onDeserialize(Deserializer & is)
{
    return is;
}

}