Skip to content

Commit 781b9cb

Browse files
committed
Replace boost::variant by std::variant
1 parent 1f838ac commit 781b9cb

47 files changed

Lines changed: 529 additions & 399 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -805,11 +805,6 @@ endif()
805805

806806
find_package(Boost 1.83.0 REQUIRED ${ESPRESSO_BOOST_COMPONENTS})
807807

808-
# enable boost::variant with more than 20 types
809-
target_compile_options(
810-
espresso_cpp_flags INTERFACE -DBOOST_MPL_CFG_NO_PREPROCESSED_HEADERS
811-
-DBOOST_MPL_LIMIT_LIST_SIZE=30)
812-
813808
#
814809
# Paths
815810
#

src/core/bond_breakage/actions.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#pragma once
2121

2222
#include <boost/functional/hash.hpp>
23-
#include <boost/variant.hpp>
2423

2524
#include <array>
2625
#include <cstddef>
@@ -74,7 +73,7 @@ struct DeleteAllBonds {
7473
} // namespace BondBreakage
7574

7675
// Hash support for std::unordered_set
77-
namespace boost {
76+
namespace std {
7877
template <> struct hash<BondBreakage::DeleteBond> {
7978
std::size_t operator()(BondBreakage::DeleteBond const &t) const noexcept {
8079
return t.hash_value();
@@ -91,4 +90,4 @@ template <> struct hash<BondBreakage::DeleteAllBonds> {
9190
return t.hash_value();
9291
}
9392
};
94-
} // namespace boost
93+
} // namespace std

src/core/bond_breakage/bond_breakage.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@
3030

3131
#include <boost/mpi.hpp>
3232
#include <boost/serialization/access.hpp>
33-
#include <boost/variant.hpp>
3433

3534
#include <cassert>
3635
#include <memory>
3736
#include <unordered_set>
3837
#include <utility>
38+
#include <variant>
3939
#include <vector>
4040

4141
namespace BondBreakage {
4242

4343
// Variant holding any of the actions
44-
using Action = boost::variant<DeleteBond, DeleteAngleBond, DeleteAllBonds>;
44+
using Action = std::variant<DeleteBond, DeleteAngleBond, DeleteAllBonds>;
4545

4646
// Set of actions
4747
using ActionSet = std::unordered_set<Action>;
@@ -160,7 +160,7 @@ static void remove_pair_bonds_to(Particle &p, int other_pid) {
160160
}
161161

162162
// Handler for the different delete events
163-
class execute : public boost::static_visitor<> {
163+
class execute {
164164
CellStructure &cell_structure;
165165

166166
public:
@@ -199,7 +199,7 @@ void BondBreakage::process_queue_impl(System::System &system) {
199199

200200
// Execute actions
201201
for (auto const &a : actions) {
202-
boost::apply_visitor(execute(cell_structure), a);
202+
std::visit(execute(cell_structure), a);
203203
system.on_particle_change();
204204
}
205205
}

src/core/bonded_interactions/bonded_interaction_data.cpp

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,22 @@
2727
#include "thermalized_bond.hpp"
2828
#include "thermostat.hpp"
2929

30-
#include <boost/variant.hpp>
31-
3230
#include <algorithm>
3331
#include <numeric>
3432
#include <ranges>
35-
36-
/** Visitor to get the bond cutoff from the bond parameter variant */
37-
class BondCutoff : public boost::static_visitor<double> {
38-
public:
39-
template <typename T> double operator()(T const &bond) const {
40-
return bond.cutoff();
41-
}
42-
};
33+
#include <variant>
4334

4435
double BondedInteractionsMap::maximal_cutoff() const {
4536
auto const max_cut_bonded = std::accumulate(
4637
begin(), end(), BONDED_INACTIVE_CUTOFF, [](auto max_cut, auto const &kv) {
47-
return std::max(max_cut,
48-
boost::apply_visitor(BondCutoff(), *kv.second));
38+
auto constexpr visitor = [](auto const &bond) { return bond.cutoff(); };
39+
return std::max(max_cut, std::visit(visitor, *kv.second));
4940
});
5041

5142
/* Check if there are dihedrals */
5243
auto const any_dihedrals = std::ranges::any_of(*this, [](auto const &kv) {
53-
return (boost::get<DihedralBond>(&(*kv.second)) ||
54-
boost::get<TabulatedDihedralBond>(&(*kv.second)));
44+
return (std::holds_alternative<DihedralBond>(*kv.second) or
45+
std::holds_alternative<TabulatedDihedralBond>(*kv.second));
5546
});
5647

5748
/* dihedrals: the central particle is indirectly connected to the fourth
@@ -65,11 +56,11 @@ void BondedInteractionsMap::on_ia_change() {
6556
n_rigid_bonds = 0;
6657
#endif
6758
for (auto const &bond : std::views::elements<1>(*this)) {
68-
if (boost::get<ThermalizedBond>(&(*bond)) != nullptr) {
59+
if (std::holds_alternative<ThermalizedBond>(*bond)) {
6960
++n_thermalized_bonds;
7061
}
7162
#ifdef BOND_CONSTRAINT
72-
if (boost::get<RigidBond>(&(*bond)) != nullptr) {
63+
if (std::holds_alternative<RigidBond>(*bond)) {
7364
++n_rigid_bonds;
7465
}
7566
#endif
@@ -82,30 +73,30 @@ void BondedInteractionsMap::on_ia_change() {
8273

8374
void BondedInteractionsMap::activate_bond(mapped_type const &ptr) {
8475
auto &system = get_system();
85-
if (auto bond = boost::get<ThermalizedBond>(ptr.get())) {
76+
if (auto bond = std::get_if<ThermalizedBond>(ptr.get())) {
8677
bond->set_thermostat_view(system.thermostat);
8778
}
88-
if (auto bond = boost::get<IBMVolCons>(ptr.get())) {
79+
if (auto bond = std::get_if<IBMVolCons>(ptr.get())) {
8980
system.immersed_boundaries->register_softID(*bond);
9081
}
91-
if (auto bond = boost::get<IBMTriel>(ptr.get())) {
82+
if (auto bond = std::get_if<IBMTriel>(ptr.get())) {
9283
bond->initialize(*system.box_geo, *system.cell_structure);
9384
}
94-
if (auto bond = boost::get<IBMTribend>(ptr.get())) {
85+
if (auto bond = std::get_if<IBMTribend>(ptr.get())) {
9586
bond->initialize(*system.box_geo, *system.cell_structure);
9687
}
9788
}
9889

9990
void BondedInteractionsMap::deactivate_bond(mapped_type const &ptr) {
100-
if (auto bond = boost::get<ThermalizedBond>(ptr.get())) {
91+
if (auto bond = std::get_if<ThermalizedBond>(ptr.get())) {
10192
bond->unset_thermostat_view();
10293
n_thermalized_bonds = -1;
10394
}
104-
if (auto bond = boost::get<IBMVolCons>(ptr.get())) {
95+
if (auto bond = std::get_if<IBMVolCons>(ptr.get())) {
10596
bond->unset_volumes_view();
10697
}
10798
#ifdef BOND_CONSTRAINT
108-
if (boost::get<RigidBond>(ptr.get()) != nullptr) {
99+
if (std::get_if<RigidBond>(ptr.get())) {
109100
n_rigid_bonds = -1;
110101
}
111102
#endif

src/core/bonded_interactions/bonded_interaction_data.hpp

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,13 @@
4747
#include "TabulatedPotential.hpp"
4848
#include "system/Leaf.hpp"
4949

50-
#include <boost/variant.hpp>
51-
5250
#include <algorithm>
5351
#include <cassert>
5452
#include <cmath>
5553
#include <optional>
5654
#include <stdexcept>
5755
#include <unordered_map>
56+
#include <variant>
5857
#include <vector>
5958

6059
/* Special cutoff value for a disabled bond.
@@ -74,24 +73,16 @@ struct VirtualBond {
7473
double cutoff() const { return BONDED_INACTIVE_CUTOFF; }
7574
};
7675

77-
/** Visitor to get the number of bound partners from the bond parameter
78-
* variant.
79-
*/
80-
class BondNumPartners : public boost::static_visitor<int> {
81-
public:
82-
template <typename T> int operator()(T const &) const { return T::num; }
83-
};
84-
8576
/** Variant in which to store the parameters of an individual bonded
8677
* interaction
8778
*/
8879
using Bonded_IA_Parameters =
89-
boost::variant<NoneBond, FeneBond, HarmonicBond, QuarticBond, BondedCoulomb,
90-
BondedCoulombSR, AngleHarmonicBond, AngleCosineBond,
91-
AngleCossquareBond, DihedralBond, TabulatedDistanceBond,
92-
TabulatedAngleBond, TabulatedDihedralBond, ThermalizedBond,
93-
RigidBond, IBMTriel, IBMVolCons, IBMTribend,
94-
OifGlobalForcesBond, OifLocalForcesBond, VirtualBond>;
80+
std::variant<NoneBond, FeneBond, HarmonicBond, QuarticBond, BondedCoulomb,
81+
BondedCoulombSR, AngleHarmonicBond, AngleCosineBond,
82+
AngleCossquareBond, DihedralBond, TabulatedDistanceBond,
83+
TabulatedAngleBond, TabulatedDihedralBond, ThermalizedBond,
84+
RigidBond, IBMTriel, IBMVolCons, IBMTribend,
85+
OifGlobalForcesBond, OifLocalForcesBond, VirtualBond>;
9586

9687
/**
9788
* @brief container for bonded interactions.
@@ -147,7 +138,7 @@ class BondedInteractionsMap : public System::Leaf<BondedInteractionsMap> {
147138
auto size() const { return m_params.size(); }
148139
auto get_next_key() const { return next_key; }
149140
auto get_zero_based_type(int bond_id) const {
150-
return contains(bond_id) ? at(bond_id)->which() : 0;
141+
return contains(bond_id) ? static_cast<int>(at(bond_id)->index()) : 0;
151142
}
152143
auto get_n_thermalized_bonds() const {
153144
assert(n_thermalized_bonds >= 0);
@@ -198,7 +189,7 @@ class BondedInteractionsMap : public System::Leaf<BondedInteractionsMap> {
198189
bonds.begin(), bonds.end(),
199190
[this, partner_id = p_partner.id()](BondView const &bond) {
200191
auto const &bond_ptr = at(bond.bond_id());
201-
return (boost::get<BondType>(bond_ptr.get()) != nullptr) and
192+
return std::holds_alternative<BondType>(*bond_ptr.get()) and
202193
(bond.partner_ids()[0] == partner_id);
203194
});
204195
}
@@ -234,5 +225,5 @@ class BondedInteractionsMap : public System::Leaf<BondedInteractionsMap> {
234225

235226
/** @brief Get the number of bonded partners for the specified bond. */
236227
inline int number_of_partners(Bonded_IA_Parameters const &iaparams) {
237-
return boost::apply_visitor(BondNumPartners(), iaparams);
228+
return std::visit([]<typename T>(T const &) { return T::num; }, iaparams);
238229
}

src/core/bonded_interactions/bonded_interactions.dox

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,6 @@
110110
* - Include the header file containing the new bond type.
111111
* - Add the new bond type to @ref Bonded_IA_Parameters at the end of the
112112
* types list.
113-
* - If by doing this, the length of the list in Bonded_IA_Parameters passes
114-
* over a multiple of 10, you may have to update ESPResSo's top level
115-
* CMakeLists.txt:
116-
* @code
117-
* # enable boost::variant with more than 20 types
118-
* target_compile_options(
119-
* espresso_cpp_flags INTERFACE -DBOOST_MPL_CFG_NO_PREPROCESSED_HEADERS
120-
* -DBOOST_MPL_LIMIT_LIST_SIZE=40)
121-
* @endcode
122113
* * In forces_inline.hpp:
123114
* - A call to the new bond's force calculation needs to be placed in either
124115
* of the functions @ref calc_bond_pair_force(), @ref
@@ -128,7 +119,7 @@
128119
* example
129120
* @code{.cpp}
130121
* // ...
131-
* else if (auto const *iap = boost::get<QuarticBond>(&iaparams)) {
122+
* else if (auto const *iap = std::get_if<QuarticBond>(&iaparams)) {
132123
* return iap->force(dx);
133124
* }
134125
* // ...
@@ -141,7 +132,7 @@
141132
* example
142133
* @code{.cpp}
143134
* // ...
144-
* else if (auto const *iap = boost::get<QuarticBond>(&iaparams)) {
135+
* else if (auto const *iap = std::get_if<QuarticBond>(&iaparams)) {
145136
* return iap->energy(dx);
146137
* }
147138
* // ...

src/core/cell_system/CellStructure.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
#include <utils/math/sqr.hpp>
4141

4242
#include <boost/mpi/collectives/all_reduce.hpp>
43-
#include <boost/variant.hpp>
4443

4544
#include <algorithm>
4645
#include <cassert>
@@ -53,6 +52,7 @@
5352
#include <stdexcept>
5453
#include <string>
5554
#include <utility>
55+
#include <variant>
5656
#include <vector>
5757

5858
#ifdef SHARED_MEMORY_PARALLELISM
@@ -231,7 +231,7 @@ void CellStructure::resort_particles(bool global_flag) {
231231
m_decomposition->resort(global_flag, diff);
232232

233233
for (auto d : diff) {
234-
boost::apply_visitor(UpdateParticleIndexVisitor{this}, d);
234+
std::visit(UpdateParticleIndexVisitor{this}, d);
235235
}
236236

237237
auto const &lebc = get_system().box_geo->lees_edwards_bc();

src/core/cell_system/ParticleDecomposition.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@
2626

2727
#include <utils/Vector.hpp>
2828

29-
#include <boost/variant.hpp>
30-
3129
#include <optional>
3230
#include <span>
31+
#include <variant>
3332
#include <vector>
3433

3534
struct RemovedParticle {
@@ -43,7 +42,7 @@ struct ModifiedList {
4342
/**
4443
* @brief Change of Particle Address.
4544
*/
46-
using ParticleChange = boost::variant<RemovedParticle, ModifiedList>;
45+
using ParticleChange = std::variant<RemovedParticle, ModifiedList>;
4746

4847
/**
4948
* @brief A distributed particle decomposition.

0 commit comments

Comments
 (0)