From 31129c80d17639ad106452ecac4eb924de7a2d22 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Wed, 28 Oct 2020 17:32:40 +0100 Subject: [PATCH] Prefer enum class over legacy enum. --- src/kernel/resource/profile/Profile.cpp | 16 +++++++------- src/kernel/resource/profile/Profile_test.cpp | 22 ++++++++++--------- .../resource/profile/StochasticDatedValue.cpp | 8 +++---- .../resource/profile/StochasticDatedValue.hpp | 13 +++++------ 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/kernel/resource/profile/Profile.cpp b/src/kernel/resource/profile/Profile.cpp index 587a063a67..a8ac733ce3 100644 --- a/src/kernel/resource/profile/Profile.cpp +++ b/src/kernel/resource/profile/Profile.cpp @@ -133,17 +133,17 @@ Profile* Profile::from_string(const std::string& name, const std::string& input, xbt_assert(splittedval.size() > 0, "Invalid profile line"); if (splittedval[0] == "DET") { - stochevent.date_law = Dist_Det; + stochevent.date_law = Distribution::DET; i = 2; } else if (splittedval[0] == "NORM" || splittedval[0] == "NORMAL" || splittedval[0] == "GAUSS" || splittedval[0] == "GAUSSIAN") { - stochevent.date_law = Dist_Norm; + stochevent.date_law = Distribution::NORM; i = 3; } else if (splittedval[0] == "EXP" || splittedval[0] == "EXPONENTIAL") { - stochevent.date_law = Dist_Exp; + stochevent.date_law = Distribution::EXP; i = 2; } else if (splittedval[0] == "UNIF" || splittedval[0] == "UNIFORM") { - stochevent.date_law = Dist_Unif; + stochevent.date_law = Distribution::UNIF; i = 3; } else { xbt_assert(false, "Unknown law %s", splittedval[0].c_str()); @@ -158,17 +158,17 @@ Profile* Profile::from_string(const std::string& name, const std::string& input, } if (splittedval[i] == "DET") { - stochevent.value_law = Dist_Det; + stochevent.value_law = Distribution::DET; j = 1; } else if (splittedval[i] == "NORM" || splittedval[i] == "NORMAL" || splittedval[i] == "GAUSS" || splittedval[i] == "GAUSSIAN") { - stochevent.value_law = Dist_Norm; + stochevent.value_law = Distribution::NORM; j = 2; } else if (splittedval[i] == "EXP" || splittedval[i] == "EXPONENTIAL") { - stochevent.value_law = Dist_Exp; + stochevent.value_law = Distribution::EXP; j = 1; } else if (splittedval[i] == "UNIF" || splittedval[i] == "UNIFORM") { - stochevent.value_law = Dist_Unif; + stochevent.value_law = Distribution::UNIF; j = 2; } else { xbt_assert(false, "Unknown law %s", splittedval[i].c_str()); diff --git a/src/kernel/resource/profile/Profile_test.cpp b/src/kernel/resource/profile/Profile_test.cpp index 55f4c610d8..3dce615cd0 100644 --- a/src/kernel/resource/profile/Profile_test.cpp +++ b/src/kernel/resource/profile/Profile_test.cpp @@ -154,19 +154,21 @@ TEST_CASE("kernel::profile: Resource profiles, defining the external load", "ker SECTION("One stochastic event (parsing)") { + using simgrid::kernel::profile::Distribution; std::vector got = trace2selist("STOCHASTIC\n" "DET 0 UNIF 10 20"); std::vector want; want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(0, -1)); // The initial fake event - want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Det, {0}, - simgrid::kernel::profile::Dist_Unif, {10, 20})); + want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(Distribution::DET, {0}, + Distribution::UNIF, {10, 20})); REQUIRE(want == got); } SECTION("Several stochastic events (all possible parsing forms)") { + using simgrid::kernel::profile::Distribution; std::vector got = trace2selist("STOCHASTIC\n" "DET 0 DET 4\n" "NORMAL 25 10 DET 3\n" @@ -175,14 +177,14 @@ TEST_CASE("kernel::profile: Resource profiles, defining the external load", "ker std::vector want; want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(0, -1)); - want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Det, {0}, - simgrid::kernel::profile::Dist_Det, {4})); - want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Norm, {25, 10}, - simgrid::kernel::profile::Dist_Det, {3})); - want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Unif, {10, 20}, - simgrid::kernel::profile::Dist_Norm, {25, 10})); - want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Det, {5}, - simgrid::kernel::profile::Dist_Unif, {5, 25})); + want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(Distribution::DET, {0}, + Distribution::DET, {4})); + want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(Distribution::NORM, {25, 10}, + Distribution::DET, {3})); + want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(Distribution::UNIF, {10, 20}, + Distribution::NORM, {25, 10})); + want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(Distribution::DET, {5}, + Distribution::UNIF, {5, 25})); REQUIRE(want == got); } diff --git a/src/kernel/resource/profile/StochasticDatedValue.cpp b/src/kernel/resource/profile/StochasticDatedValue.cpp index 3cf0407aef..0695de3e53 100644 --- a/src/kernel/resource/profile/StochasticDatedValue.cpp +++ b/src/kernel/resource/profile/StochasticDatedValue.cpp @@ -15,13 +15,13 @@ namespace profile { double StochasticDatedValue::draw(Distribution law, std::vector params) { switch (law) { - case Dist_Det: + case Distribution::DET: return params[0]; - case Dist_Exp: + case Distribution::EXP: return simgrid::xbt::random::exponential(params[0]); - case Dist_Unif: + case Distribution::UNIF: return simgrid::xbt::random::uniform_real(params[0], params[1]); - case Dist_Norm: + case Distribution::NORM: return simgrid::xbt::random::normal(params[0], params[1]); default: xbt_assert(false, "Unimplemented distribution"); diff --git a/src/kernel/resource/profile/StochasticDatedValue.hpp b/src/kernel/resource/profile/StochasticDatedValue.hpp index 4533bb304e..f4320390cc 100644 --- a/src/kernel/resource/profile/StochasticDatedValue.hpp +++ b/src/kernel/resource/profile/StochasticDatedValue.hpp @@ -14,22 +14,19 @@ namespace simgrid { namespace kernel { namespace profile { -enum Distribution { Dist_Exp, Dist_Norm, Dist_Unif, Dist_Det }; +enum class Distribution { EXP, NORM, UNIF, DET }; class XBT_PUBLIC StochasticDatedValue { public: - Distribution date_law; + Distribution date_law = Distribution::DET; std::vector date_params; - Distribution value_law; + Distribution value_law = Distribution::DET; std::vector value_params; DatedValue get_datedvalue(); double get_date(); double get_value(); explicit StochasticDatedValue() = default; - explicit StochasticDatedValue(double d, double v) - : date_law(Dist_Det), date_params({d}), value_law(Dist_Det), value_params({v}) - { - } + explicit StochasticDatedValue(double d, double v) : date_params({d}), value_params({v}) {} explicit StochasticDatedValue(Distribution dl, const std::vector& dp, Distribution vl, const std::vector& vp) : date_law(dl), date_params(dp), value_law(vl), value_params(vp) @@ -38,7 +35,7 @@ public: bool operator==(StochasticDatedValue const& e2) const; private: - double draw(Distribution law, std::vector params); + static double draw(Distribution law, std::vector params); }; } // namespace profile -- 2.20.1