• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
19 class noncopyable {
20 protected:
noncopyable()21 	noncopyable() {}
~noncopyable()22 	~noncopyable() {}
23 private:
24 	noncopyable(noncopyable const &);
25 	noncopyable const & operator=(noncopyable const &);
26 };
27 
28 
29 template<typename T> class scoped_ptr {
30 public:
p_(p)31 	explicit scoped_ptr(T * p = 0) : p_(p) {}
~scoped_ptr()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_; }
get()43 	T * get() const { return p_; }
44 
swap(scoped_ptr & sp)45 	void swap(scoped_ptr & sp) {
46 		T * tmp = sp.p_;
47 		sp.p_ = p_;
48 		p_ = tmp;
49 	}
50 
51 private:
52 	scoped_ptr & operator=(scoped_ptr const &);
53 	scoped_ptr(scoped_ptr const &);
54 	T * p_;
55 };
56 
57 template<typename T> class scoped_array {
58 public:
p_(p)59 	explicit scoped_array(T * p = 0) : p_(p) {}
~scoped_array()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]; }
get()70 	T * get() const { return p_; }
71 
swap(scoped_array & sp)72 	void swap(scoped_array & sp) {
73 		T * tmp = sp.p_;
74 		sp.p_ = p_;
75 		p_ = tmp;
76 	}
77 
78 private:
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  */
op_ratio(double count,double total)90 inline 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