File: object.cc

package info (click to toggle)
step 4%3A25.08.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,628 kB
  • sloc: cpp: 16,808; xml: 762; python: 380; javascript: 93; sh: 39; makefile: 3
file content (132 lines) | stat: -rw-r--r-- 4,010 bytes parent folder | download | duplicates (3)
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
/*
    SPDX-FileCopyrightText: 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "object.h"
#include <cstring>
#include <QCoreApplication>

namespace StepCore {

STEPCORE_META_OBJECT(Object, QT_TRANSLATE_NOOP("ObjectClass", "Object"), QT_TRANSLATE_NOOP("ObjectDescription", "Object"), MetaObject::ABSTRACT,,
        STEPCORE_PROPERTY_RW(QString, name, QT_TRANSLATE_NOOP("PropertyName", "name"), STEPCORE_UNITS_NULL, QT_TRANSLATE_NOOP("PropertyDescription", "Object name"), name, setName))

int MetaObject::s_classIdCount = 0;

void MetaObject::copyProperties(const MetaProperty** dest) const
{
    int c = 0;
    for(int i=0; i<superClassCount(); ++i) {
        superClass(i)->copyProperties(dest + c);
        c += superClass(i)->propertyCount();
    }
    for(int i=0; i<_classPropertyCount; ++i) {
        dest[c+i] = _classProperties + i;
    }
}

void MetaObject::init() const
{
    if(_initialized) return;

    // properties
    _allPropertyCount = classPropertyCount();
    for(int i=0; i<superClassCount(); ++i) {
        _allPropertyCount += superClass(i)->propertyCount();
    }

    _allProperties = new const MetaProperty*[_allPropertyCount];
    copyProperties(_allProperties);

    // classId and super classes
    _classId = s_classIdCount++; // all super classes is already registered
    _allSuperClassIds.fill(false, s_classIdCount);
    _allSuperClassIds.setBit(_classId);
    for(int i=0; i<superClassCount(); ++i) {
        _allSuperClassIds |= superClass(i)->_allSuperClassIds;
    }

    // strings
    _classNameTr = QCoreApplication::translate("ObjectClass", _className.toUtf8().constData());
    _descriptionTr = QCoreApplication::translate("ObjectDescription", _description.toUtf8().constData());

    _initialized = true;
}

bool MetaObject::inherits(const MetaObject* obj) const
{
    if(!_initialized) init();
    int objClassId = obj->classId();
    if(objClassId == _classId) return true;
    else if(objClassId > _classId) return false;
    return _allSuperClassIds.testBit(objClassId);
}

bool MetaObject::inherits(const char* name) const
{
    //if(std::strcmp(className(), name) == 0) return true;
    if(className() == name) return true;
    for(int i=superClassCount()-1; i>=0; --i) {
        if(superClass(i)->inherits(name)) return true;
    }
    return false;
}

/*
int MetaObject::propertyCount() const
{
    if(_allPropertyCount >= 0) return _allPropertyCount;

    _allPropertyCount = 0;
    for(int i=0; i<superClassCount(); ++i)
        _allPropertyCount += superClass(i)->propertyCount();
    _allPropertyCount += classPropertyCount();
    return _allPropertyCount;
}*/

/*
const MetaProperty* MetaObject::property(int n) const
{
    for(int i=0; i<superClassCount(); ++i) {
        const MetaProperty* pr = superClass(i)->property(n);
        if(pr) return pr;
        n -= superClass(i)->propertyCount();
    }
    if(n < classPropertyCount()) return &_classProperties[n];
    else return NULL;
}*/

void MetaProperty::init() const
{
    _nameTr = QCoreApplication::translate("PropertyName", _name.toUtf8().constData());
    _unitsTr = QCoreApplication::translate("Units", _units.toUtf8().constData());
    _descriptionTr = QCoreApplication::translate("PropertyDescription", _description.toUtf8().constData());

    _initialized = true;
}

const MetaProperty* MetaObject::property(const QString& name) const
{
    if(!_initialized) init();
    for(int i=0; i<_allPropertyCount; ++i) {
        //if(std::strcmp(_allProperties[i]->name(), name) == 0)
        if(_allProperties[i]->name() == name)
            return _allProperties[i];
    }
    /*
    for(int i=0; i<classPropertyCount(); ++i) {
        if(std::strcmp(classProperty(i)->name(), name) == 0)
            return classProperty(i);
    }*/
    /*
    for(int i=superClassCount()-1; i>=0; --i) {
        const MetaProperty* pr = superClass(i)->property(name);
        if(pr) return pr;
    }*/
    return nullptr;
}

} // namespace StepCore