#pragma once #include #include #include #include #include #include #include #include #include namespace ikarus::util { template T * create_property( struct IkarusProject * project, char const * name, struct IkarusPropertyScope * property_scope, IkarusErrorData * error_out ) { IKARUS_FAIL_IF_NULL(project, nullptr); IKARUS_FAIL_IF_NULL(property_scope, nullptr); IKARUS_FAIL_IF_PROPERTY_SCOPE_INVALID(property_scope, nullptr); IKARUS_FAIL_IF_NAME_INVALID(name, project, property_scope, nullptr); IKARUS_VTRYRV_OR_FAIL( IkarusId const id, nullptr, "failed to create property: {}", IkarusErrorInfo_Database_QueryFailed, project->db->transact([name, property_scope](auto * db) -> cppbase::Result { TRY(db->execute("INSERT INTO `objects`(`type`, `name`, `information`) VALUES(?, ?, ?)", IkarusObjectType_Property, name, "")); auto id = ikarus_id_from_data_and_type(db->last_insert_rowid(), IkarusObjectType_Property); TRY(db->execute( "INSERT INTO `properties`(`id`, `type`, `source`) VALUES(?, ?, ?)", id, T::PropertyType, property_scope->get_id() )); return cppbase::ok(id); }) ); auto * ret = dynamic_cast(project->get_property(id, T::PropertyType)); IKARUS_FAIL_IF( ret == nullptr, nullptr, fmt::format("created {} cannot be casted down from IkarusProject", boost::typeindex::type_id().pretty_name()), IkarusErrorInfo_LibIkarus_InvalidState ); return ret; } template typename T::value_type * get_default_value(IkarusProperty const * property, IkarusErrorData * error_out) { auto * value = ikarus_property_get_default_value(property, error_out); IKARUS_FAIL_IF_ERROR(nullptr); auto * ret = boost::variant2::get_if(&value->data); IKARUS_FAIL_IF( ret == nullptr, nullptr, fmt::format( "{} default value is not a(n) {}", boost::typeindex::type_id().pretty_name(), boost::typeindex::type_id().pretty_name() ), IkarusErrorInfo_Database_InvalidState ); return *ret; } template void set_default_value(T * property, typename T::value_type * new_default_value, IkarusErrorData * error_out) { IKARUS_FAIL_IF_NULL(property, ); IKARUS_FAIL_IF_OBJECT_MISSING(property, ); IKARUS_FAIL_IF_NULL(new_default_value, ); auto value_json_str = boost::json::serialize(new_default_value->to_json()); IKARUS_TRYRV_OR_FAIL( , "unable to set property default value: {}", IkarusErrorInfo_Database_QueryFailed, property->project->db->execute("UPDATE `properties` SET `default_value` = ? WHERE `id` = ?", value_json_str, property->id) ); } } // namespace ikarus::util