-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmart_pointers.cpp
37 lines (29 loc) · 1.1 KB
/
smart_pointers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Examples of the use of smart pointers to describe ownership
// Author: Jeff Trull <[email protected]>
#include <algorithm>
#include "smart_pointers.h"
Instance::Instance(std::string const & name,
std::shared_ptr<const Cell> cell,
std::weak_ptr<Design> parent)
: name_(name), cell_(std::move(cell)), parent_(parent) {}
std::shared_ptr<Design>
Instance::parent() const {
return std::shared_ptr<Design>(parent_);
}
std::string const&
Instance::name() const {
return name_;
}
std::shared_ptr<Instance>
Design::addInstance(std::shared_ptr<const Cell> cell, std::string name) {
instances_.emplace_back(std::make_shared<Instance>(std::move(name), cell, shared_from_this()));
return instances_.back();
}
void
Design::removeInstance(std::string const& name) {
// apply the erase-remove idiom for instances with the given name
instances_.erase(std::remove_if(instances_.begin(),
instances_.end(),
[&](auto inst) { return inst->name() == name; }),
instances_.end());
}