From 5fc309f0124ca91be813d84d9c4f597162f07a05 Mon Sep 17 00:00:00 2001 From: Frederic Suter Date: Thu, 14 Mar 2019 11:14:27 +0100 Subject: [PATCH] Start to use Curiously Recurring Template Patterns --- include/simgrid/s4u/Activity.hpp | 52 ++++++++++++++++++++++++-------- include/simgrid/s4u/Comm.hpp | 8 ++--- include/simgrid/s4u/Exec.hpp | 6 +--- include/simgrid/s4u/Io.hpp | 3 +- src/s4u/s4u_Activity.cpp | 1 - src/s4u/s4u_Comm.cpp | 10 +++--- src/s4u/s4u_Exec.cpp | 16 +--------- src/s4u/s4u_Io.cpp | 4 +-- 8 files changed, 53 insertions(+), 47 deletions(-) diff --git a/include/simgrid/s4u/Activity.hpp b/include/simgrid/s4u/Activity.hpp index d478353ff1..085c853c97 100644 --- a/include/simgrid/s4u/Activity.hpp +++ b/include/simgrid/s4u/Activity.hpp @@ -6,6 +6,7 @@ #ifndef SIMGRID_S4U_ACTIVITY_HPP #define SIMGRID_S4U_ACTIVITY_HPP +#include "xbt/asserts.h" #include #include @@ -84,13 +85,6 @@ public: Activity* set_remaining(double remains); /** Put some user data onto the Activity */ - Activity* set_user_data(void* data) - { - user_data_ = data; - return this; - } - /** Retrieve the user data of the Activity */ - void* get_user_data() { return user_data_; } kernel::activity::ActivityImplPtr get_impl() { return pimpl_; } @@ -102,18 +96,50 @@ public: { return set_remaining(remains); } - XBT_ATTRIB_DEPRECATED_v323("Please use Activity::set_user_data()") Activity* setUserData(void* data) - { - return set_user_data(data); - } - XBT_ATTRIB_DEPRECATED_v323("Please use Activity::get_user_data()") void* getUserData() { return user_data_; } #endif private: kernel::activity::ActivityImplPtr pimpl_ = nullptr; Activity::State state_ = Activity::State::INITED; double remains_ = 0; - void* user_data_ = nullptr; +}; + +template class Activity_T : public Activity { +private: + std::string name_ = ""; + std::string tracing_category_ = ""; + void* user_data_ = nullptr; + +public: + AnyActivity* set_name(const std::string& name) + { + xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start"); + name_ = name; + return static_cast(this); + } + const std::string& get_name() { return name_; } + const char* get_cname() { return name_.c_str(); } + + AnyActivity* set_tracing_category(const std::string& category) + { + xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start"); + tracing_category_ = category; + return static_cast(this); + } + const std::string& get_tracing_category() { return tracing_category_; } + + AnyActivity* set_user_data(void* data) + { + user_data_ = data; + return static_cast(this); + } + + void* get_user_data() { return user_data_; } + XBT_ATTRIB_DEPRECATED_v323("Please use Activity::set_user_data()") AnyActivity* setUserData(void* data) + { + return set_user_data(data); + } + XBT_ATTRIB_DEPRECATED_v323("Please use Activity::get_user_data()") void* getUserData() { return user_data_; } }; } // namespace s4u diff --git a/include/simgrid/s4u/Comm.hpp b/include/simgrid/s4u/Comm.hpp index 575d40ec54..abf147a188 100644 --- a/include/simgrid/s4u/Comm.hpp +++ b/include/simgrid/s4u/Comm.hpp @@ -18,7 +18,7 @@ namespace s4u { * * Represents all asynchronous communications, that you can test or wait onto. */ -class XBT_PUBLIC Comm : public Activity { +class XBT_PUBLIC Comm : public Activity_T { Mailbox* mailbox_ = nullptr; kernel::actor::ActorImpl* sender_ = nullptr; kernel::actor::ActorImpl* receiver_ = nullptr; @@ -35,11 +35,11 @@ class XBT_PUBLIC Comm : public Activity { void (*clean_fun_)(void*) = nullptr; void (*copy_data_function_)(kernel::activity::CommImpl*, void*, size_t) = nullptr; - Comm() : Activity() {} + Comm() = default; public: - friend XBT_PUBLIC void intrusive_ptr_release(simgrid::s4u::Comm * c); - friend XBT_PUBLIC void intrusive_ptr_add_ref(simgrid::s4u::Comm * c); + friend XBT_PUBLIC void intrusive_ptr_release(Comm* c); + friend XBT_PUBLIC void intrusive_ptr_add_ref(Comm* c); friend Mailbox; // Factory of comms virtual ~Comm(); diff --git a/include/simgrid/s4u/Exec.hpp b/include/simgrid/s4u/Exec.hpp index beed7e1df8..73f3d236a4 100644 --- a/include/simgrid/s4u/Exec.hpp +++ b/include/simgrid/s4u/Exec.hpp @@ -20,12 +20,10 @@ namespace s4u { * They are generated from this_actor::exec_init() or Host::execute(), and can be used to model pools of threads or * similar mechanisms. */ -class XBT_PUBLIC Exec : public Activity { - std::string name_ = ""; +class XBT_PUBLIC Exec : public Activity_T { double priority_ = 1.0; double bound_ = 0.0; double timeout_ = 0.0; - std::string tracing_category_ = ""; std::atomic_int_fast32_t refcount_{0}; Host* host_ = nullptr; @@ -55,9 +53,7 @@ public: bool test() override; ExecPtr set_bound(double bound); - ExecPtr set_name(const std::string& name); ExecPtr set_priority(double priority); - ExecPtr set_tracing_category(const std::string& category); ExecPtr set_timeout(double timeout); Exec* cancel() override; diff --git a/include/simgrid/s4u/Io.hpp b/include/simgrid/s4u/Io.hpp index 8ac60403f2..395dfb974b 100644 --- a/include/simgrid/s4u/Io.hpp +++ b/include/simgrid/s4u/Io.hpp @@ -20,7 +20,7 @@ namespace s4u { * They are generated from Storage::io_init() or Storage::read() and Storage::write(). */ -class XBT_PUBLIC Io : public Activity { +class XBT_PUBLIC Io : public Activity_T { public: enum class OpType { READ, WRITE }; @@ -28,7 +28,6 @@ private: Storage* storage_ = nullptr; sg_size_t size_ = 0; OpType type_ = OpType::READ; - std::string name_ = ""; std::atomic_int_fast32_t refcount_{0}; explicit Io(sg_storage_t storage, sg_size_t size, OpType type); diff --git a/src/s4u/s4u_Activity.cpp b/src/s4u/s4u_Activity.cpp index 2d3f344302..e52b3f2a13 100644 --- a/src/s4u/s4u_Activity.cpp +++ b/src/s4u/s4u_Activity.cpp @@ -3,7 +3,6 @@ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ -#include "xbt/asserts.h" #include "xbt/log.h" #include "simgrid/s4u/Activity.hpp" diff --git a/src/s4u/s4u_Comm.cpp b/src/s4u/s4u_Comm.cpp index 16b1284b19..03d222affe 100644 --- a/src/s4u/s4u_Comm.cpp +++ b/src/s4u/s4u_Comm.cpp @@ -108,18 +108,18 @@ CommPtr Comm::set_dst_data(void** buff, size_t size) Comm* Comm::start() { - xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)", + xbt_assert(get_state() == State::INITED, "You cannot use %s() once your communication started (not implemented)", __FUNCTION__); if (src_buff_ != nullptr) { // Sender side on_sender_start(Actor::self()); pimpl_ = simcall_comm_isend(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_, - clean_fun_, copy_data_function_, user_data_, detached_); + clean_fun_, copy_data_function_, get_user_data(), detached_); } else if (dst_buff_ != nullptr) { // Receiver side xbt_assert(not detached_, "Receive cannot be detached"); on_receiver_start(Actor::self()); pimpl_ = simcall_comm_irecv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, - copy_data_function_, user_data_, rate_); + copy_data_function_, get_user_data(), rate_); } else { xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver"); @@ -150,12 +150,12 @@ Comm* Comm::wait_for(double timeout) if (src_buff_ != nullptr) { on_sender_start(Actor::self()); simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_, - copy_data_function_, user_data_, timeout); + copy_data_function_, get_user_data(), timeout); } else { // Receiver on_receiver_start(Actor::self()); simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_, - user_data_, timeout, rate_); + get_user_data(), timeout, rate_); } state_ = State::FINISHED; break; diff --git a/src/s4u/s4u_Exec.cpp b/src/s4u/s4u_Exec.cpp index c3431b5efb..27ff9fd985 100644 --- a/src/s4u/s4u_Exec.cpp +++ b/src/s4u/s4u_Exec.cpp @@ -17,7 +17,7 @@ xbt::signal Exec::on_completion; Exec::Exec() { - pimpl_ = kernel::activity::ExecImplPtr(new kernel::activity::ExecImpl(name_, tracing_category_)); + pimpl_ = kernel::activity::ExecImplPtr(new kernel::activity::ExecImpl(get_name(), get_tracing_category())); } bool Exec::test() @@ -90,13 +90,6 @@ ExecPtr Exec::set_timeout(double timeout) return this; } -ExecPtr Exec::set_name(const std::string& name) -{ - xbt_assert(state_ == State::INITED, "Cannot change the name of an exec after its start"); - name_ = name; - return this; -} - /** @brief Change the execution priority, don't you think? * * An execution with twice the priority will get twice the amount of flops when the resource is shared. @@ -110,13 +103,6 @@ ExecPtr Exec::set_priority(double priority) return this; } -ExecPtr Exec::set_tracing_category(const std::string& category) -{ - xbt_assert(state_ == State::INITED, "Cannot change the tracing category of an exec after its start"); - tracing_category_ = category; - return this; -} - ///////////// SEQUENTIAL EXECUTIONS //////// ExecSeq::ExecSeq(sg_host_t host, double flops_amount) : Exec(), flops_amount_(flops_amount) { diff --git a/src/s4u/s4u_Io.cpp b/src/s4u/s4u_Io.cpp index 7945a3a3c8..eb450ed35d 100644 --- a/src/s4u/s4u_Io.cpp +++ b/src/s4u/s4u_Io.cpp @@ -13,10 +13,10 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_io, s4u_activity, "S4U asynchronous IOs"); namespace simgrid { namespace s4u { -Io::Io(sg_storage_t storage, sg_size_t size, OpType type) : Activity(), storage_(storage), size_(size), type_(type) +Io::Io(sg_storage_t storage, sg_size_t size, OpType type) : storage_(storage), size_(size), type_(type) { Activity::set_remaining(size_); - pimpl_ = kernel::activity::IoImplPtr(new kernel::activity::IoImpl(name_, storage_->get_impl())); + pimpl_ = kernel::activity::IoImplPtr(new kernel::activity::IoImpl(get_name(), storage_->get_impl())); } Io* Io::start() -- 2.20.1