From a402c017e834eb6f77f709ed1ea65752e4f68494 Mon Sep 17 00:00:00 2001 From: "Paul P.H. Wilson" Date: Thu, 25 Apr 2024 17:28:05 -0500 Subject: [PATCH] promote Package to parent class with templating --- src/material.cc | 23 ----------------------- src/material.h | 3 --- src/product.cc | 23 ----------------------- src/product.h | 3 --- src/resource.h | 25 ++++++++++++++++++++++++- tests/resource_tests.cc | 6 ++++-- 6 files changed, 28 insertions(+), 55 deletions(-) diff --git a/src/material.cc b/src/material.cc index 42b260e735..ca65bf65f6 100644 --- a/src/material.cc +++ b/src/material.cc @@ -227,29 +227,6 @@ double Material::DecayHeat() { return decay_heat; } -std::vector Material::Package(Package::Ptr pkg) { - std::vector ms_pkgd; - Material::Ptr m_pkgd; - - double fill_mass = pkg->GetFillMass(quantity()); - if (fill_mass == 0) { - std::vector rs_pkgd; - return rs_pkgd; - } - - while (quantity() > pkg->fill_min()) { - double pkg_fill = std::min(quantity(), fill_mass); - m_pkgd = boost::dynamic_pointer_cast(ExtractRes(pkg_fill)); - m_pkgd->ChangePackageId(pkg->id()); - ms_pkgd.push_back(m_pkgd); - } - std::vector rs_pkgd; - for (int i = 0; i < ms_pkgd.size(); ++i) { - rs_pkgd.push_back(boost::dynamic_pointer_cast(ms_pkgd[i])); - } - return rs_pkgd; -} - Composition::Ptr Material::comp() const { throw Error("comp() const is deprecated - use non-const comp() function." " Recompilation should fix the problem."); diff --git a/src/material.h b/src/material.h index 32637bfac7..a39b9113ba 100644 --- a/src/material.h +++ b/src/material.h @@ -161,9 +161,6 @@ class Material: public Resource { /// type minimum and maximum mass criteria. virtual void ChangePackageId(int new_package_id = Package::unpackaged_id()); - /// Packages the material - virtual std::vector Package(Package::Ptr pkg); - protected: Material(Context* ctx, double quantity, Composition::Ptr c, int package_id = Package::unpackaged_id()); diff --git a/src/product.cc b/src/product.cc index d6561e6b33..b186ab1d22 100644 --- a/src/product.cc +++ b/src/product.cc @@ -97,29 +97,6 @@ void Product::ChangePackageId(int new_package_id) { } } -std::vector Product::Package(Package::Ptr pkg) { - std::vector ps_pkgd; - Product::Ptr p_pkgd; - - double fill_mass = pkg->GetFillMass(quantity()); - if (fill_mass == 0) { - std::vector rs_pkgd; - return rs_pkgd; - } - - while (quantity() > pkg->fill_min()) { - double pkg_fill = std::min(quantity(), fill_mass); - p_pkgd = boost::dynamic_pointer_cast(ExtractRes(pkg_fill)); - p_pkgd->ChangePackageId(pkg->id()); - ps_pkgd.push_back(p_pkgd); - } - std::vector rs_pkgd; - for (int i = 0; i < ps_pkgd.size(); ++i) { - rs_pkgd.push_back(boost::dynamic_pointer_cast(ps_pkgd[i])); - } - return rs_pkgd; -} - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Product::Product(Context* ctx, double quantity, std::string quality, int package_id) : quality_(quality), diff --git a/src/product.h b/src/product.h index e67e65fd41..6d761b0068 100644 --- a/src/product.h +++ b/src/product.h @@ -77,9 +77,6 @@ class Product : public Resource { /// Changes the product's package id virtual void ChangePackageId(int new_package_id = Package::unpackaged_id()); - // Packages the product - virtual std::vector Package(Package::Ptr pkg); - private: /// @param ctx the simulation context /// @param quantity is a double indicating the quantity diff --git a/src/resource.h b/src/resource.h index 2cdd21cb98..36ed6b2ce7 100644 --- a/src/resource.h +++ b/src/resource.h @@ -101,7 +101,8 @@ class Resource { /// Repackages a single resource into a package. If some quantity of the /// resource cannot be packaged using the given packaging strategy and /// restrictions, the remainder is left in the resource object. - virtual std::vector Package(Package::Ptr pkg) = 0; + template + std::vector Package(Package::Ptr pkg); private: static int nextstate_id_; @@ -126,6 +127,28 @@ typename T::Ptr ResCast(Resource::Ptr r) { return boost::dynamic_pointer_cast(r); } + +template +std::vector Resource::Package(Package::Ptr pkg) { + std::vector ts_pkgd; + typename T::Ptr t_pkgd; + + double fill_mass = pkg->GetFillMass(quantity()); + if (fill_mass == 0) { + return ts_pkgd; + } + + while (quantity() > pkg->fill_min()) { + double pkg_fill = std::min(quantity(), fill_mass); + t_pkgd = boost::dynamic_pointer_cast(ExtractRes(pkg_fill)); + t_pkgd->ChangePackageId(pkg->id()); + ts_pkgd.push_back(t_pkgd); + } + + return ts_pkgd; +} + + } // namespace cyclus #endif // CYCLUS_SRC_RESOURCE_H_ diff --git a/tests/resource_tests.cc b/tests/resource_tests.cc index 7d73feae75..e2e1dc70b3 100644 --- a/tests/resource_tests.cc +++ b/tests/resource_tests.cc @@ -10,6 +10,7 @@ using cyclus::Material; using cyclus::Product; +using cyclus::Resource; using cyclus::Package; class Dummy : public cyclus::Region { @@ -131,13 +132,14 @@ TEST_F(ResourceTest, PackageResource) { // nothing packaged Product::Ptr p3 = p1->Extract(0.5); - std::vector p3_pkgd = p3->Package(pkg); + std::vector p3_pkgd = p3->Package(pkg); + // everything stays in old product, with same (default) package id EXPECT_EQ(p3->package_id(), Package::unpackaged_id()); EXPECT_EQ(p3->quantity(), 0.5); // all packaged - std::vector p1_pkgd = p1->Package(pkg); + std::vector p1_pkgd = p1->Package(pkg); EXPECT_EQ(p1->quantity(), 0); EXPECT_EQ(p1_pkgd[0]->package_id(), pkg_id); }