Skip to content

Commit

Permalink
Use reference_wrapper instead of raw pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethassogba committed Nov 25, 2024
1 parent de729e5 commit 0e80e49
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int main() {
Material uo2(sigma_t, sigma_s, sigma_a, sigma_f, nu_sigma_f, chi, "UO2");
std::cout << uo2 << std::endl;

Cell uo2_cell(1.0, &uo2, "UO2");
Cell uo2_cell(1.0, uo2, "UO2");
std::cout << uo2_cell << std::endl;

return 0;
Expand Down
5 changes: 4 additions & 1 deletion src/demeter/model/cell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ void Cell::check() const {
std::string Cell::print() const {
std::ostringstream ss;
ss << "Cell " << name_ << " has " << radii_.size() << " rings and "
<< materials_.size() << " materials";
<< materials_.size() << " materials:\n";
for (const auto& m : materials_) {
ss << " * " << m.get().print();
}
return ss.str();
}
} // namespace Demeter
9 changes: 6 additions & 3 deletions src/demeter/model/cell.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <string>
#include <string_view>
#include <memory>
#include <functional>

#include <Eigen/Core>

#include "demeter/common.hpp"
Expand All @@ -13,13 +15,14 @@ namespace Demeter {

class Cell {
public:
Cell(double side, Material* material, std::string_view name = "")
Cell(double side, Material& material, std::string_view name = "")
: side_(side), name_(name) {
materials_.push_back(material);
}

Cell(double side, std::vector<double>& radii,
std::vector<Material*>& materials, std::string_view name = "")
std::vector<std::reference_wrapper<Material>>& materials,
std::string_view name = "")
: side_(side), radii_(radii), materials_(materials), name_(name) {
check();
}
Expand All @@ -40,7 +43,7 @@ class Cell {

// size_t num_sectors_; //TODO

std::vector<Material*> materials_;
std::vector<std::reference_wrapper<Material>> materials_;

/* A name for the Cell */
std::string name_;
Expand Down

0 comments on commit 0e80e49

Please sign in to comment.