File indexing completed on 2023-10-01 03:58:58
0001 /* 0002 SPDX-FileCopyrightText: 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "object.h" 0008 #include <cstring> 0009 #include <QCoreApplication> 0010 0011 namespace StepCore { 0012 0013 STEPCORE_META_OBJECT(Object, QT_TRANSLATE_NOOP("ObjectClass", "Object"), QT_TRANSLATE_NOOP("ObjectDescription", "Object"), MetaObject::ABSTRACT,, 0014 STEPCORE_PROPERTY_RW(QString, name, QT_TRANSLATE_NOOP("PropertyName", "name"), STEPCORE_UNITS_NULL, QT_TRANSLATE_NOOP("PropertyDescription", "Object name"), name, setName)) 0015 0016 int MetaObject::s_classIdCount = 0; 0017 0018 void MetaObject::copyProperties(const MetaProperty** dest) const 0019 { 0020 int c = 0; 0021 for(int i=0; i<superClassCount(); ++i) { 0022 superClass(i)->copyProperties(dest + c); 0023 c += superClass(i)->propertyCount(); 0024 } 0025 for(int i=0; i<_classPropertyCount; ++i) { 0026 dest[c+i] = _classProperties + i; 0027 } 0028 } 0029 0030 void MetaObject::init() const 0031 { 0032 if(_initialized) return; 0033 0034 // properties 0035 _allPropertyCount = classPropertyCount(); 0036 for(int i=0; i<superClassCount(); ++i) { 0037 _allPropertyCount += superClass(i)->propertyCount(); 0038 } 0039 0040 _allProperties = new const MetaProperty*[_allPropertyCount]; 0041 copyProperties(_allProperties); 0042 0043 // classId and super classes 0044 _classId = s_classIdCount++; // all super classes is already registered 0045 _allSuperClassIds.fill(false, s_classIdCount); 0046 _allSuperClassIds.setBit(_classId); 0047 for(int i=0; i<superClassCount(); ++i) { 0048 _allSuperClassIds |= superClass(i)->_allSuperClassIds; 0049 } 0050 0051 // strings 0052 _classNameTr = QCoreApplication::translate("ObjectClass", _className.toUtf8().constData()); 0053 _descriptionTr = QCoreApplication::translate("ObjectDescription", _description.toUtf8().constData()); 0054 0055 _initialized = true; 0056 } 0057 0058 bool MetaObject::inherits(const MetaObject* obj) const 0059 { 0060 if(!_initialized) init(); 0061 int objClassId = obj->classId(); 0062 if(objClassId == _classId) return true; 0063 else if(objClassId > _classId) return false; 0064 return _allSuperClassIds.testBit(objClassId); 0065 } 0066 0067 bool MetaObject::inherits(const char* name) const 0068 { 0069 //if(std::strcmp(className(), name) == 0) return true; 0070 if(className() == name) return true; 0071 for(int i=superClassCount()-1; i>=0; --i) { 0072 if(superClass(i)->inherits(name)) return true; 0073 } 0074 return false; 0075 } 0076 0077 /* 0078 int MetaObject::propertyCount() const 0079 { 0080 if(_allPropertyCount >= 0) return _allPropertyCount; 0081 0082 _allPropertyCount = 0; 0083 for(int i=0; i<superClassCount(); ++i) 0084 _allPropertyCount += superClass(i)->propertyCount(); 0085 _allPropertyCount += classPropertyCount(); 0086 return _allPropertyCount; 0087 }*/ 0088 0089 /* 0090 const MetaProperty* MetaObject::property(int n) const 0091 { 0092 for(int i=0; i<superClassCount(); ++i) { 0093 const MetaProperty* pr = superClass(i)->property(n); 0094 if(pr) return pr; 0095 n -= superClass(i)->propertyCount(); 0096 } 0097 if(n < classPropertyCount()) return &_classProperties[n]; 0098 else return NULL; 0099 }*/ 0100 0101 void MetaProperty::init() const 0102 { 0103 _nameTr = QCoreApplication::translate("PropertyName", _name.toUtf8().constData()); 0104 _unitsTr = QCoreApplication::translate("Units", _units.toUtf8().constData()); 0105 _descriptionTr = QCoreApplication::translate("PropertyDescription", _description.toUtf8().constData()); 0106 0107 _initialized = true; 0108 } 0109 0110 const MetaProperty* MetaObject::property(const QString& name) const 0111 { 0112 if(!_initialized) init(); 0113 for(int i=0; i<_allPropertyCount; ++i) { 0114 //if(std::strcmp(_allProperties[i]->name(), name) == 0) 0115 if(_allProperties[i]->name() == name) 0116 return _allProperties[i]; 0117 } 0118 /* 0119 for(int i=0; i<classPropertyCount(); ++i) { 0120 if(std::strcmp(classProperty(i)->name(), name) == 0) 0121 return classProperty(i); 0122 }*/ 0123 /* 0124 for(int i=superClassCount()-1; i>=0; --i) { 0125 const MetaProperty* pr = superClass(i)->property(name); 0126 if(pr) return pr; 0127 }*/ 0128 return nullptr; 0129 } 0130 0131 } // namespace StepCore 0132