| 213 | } |
| 214 | |
| 215 | Property* DynamicProperty::addDynamicProperty( |
| 216 | PropertyContainer& pc, |
| 217 | std::string_view type, |
| 218 | const char* cstrName, |
| 219 | const char* group, |
| 220 | const char* doc, |
| 221 | short attr, |
| 222 | bool ro, |
| 223 | bool hidden |
| 224 | ) |
| 225 | { |
| 226 | if (type.empty()) { |
| 227 | type = "<null>"; |
| 228 | } |
| 229 | |
| 230 | std::string name {(cstrName && cstrName[0] != '\0') ? cstrName : ""}; |
| 231 | |
| 232 | static ParameterGrp::handle hGrp = |
| 233 | GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Document"); |
| 234 | if (hGrp->GetBool("AutoNameDynamicProperty", false)) { |
| 235 | if (name.empty()) { |
| 236 | name = type; |
| 237 | } |
| 238 | std::string uniqueName = getUniquePropertyName(pc, name.c_str()); |
| 239 | if (uniqueName != name) { |
| 240 | FC_WARN( |
| 241 | pc.getFullName() << " rename dynamic property from '" << name << "' to '" |
| 242 | << uniqueName << "'" |
| 243 | ); |
| 244 | name = uniqueName; |
| 245 | } |
| 246 | } |
| 247 | else if (name.empty()) { |
| 248 | name = "<null>"; // setting a bad name to trigger exception |
| 249 | } |
| 250 | |
| 251 | auto prop = pc.getPropertyByName(name.c_str()); |
| 252 | if (prop && prop->getContainer() == &pc) { |
| 253 | FC_THROWM(Base::NameError, |
| 254 | "Property " << pc.getFullName() << '.' << name << " already exists"); |
| 255 | } |
| 256 | |
| 257 | if (Base::Tools::getIdentifier(name) != name) { |
| 258 | FC_THROWM(Base::NameError, "Invalid property name '" << name << "'"); |
| 259 | } |
| 260 | |
| 261 | Base::Type propType = |
| 262 | Base::Type::getTypeIfDerivedFrom(type, App::Property::getClassTypeId(), true); |
| 263 | if (propType.isBad()) { |
| 264 | FC_THROWM(Base::TypeError, |
| 265 | "Invalid type " << type << " for property " << pc.getFullName() << '.' << name); |
| 266 | } |
| 267 | |
| 268 | void* propInstance = propType.createInstance(); |
| 269 | if (!propInstance) { |
| 270 | FC_THROWM(Base::RuntimeError, |
| 271 | "Failed to create property " << pc.getFullName() << '.' << name << " of type " |
| 272 | << type); |
nothing calls this directly
no test coverage detected