1/**
2 * @file utility.h
3 * General purpose C++ utility
4 *
5 * @remark Copyright 2002 OProfile authors
6 * @remark Read the file COPYING
7 *
8 * @author Philippe Elie
9 * @author John Levon
10 */
11
12#ifndef UTILITY_H
13#define UTILITY_H
14
15#include <cstddef>
16
17/** noncopyable : object of class derived from this class can't be copied
18 * and isn't copy-constructible */
19class noncopyable {
20protected:
21	noncopyable() {}
22	~noncopyable() {}
23private:
24	noncopyable(noncopyable const &);
25	noncopyable const & operator=(noncopyable const &);
26};
27
28
29template<typename T> class scoped_ptr {
30public:
31	explicit scoped_ptr(T * p = 0) : p_(p) {}
32	~scoped_ptr() { delete p_; }
33
34	void reset(T * p = 0) {
35		if (p == p_)
36			return;
37		delete p_;
38		p_ = p;
39	}
40
41	T & operator*() const { return *p_; }
42	T * operator->() const { return p_; }
43	T * get() const { return p_; }
44
45	void swap(scoped_ptr & sp) {
46		T * tmp = sp.p_;
47		sp.p_ = p_;
48		p_ = tmp;
49	}
50
51private:
52	scoped_ptr & operator=(scoped_ptr const &);
53	scoped_ptr(scoped_ptr const &);
54	T * p_;
55};
56
57template<typename T> class scoped_array {
58public:
59	explicit scoped_array(T * p = 0) : p_(p) {}
60	~scoped_array() { delete [] p_; }
61
62	void reset(T * p = 0) {
63		if (p == p_)
64			return;
65		delete [] p_;
66		p_ = p;
67	}
68
69	T & operator[](std::ptrdiff_t i) const { return p_[i]; }
70	T * get() const { return p_; }
71
72	void swap(scoped_array & sp) {
73		T * tmp = sp.p_;
74		sp.p_ = p_;
75		p_ = tmp;
76	}
77
78private:
79	scoped_array & operator=(scoped_array const &);
80	scoped_array(scoped_array const &);
81	T * p_;
82};
83
84/**
85 * @param count
86 * @param total
87 *
88 * return total == 0 ? 1.0 : (count / total);
89 */
90inline double op_ratio(double count, double total)
91{
92	return total == 0 ? 0.0 : (count / total);
93}
94
95// Part copyright:
96//  (C) Copyright boost.org 1999. Permission to copy, use, modify, sell
97//  and distribute this software is granted provided this copyright
98//  notice appears in all copies. This software is provided "as is" without
99//  express or implied warranty, and with no claim as to its suitability for
100//  any purpose.
101
102#endif /* !UTILITY_H */
103