From 5e3b14a56a1e8f4a63b868ec4283608acf5c2937 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Mon, 24 Jan 2022 16:58:05 +0100 Subject: [PATCH] Typed template for Extendable::get_data. --- ChangeLog | 5 +++++ examples/cpp/dag-scheduling/s4u-dag-scheduling.cpp | 14 ++++++-------- examples/cpp/io-disk-raw/s4u-io-disk-raw.cpp | 4 ++-- examples/cpp/io-file-system/s4u-io-file-system.cpp | 2 +- include/xbt/Extendable.hpp | 7 ++++++- src/kernel/actor/ActorImpl.hpp | 2 +- src/msg/msg_task.cpp | 2 +- src/plugins/file_system/s4u_FileSystem.cpp | 2 +- src/s4u/s4u_Actor.cpp | 4 ++-- src/s4u/s4u_Disk.cpp | 2 +- src/s4u/s4u_Host.cpp | 2 +- src/s4u/s4u_Link.cpp | 2 +- src/smpi/internals/smpi_global.cpp | 2 +- teshsuite/s4u/basic-link-test/basic-link-test.cpp | 2 +- .../storage_client_server.cpp | 4 ++-- 15 files changed, 32 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index ec1a79743c..1419d43701 100644 --- a/ChangeLog +++ b/ChangeLog @@ -36,6 +36,11 @@ SMPI: change injected costs for MPI_Recv, MPI_Send and MPI_Isend operations. Alternative for smpi/or, smpi/os and smpi/ois configuration options. +XBT: + - Function xbt::Extendable::get_data() is now templated with the type of the + pointee. Untyped function is deprecated. Use get_data() if you still + want to retrieve void*. + Documentation: - New section: "SimGrid MPI calibration of a Grid5000 cluster" presenting how to properly calibrate MPI communications in SimGrid. diff --git a/examples/cpp/dag-scheduling/s4u-dag-scheduling.cpp b/examples/cpp/dag-scheduling/s4u-dag-scheduling.cpp index 4f8334a7e5..f2d1549e36 100644 --- a/examples/cpp/dag-scheduling/s4u-dag-scheduling.cpp +++ b/examples/cpp/dag-scheduling/s4u-dag-scheduling.cpp @@ -20,24 +20,22 @@ struct HostAttribute { static double sg_host_get_available_at(const simgrid::s4u::Host* host) { - return static_cast(host->get_data())->available_at; + return host->get_data()->available_at; } static void sg_host_set_available_at(const simgrid::s4u::Host* host, double time) { - auto* attr = static_cast(host->get_data()); - attr->available_at = time; + host->get_data()->available_at = time; } static simgrid::s4u::Exec* sg_host_get_last_scheduled_task(const simgrid::s4u::Host* host) { - return static_cast(host->get_data())->last_scheduled_task; + return host->get_data()->last_scheduled_task; } static void sg_host_set_last_scheduled_task(const simgrid::s4u::Host* host, simgrid::s4u::ExecPtr task) { - auto* attr = static_cast(host->get_data()); - attr->last_scheduled_task = task.get(); + host->get_data()->last_scheduled_task = task.get(); } static bool dependency_exists(const simgrid::s4u::Exec* src, simgrid::s4u::Exec* dst) @@ -102,7 +100,7 @@ static double finish_on_at(const simgrid::s4u::ExecPtr task, const simgrid::s4u: } // We use the user data field to store the finish time of the predecessor of the comm, i.e., its potential start // time - data_available = *(static_cast(comm->get_data())) + redist_time; + data_available = *comm->get_data() + redist_time; } const auto* exec = dynamic_cast(parent.get()); @@ -148,7 +146,7 @@ static void schedule_on(simgrid::s4u::ExecPtr exec, simgrid::s4u::Host* host) auto* comm = dynamic_cast(pred.get()); if (comm != nullptr) { comm->set_destination(host); - delete static_cast(comm->get_data()); + delete comm->get_data(); } } // we can also set the source of all the output comms of this exec diff --git a/examples/cpp/io-disk-raw/s4u-io-disk-raw.cpp b/examples/cpp/io-disk-raw/s4u-io-disk-raw.cpp index 925291fa99..d86b014cd4 100644 --- a/examples/cpp/io-disk-raw/s4u-io-disk-raw.cpp +++ b/examples/cpp/io-disk-raw/s4u-io-disk-raw.cpp @@ -42,12 +42,12 @@ static void host() /* - Attach some user data to disk1 */ XBT_INFO("*** Get/set data for storage element: Disk1 ***"); - const auto* data = static_cast(disk->get_data()); + const auto* data = disk->get_data(); XBT_INFO("Get storage data: '%s'", data ? data->c_str() : "No user data"); disk->set_data(new std::string("Some user data")); - data = static_cast(disk->get_data()); + data = disk->get_data(); XBT_INFO("Set and get data: '%s'", data->c_str()); delete data; } diff --git a/examples/cpp/io-file-system/s4u-io-file-system.cpp b/examples/cpp/io-file-system/s4u-io-file-system.cpp index bc745d124e..3dc82a05fc 100644 --- a/examples/cpp/io-file-system/s4u-io-file-system.cpp +++ b/examples/cpp/io-file-system/s4u-io-file-system.cpp @@ -57,7 +57,7 @@ public: // Test attaching some user data to the file file->set_data(new std::string("777")); - const auto* file_data = static_cast(file->get_data()); + const auto* file_data = file->get_data(); XBT_INFO("User data attached to the file: %s", file_data->c_str()); delete file_data; diff --git a/include/xbt/Extendable.hpp b/include/xbt/Extendable.hpp index ccfa937744..195f287556 100644 --- a/include/xbt/Extendable.hpp +++ b/include/xbt/Extendable.hpp @@ -7,6 +7,7 @@ #ifndef SIMGRID_XBT_LIB_HPP #define SIMGRID_XBT_LIB_HPP +#include "xbt/base.h" // XBT_ATTRIB_DEPRECATED_v334 #include #include #include @@ -111,7 +112,11 @@ public: void set_data(void* data){ extensions_[0]=data; } - void* get_data() const { return extensions_[0]; } + template D* get_data() const { return static_cast(extensions_[0]); } + XBT_ATTRIB_DEPRECATED_v334("Please use typed template Extendable::get_data<>()") void* get_data() const + { + return get_data(); + } // Convenience extension access when the type has an associated EXTENSION ID: template U* extension() const { return extension(U::EXTENSION_ID); } template void extension_set(U* p) { extension_set(U::EXTENSION_ID, p); } diff --git a/src/kernel/actor/ActorImpl.hpp b/src/kernel/actor/ActorImpl.hpp index 678de9a206..ce54dfcf91 100644 --- a/src/kernel/actor/ActorImpl.hpp +++ b/src/kernel/actor/ActorImpl.hpp @@ -181,7 +181,7 @@ public: explicit ProcessArg(s4u::Host* host, ActorImpl* actor) : name(actor->get_name()) , code(actor->code_) - , data(actor->get_ciface()->get_data()) + , data(actor->get_ciface()->get_data()) , host(host) , kill_time(actor->get_kill_time()) , auto_restart(actor->has_to_auto_restart()) diff --git a/src/msg/msg_task.cpp b/src/msg/msg_task.cpp index 968a79898e..d17bed86c2 100644 --- a/src/msg/msg_task.cpp +++ b/src/msg/msg_task.cpp @@ -246,7 +246,7 @@ msg_task_t MSG_parallel_task_create(const char *name, int host_nb, const msg_hos /** @brief Return the user data of the given task */ void* MSG_task_get_data(const_msg_task_t task) { - return task->get_data(); + return task->get_data(); } /** @brief Sets the user data of a given task */ diff --git a/src/plugins/file_system/s4u_FileSystem.cpp b/src/plugins/file_system/s4u_FileSystem.cpp index 07a10c1421..51d3ccc28c 100644 --- a/src/plugins/file_system/s4u_FileSystem.cpp +++ b/src/plugins/file_system/s4u_FileSystem.cpp @@ -521,7 +521,7 @@ void sg_file_dump(const_sg_file_t fd) */ void* sg_file_get_data(const_sg_file_t fd) { - return fd->get_data(); + return fd->get_data(); } /** Changes the user data associated with the file diff --git a/src/s4u/s4u_Actor.cpp b/src/s4u/s4u_Actor.cpp index 2136988172..d4bd80d313 100644 --- a/src/s4u/s4u_Actor.cpp +++ b/src/s4u/s4u_Actor.cpp @@ -756,7 +756,7 @@ const char* sg_actor_self_get_name() void* sg_actor_self_get_data() { - return simgrid::s4u::Actor::self()->get_data(); + return simgrid::s4u::Actor::self()->get_data(); } void sg_actor_self_set_data(void* userdata) @@ -805,7 +805,7 @@ void sg_actor_unref(const_sg_actor_t actor) /** @brief Return the user data of a #sg_actor_t */ void* sg_actor_get_data(const_sg_actor_t actor) { - return actor->get_data(); + return actor->get_data(); } /** @brief Set the user data of a #sg_actor_t */ diff --git a/src/s4u/s4u_Disk.cpp b/src/s4u/s4u_Disk.cpp index 91b3faa880..b95164d7c1 100644 --- a/src/s4u/s4u_Disk.cpp +++ b/src/s4u/s4u_Disk.cpp @@ -213,7 +213,7 @@ sg_size_t sg_disk_write(const_sg_disk_t disk, sg_size_t size) void* sg_disk_get_data(const_sg_disk_t disk) { - return disk->get_data(); + return disk->get_data(); } void sg_disk_set_data(sg_disk_t disk, void* data) diff --git a/src/s4u/s4u_Host.cpp b/src/s4u/s4u_Host.cpp index aa56f7783d..63d0302aa8 100644 --- a/src/s4u/s4u_Host.cpp +++ b/src/s4u/s4u_Host.cpp @@ -462,7 +462,7 @@ sg_host_t sg_host_by_name(const char* name) // ========== User data Layer ========== void* sg_host_get_data(const_sg_host_t host) { - return host->get_data(); + return host->get_data(); } void sg_host_set_data(sg_host_t host, void* userdata) { diff --git a/src/s4u/s4u_Link.cpp b/src/s4u/s4u_Link.cpp index af8ae86a95..ff36c714b7 100644 --- a/src/s4u/s4u_Link.cpp +++ b/src/s4u/s4u_Link.cpp @@ -254,7 +254,7 @@ void sg_link_set_latency(sg_link_t link, double value) void* sg_link_get_data(const_sg_link_t link) { - return link->get_data(); + return link->get_data(); } void sg_link_set_data(sg_link_t link, void* data) diff --git a/src/smpi/internals/smpi_global.cpp b/src/smpi/internals/smpi_global.cpp index 34a53e516f..b8297e48bd 100644 --- a/src/smpi/internals/smpi_global.cpp +++ b/src/smpi/internals/smpi_global.cpp @@ -120,7 +120,7 @@ MPI_Info smpi_process_info_env(){ } void * smpi_process_get_user_data(){ - return simgrid::s4u::Actor::self()->get_data(); + return simgrid::s4u::Actor::self()->get_data(); } void smpi_process_set_user_data(void *data){ diff --git a/teshsuite/s4u/basic-link-test/basic-link-test.cpp b/teshsuite/s4u/basic-link-test/basic-link-test.cpp index 14380a5d62..bf6013a7a5 100644 --- a/teshsuite/s4u/basic-link-test/basic-link-test.cpp +++ b/teshsuite/s4u/basic-link-test/basic-link-test.cpp @@ -29,7 +29,7 @@ int main(int argc, char** argv) for (const auto& l : links) { XBT_INFO("%s: latency = %.5f, bandwidth = %f", l->get_cname(), l->get_latency(), l->get_bandwidth()); l->set_data(&user_data); - xbt_assert(user_data == *static_cast(l->get_data()), "User data was corrupted."); + xbt_assert(user_data == *l->get_data(), "User data was corrupted."); } return 0; diff --git a/teshsuite/s4u/storage_client_server/storage_client_server.cpp b/teshsuite/s4u/storage_client_server/storage_client_server.cpp index 232c0920cc..8682197626 100644 --- a/teshsuite/s4u/storage_client_server/storage_client_server.cpp +++ b/teshsuite/s4u/storage_client_server/storage_client_server.cpp @@ -74,10 +74,10 @@ static void get_set_disk_data(simgrid::s4u::Disk* disk) { XBT_INFO("*** GET/SET DATA for disk: %s ***", disk->get_cname()); - const std::string* data = static_cast(disk->get_data()); + const std::string* data = disk->get_data(); XBT_INFO("Get data: '%s'", data ? data->c_str() : "No User Data"); disk->set_data(new std::string("Some data")); - data = static_cast(disk->get_data()); + data = disk->get_data(); XBT_INFO(" Set and get data: '%s'", data->c_str()); delete data; } -- 2.20.1