convert ptr_stack to unique_ptr

This commit is contained in:
Hannes Janetzek 2015-07-03 15:09:58 +02:00
parent b0322de6a4
commit 398ec19e81
2 changed files with 17 additions and 13 deletions

View File

@ -136,7 +136,7 @@ void EmitterState::StartedGroup(GroupType::value type) {
pGroup->flowType = FlowType::Flow; pGroup->flowType = FlowType::Flow;
pGroup->indent = GetIndent(); pGroup->indent = GetIndent();
m_groups.push(pGroup); m_groups.push(std::move(pGroup));
} }
void EmitterState::EndedGroup(GroupType::value type) { void EmitterState::EndedGroup(GroupType::value type) {

View File

@ -18,36 +18,40 @@ template <typename T>
class ptr_stack : private YAML::noncopyable { class ptr_stack : private YAML::noncopyable {
public: public:
ptr_stack() {} ptr_stack() {}
~ptr_stack() { clear(); }
void clear() { void clear() {
for (std::size_t i = 0; i < m_data.size(); i++)
delete m_data[i];
m_data.clear(); m_data.clear();
} }
std::size_t size() const { return m_data.size(); } std::size_t size() const { return m_data.size(); }
bool empty() const { return m_data.empty(); } bool empty() const { return m_data.empty(); }
void push(std::unique_ptr<T> &t) { void push(std::unique_ptr<T>&& t) {
m_data.push_back(NULL); m_data.push_back(std::move(t));
m_data.back() = t.release();
} }
std::unique_ptr<T> pop() { std::unique_ptr<T> pop() {
std::unique_ptr<T> t(m_data.back()); std::unique_ptr<T> t(std::move(m_data.back()));
m_data.pop_back(); m_data.pop_back();
return t; return t;
} }
T& top() { return *m_data.back(); }
const T& top() const { return *m_data.back(); }
T& top(std::ptrdiff_t diff) { return **(m_data.end() - 1 + diff); } T& top() {
return *(m_data.back().get());
}
const T& top() const {
return *(m_data.back().get());
}
T& top(std::ptrdiff_t diff) {
return *((m_data.end() - 1 + diff)->get());
}
const T& top(std::ptrdiff_t diff) const { const T& top(std::ptrdiff_t diff) const {
return **(m_data.end() - 1 + diff); return *((m_data.end() - 1 + diff)->get());
} }
private: private:
std::vector<T*> m_data; std::vector<std::unique_ptr<T>> m_data;
}; };
#endif // PTR_STACK_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #endif // PTR_STACK_H_62B23520_7C8E_11DE_8A39_0800200C9A66