ptr_stack.h 1.22 KB
Newer Older
1
2
3
4
5
#pragma once

#ifndef PTR_STACK_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define PTR_STACK_H_62B23520_7C8E_11DE_8A39_0800200C9A66

6
#include <algorithm>
7
8
9
#include <memory>
#include <vector>

10
11
#include "noncopyable.h"

12
template <typename T>
13
class ptr_stack : private YAML::noncopyable
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
public:
	ptr_stack() {}
	~ptr_stack() { clear(); }
	
	void clear() {
		for(unsigned i=0;i<m_data.size();i++)
			delete m_data[i];
		m_data.clear();
	}
	
	std::size_t size() const { return m_data.size(); }
	bool empty() const { return m_data.empty(); }
	
28
29
30
31
32
33
	void push(std::auto_ptr<T> t) {
		// Make sure that the space is available before releasing the
		// auto_ptr to it.  NULL can be deleted safely.
		m_data.push_back(NULL);
		m_data.back() = t.release();
	}
34
35
36
37
38
	std::auto_ptr<T> pop() {
		std::auto_ptr<T> t(m_data.back());
		m_data.pop_back();
		return t;
	}
39
40
41
42
43
44
45
46
47
48
49
	std::auto_ptr<T> pop(T* val) {
		typename std::vector<T*>::reverse_iterator itVal =
		  std::find(m_data.rbegin(), m_data.rend(), val);
		std::auto_ptr<T> t;
		if (itVal != m_data.rend()) {
			t.reset(*itVal);
			m_data.erase((itVal + 1).base());
		}
		return t;
	}
	T& top() const { return *m_data.back(); }
50
51
52
53
54
55
	
private:
	std::vector<T*> m_data;
};

#endif // PTR_STACK_H_62B23520_7C8E_11DE_8A39_0800200C9A66