// // MetaTable::removeString // // Removes the given field if it exists as a metastring_t. // Only one object will be removed. If more than one such object // exists, you would need to call this routine until metaerrno is // set to META_ERR_NOSUCHOBJECT. // // When calling this routine, the value of the object is returned // in case it is needed, and you will need to then free it yourself // using free(). If the return value is not needed, call // MetaTable::removeStringNR instead and the string value will be destroyed. // char *MetaTable::removeString(const char *key) { MetaObject *obj; MetaString *str; char *value; metaerrno = META_ERR_NOERR; if(!(obj = getObjectKeyAndType(key, RTTI(MetaString)))) { metaerrno = META_ERR_NOSUCHOBJECT; return NULL; } removeObject(obj); // Destroying the MetaString will destroy the value inside it too, unless we // get and then nullify its value manually. This is one reason why MetaTable // is a friend to these basic types, as it makes some simple management // chores like this more efficient. Otherwise I'd have to estrdup the string // and that's stupid. str = static_cast<MetaString *>(obj); value = str->value; str->value = NULL; // destructor does nothing if this is cleared first delete str; return value; }
// // MetaTable::setInt // // If the metatable already contains a metaint of the given name, it will // be edited to have the provided value. Otherwise, a new metaint will be // added to the table with that value. // void MetaTable::setInt(size_t keyIndex, int newValue) { MetaObject *obj; if(!(obj = getObjectKeyAndType(keyIndex, RTTI(MetaInteger)))) addInt(keyIndex, newValue); else static_cast<MetaInteger *>(obj)->value = newValue; }
// // MetaTable::setConstString // // If the table already contains a MetaConstString with the provided key, its // value will be set to newValue. Otherwise, a new MetaConstString will be // created with this key and value and will be added to the table. // void MetaTable::setConstString(size_t keyIndex, const char *newValue) { MetaObject *obj; if(!(obj = getObjectKeyAndType(keyIndex, RTTI(MetaConstString)))) addConstString(keyIndex, newValue); else static_cast<MetaConstString *>(obj)->setValue(newValue); }
// // MetaTable::setDouble // // If the metatable already contains a metadouble of the given name, it will // be edited to have the provided value. Otherwise, a new metadouble will be // added to the table with that value. // void MetaTable::setDouble(const char *key, double newValue) { MetaObject *obj; if(!(obj = getObjectKeyAndType(key, RTTI(MetaDouble)))) addDouble(key, newValue); else static_cast<MetaDouble *>(obj)->value = newValue; }
// // MetaTable::setString // // If the metatable already contains a metastring of the given name, it will // be edited to have the provided value. Otherwise, a new metastring will be // added to the table with that value. // void MetaTable::setString(const char *key, const char *newValue) { MetaObject *obj; if(!(obj = getObjectKeyAndType(key, RTTI(MetaString)))) addString(key, newValue); else static_cast<MetaString *>(obj)->setValue(newValue); }
// // MetaTable::setInt // // If the metatable already contains a metaint of the given name, it will // be edited to have the provided value. Otherwise, a new metaint will be // added to the table with that value. // void MetaTable::setInt(const char *key, int newValue) { MetaObject *obj; if(!(obj = getObjectKeyAndType(key, RTTI(MetaInteger)))) addInt(key, newValue); else static_cast<MetaInteger *>(obj)->value = newValue; }
// // MetaTable::removeStringNR // // As above, but the string value is not returned, but instead freed, to // alleviate any need the user code might have to free string values in // which it isn't interested. // void MetaTable::removeStringNR(const char *key) { MetaObject *obj; metaerrno = META_ERR_NOERR; if(!(obj = getObjectKeyAndType(key, RTTI(MetaString)))) { metaerrno = META_ERR_NOSUCHOBJECT; return; } removeObject(obj); delete obj; }
// // MetaTable::getInt // // Get an integer from the metatable. This routine returns the value // rather than a pointer to a metaint_t. If an object of the requested // name doesn't exist in the table, defvalue is returned and metaerrno // is set to indicate the problem. // // Use of this routine only returns the first such value in the table. // This routine is meant for singleton fields. // int MetaTable::getInt(size_t keyIndex, int defValue) { int retval; MetaObject *obj; metaerrno = META_ERR_NOERR; if(!(obj = getObjectKeyAndType(keyIndex, RTTI(MetaInteger)))) { metaerrno = META_ERR_NOSUCHOBJECT; retval = defValue; } else retval = static_cast<MetaInteger *>(obj)->value; return retval; }
// // MetaTable::getConstString // // Get a sharable string constant/literal value from the MetaTable. If the // requested property does not exist as a MetaConstString, the value provided // by the defValue parameter will be returned, and metaerrno will be set to // META_ERR_NOSUCHOBJECT. Otherwise, the string constant value is returned and // metaerrno is META_ERR_NOERR. // const char *MetaTable::getConstString(const char *key, const char *defValue) { const char *retval; MetaObject *obj; metaerrno = META_ERR_NOERR; if(!(obj = getObjectKeyAndType(key, RTTI(MetaConstString)))) { metaerrno = META_ERR_NOSUCHOBJECT; retval = defValue; } else retval = static_cast<MetaConstString *>(obj)->value; return retval; }
// // MetaTable::getDouble // // Get a double from the metatable. This routine returns the value // rather than a pointer to a metadouble_t. If an object of the requested // name doesn't exist in the table, defvalue is returned and metaerrno is set // to indicate the problem. // // Use of this routine only returns the first such value in the table. // This routine is meant for singleton fields. // double MetaTable::getDouble(const char *key, double defValue) { double retval; MetaObject *obj; metaerrno = META_ERR_NOERR; if(!(obj = getObjectKeyAndType(key, RTTI(MetaDouble)))) { metaerrno = META_ERR_NOSUCHOBJECT; retval = defValue; } else retval = static_cast<MetaDouble *>(obj)->value; return retval; }
// // MetaTable::removeDouble // // Removes the given field if it exists as a metadouble_t. // Only one object will be removed. If more than one such object // exists, you would need to call this routine until metaerrno is // set to META_ERR_NOSUCHOBJECT. // // The value of the object is returned in case it is needed. // double MetaTable::removeDouble(const char *key) { MetaObject *obj; double value; metaerrno = META_ERR_NOERR; if(!(obj = getObjectKeyAndType(key, RTTI(MetaDouble)))) { metaerrno = META_ERR_NOSUCHOBJECT; return 0.0; } removeObject(obj); value = static_cast<MetaDouble *>(obj)->value; delete obj; return value; }
// // MetaTable::removeInt // // Removes the given field if it exists as a metaint_t. // Only one object will be removed. If more than one such object // exists, you would need to call this routine until metaerrno is // set to META_ERR_NOSUCHOBJECT. // // The value of the object is returned in case it is needed. // int MetaTable::removeInt(const char *key) { MetaObject *obj; int value; metaerrno = META_ERR_NOERR; if(!(obj = getObjectKeyAndType(key, RTTI(MetaInteger)))) { metaerrno = META_ERR_NOSUCHOBJECT; return 0; } removeObject(obj); value = static_cast<MetaInteger *>(obj)->value; delete obj; return value; }
// // MetaTable::removeConstString // // Removes a constant string from the table with the given key. If no such // object exists, metaerrno will be META_ERR_NOSUCHOBJECT and NULL is returned. // Otherwise, metaerrno is META_ERR_NOERR and the shared string value that // was in the MetaConstString instance is returned. // const char *MetaTable::removeConstString(const char *key) { MetaObject *obj; MetaConstString *str; const char *value; metaerrno = META_ERR_NOERR; if(!(obj = getObjectKeyAndType(key, RTTI(MetaConstString)))) { metaerrno = META_ERR_NOSUCHOBJECT; return NULL; } removeObject(obj); str = static_cast<MetaConstString *>(obj); value = str->value; delete str; return value; }
// // MetaTable::getObjectKeyAndType // // Overload taking a MetaObject interned key index and type name. // MetaObject *MetaTable::getObjectKeyAndType(size_t keyIndex, const char *type) { MetaObject::Type *rttiType = FindTypeCls<MetaObject>(type); return rttiType ? getObjectKeyAndType(keyIndex, rttiType) : NULL; }
// // MetaTable::getObjectKeyAndType // // As above, but satisfying both conditions at once. // Overload for type names. // MetaObject *MetaTable::getObjectKeyAndType(const char *key, const char *type) const { MetaObject::Type *rttiType = FindTypeCls<MetaObject>(type); return rttiType ? getObjectKeyAndType(key, rttiType) : nullptr; }