Skip to content

Commit

Permalink
promote Package to parent class with templating
Browse files Browse the repository at this point in the history
  • Loading branch information
gonuke committed Apr 25, 2024
1 parent dc6d366 commit a402c01
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 55 deletions.
23 changes: 0 additions & 23 deletions src/material.cc
Original file line number Diff line number Diff line change
Expand Up @@ -227,29 +227,6 @@ double Material::DecayHeat() {
return decay_heat;
}

std::vector<Resource::Ptr> Material::Package(Package::Ptr pkg) {
std::vector<Material::Ptr> ms_pkgd;
Material::Ptr m_pkgd;

double fill_mass = pkg->GetFillMass(quantity());
if (fill_mass == 0) {
std::vector<Resource::Ptr> rs_pkgd;
return rs_pkgd;
}

while (quantity() > pkg->fill_min()) {
double pkg_fill = std::min(quantity(), fill_mass);
m_pkgd = boost::dynamic_pointer_cast<Material>(ExtractRes(pkg_fill));
m_pkgd->ChangePackageId(pkg->id());
ms_pkgd.push_back(m_pkgd);
}
std::vector<Resource::Ptr> rs_pkgd;
for (int i = 0; i < ms_pkgd.size(); ++i) {
rs_pkgd.push_back(boost::dynamic_pointer_cast<Resource>(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.");
Expand Down
3 changes: 0 additions & 3 deletions src/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Resource::Ptr> Package(Package::Ptr pkg);

protected:
Material(Context* ctx, double quantity, Composition::Ptr c, int package_id = Package::unpackaged_id());

Expand Down
23 changes: 0 additions & 23 deletions src/product.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,6 @@ void Product::ChangePackageId(int new_package_id) {
}
}

std::vector<Resource::Ptr> Product::Package(Package::Ptr pkg) {
std::vector<Product::Ptr> ps_pkgd;
Product::Ptr p_pkgd;

double fill_mass = pkg->GetFillMass(quantity());
if (fill_mass == 0) {
std::vector<Resource::Ptr> rs_pkgd;
return rs_pkgd;
}

while (quantity() > pkg->fill_min()) {
double pkg_fill = std::min(quantity(), fill_mass);
p_pkgd = boost::dynamic_pointer_cast<Product>(ExtractRes(pkg_fill));
p_pkgd->ChangePackageId(pkg->id());
ps_pkgd.push_back(p_pkgd);
}
std::vector<Resource::Ptr> rs_pkgd;
for (int i = 0; i < ps_pkgd.size(); ++i) {
rs_pkgd.push_back(boost::dynamic_pointer_cast<Resource>(ps_pkgd[i]));
}
return rs_pkgd;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Product::Product(Context* ctx, double quantity, std::string quality, int package_id)
: quality_(quality),
Expand Down
3 changes: 0 additions & 3 deletions src/product.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Resource::Ptr> Package(Package::Ptr pkg);

private:
/// @param ctx the simulation context
/// @param quantity is a double indicating the quantity
Expand Down
25 changes: 24 additions & 1 deletion src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Resource::Ptr> Package(Package::Ptr pkg) = 0;
template <class T>
std::vector<typename T::Ptr> Package(Package::Ptr pkg);

private:
static int nextstate_id_;
Expand All @@ -126,6 +127,28 @@ typename T::Ptr ResCast(Resource::Ptr r) {
return boost::dynamic_pointer_cast<T>(r);
}


template <class T>
std::vector<typename T::Ptr> Resource::Package(Package::Ptr pkg) {
std::vector<typename T::Ptr> 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<T>(ExtractRes(pkg_fill));
t_pkgd->ChangePackageId(pkg->id());
ts_pkgd.push_back(t_pkgd);
}

return ts_pkgd;
}


} // namespace cyclus

#endif // CYCLUS_SRC_RESOURCE_H_
6 changes: 4 additions & 2 deletions tests/resource_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

using cyclus::Material;
using cyclus::Product;
using cyclus::Resource;
using cyclus::Package;

class Dummy : public cyclus::Region {
Expand Down Expand Up @@ -131,13 +132,14 @@ TEST_F(ResourceTest, PackageResource) {

// nothing packaged
Product::Ptr p3 = p1->Extract(0.5);
std::vector<Product::Ptr> p3_pkgd = p3->Package(pkg);
std::vector<Product::Ptr> p3_pkgd = p3->Package<Product>(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<Product::Ptr> p1_pkgd = p1->Package(pkg);
std::vector<Product::Ptr> p1_pkgd = p1->Package<Product>(pkg);
EXPECT_EQ(p1->quantity(), 0);
EXPECT_EQ(p1_pkgd[0]->package_id(), pkg_id);
}

0 comments on commit a402c01

Please sign in to comment.