Skip to content

Commit

Permalink
Merge pull request #2 from gonuke/unpackage_singleton
Browse files Browse the repository at this point in the history
try singleton pattern for unpackaged
  • Loading branch information
nuclearkatie authored Apr 16, 2024
2 parents 0983c42 + 9b95834 commit df2fdf4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
11 changes: 7 additions & 4 deletions src/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ Context::Context(Timer* ti, Recorder* rec)
trans_id_(0),
si_(0) {
rng_ = new RandomNumberGenerator();
if (packages_.count("unpackaged") == 0) {
packages_["unpackaged"] = Package::CreateUnpackaged();
}
}
}

Context::~Context() {
if (solver_ != NULL) {
Expand Down Expand Up @@ -207,13 +204,19 @@ Package::Ptr Context::AddPackage(std::string name, double fill_min, double fill_
}

Package::Ptr Context::GetPackageByName(std::string name) {
if (name == Package::unpackaged_name()) {
return Package::unpackaged();
}
if (packages_.count(name) == 0) {
throw KeyError("Invalid package name " + name);
}
return packages_[name];
}

Package::Ptr Context::GetPackageById(int id) {
if (id == Package::unpackaged_id()) {
return Package::unpackaged();
}
if (id < 0) {
throw ValueError("Invalid package id " + std::to_string(id));
}
Expand Down
32 changes: 22 additions & 10 deletions src/package.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace cyclus {

// unpackaged id is 1, so start the user-declared packaging id at 2
int Package::next_package_id_ = 2;
Package::Ptr Package::unpackaged_ = NULL;

Package::Ptr Package::Create(std::string name, double fill_min, double fill_max, std::string strategy) {
if (fill_min < 0 || fill_max < 0) {
Expand All @@ -17,9 +18,16 @@ Package::Ptr Package::Create(std::string name, double fill_min, double fill_max,
return p;
}

Package::Ptr Package::CreateUnpackaged() {
Ptr p(new Package(unpackaged_id_, unpackaged_name_));
return p;
// singleton pattern:
// if the static member is not yet set, create a new object
// otherwise return the object that already exists
Package::Ptr& Package::unpackaged() {

if (!unpackaged_) {
unpackaged_ = Ptr(new Package(unpackaged_name_));
}

return unpackaged_;
}

double Package::GetFillMass(double qty) {
Expand All @@ -45,12 +53,16 @@ double Package::GetFillMass(double qty) {
return fill_mass;
}

Package::Package() : id_(next_package_id_++), fill_min_(0), fill_max_(std::numeric_limits<double>::max()) {
name_ = "unnamed";
}

Package::Package(std::string name, double fill_min, double fill_max, std::string strategy) : name_(name), id_(next_package_id_++), fill_min_(fill_min), fill_max_(fill_max), strategy_(strategy) {}

Package::Package(int id, std::string name) : id_(id), name_(name), fill_min_(0), fill_max_(std::numeric_limits<double>::max()) {}
Package::Package(std::string name, double fill_min, double fill_max, std::string strategy) :
name_(name), fill_min_(fill_min), fill_max_(fill_max), strategy_(strategy) {
if (name == unpackaged_name_) {
if (unpackaged_) {
throw ValueError("can't create a new package with name 'unpackaged'");
}
id_ = unpackaged_id_;
} else {
id_ = next_package_id_++;
}
}

} // namespace cyclus
16 changes: 9 additions & 7 deletions src/package.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,18 @@ class Package {
// returns the unpackaged package name
static std::string unpackaged_name() { return unpackaged_name_; }

protected:
Package();
Package(std::string name, double fill_min, double fill_max, std::string strategy);
// creates the unpackaged type. Id is always 1.
static Package::Ptr CreateUnpackaged();
Package(int id, std::string name);
// returns the unpackaged singleton object
static Ptr& unpackaged();

private:
Package(std::string name,
double fill_min = 0,
double fill_max = std::numeric_limits<double>::max(),
std::string strategy = "first");

static const int unpackaged_id_ = 1;
static const std::string unpackaged_name_ = "unpackaged";
static constexpr char unpackaged_name_[11] = "unpackaged";
static Ptr unpackaged_;
static int next_package_id_;

std::string name_;
Expand Down

0 comments on commit df2fdf4

Please sign in to comment.