clean up indentation and spacing
[supercollider.git] / server / supernova / utilities / utils.hpp
blobaa9e4b82f0be3b597aae65c98144fe9ed54fbb28
1 // utility functions
2 // Copyright (C) 2005-2010 Tim Blechmann
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; see the file COPYING. If not, write to
16 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 // Boston, MA 02111-1307, USA.
19 #ifndef UTILITIES_UTILS_HPP
20 #define UTILITIES_UTILS_HPP
22 #include <cstddef>
23 #include <cassert>
25 /** \todo later use std::tr1 type_traits */
26 #include <boost/type_traits.hpp>
27 #include <boost/static_assert.hpp>
29 #include "boost/noncopyable.hpp"
31 /** \todo later use std::tr1 smart pointers */
32 #include <boost/shared_ptr.hpp>
33 #include <boost/scoped_ptr.hpp>
34 #include <boost/weak_ptr.hpp>
35 #include <boost/intrusive_ptr.hpp>
36 #include <boost/detail/atomic_count.hpp>
38 #include "branch_hints.hpp"
40 #include <boost/foreach.hpp>
41 #define foreach BOOST_FOREACH
43 typedef unsigned int uint;
45 #include "malloc_aligned.hpp"
47 #include "function_attributes.h"
50 namespace nova
52 /* we're using some member of the boost namespace */
53 using boost::noncopyable;
55 using boost::shared_ptr;
56 using boost::weak_ptr;
57 using boost::scoped_ptr;
58 using boost::intrusive_ptr;
60 /* some basic math functions */
61 inline bool ispoweroftwo(int i)
63 return (i & (i - 1)) == 0;
67 template <unsigned int n>
68 struct is_power_of_two
70 static const bool val = (n%2==0) && (is_power_of_two<(n>>1)>::val);
73 template <>
74 struct is_power_of_two<2>
76 static const bool val = true;
79 inline int log2(int n)
81 if (unlikely(n <= 0))
82 return(0);
84 #ifdef __GNUC__
85 return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz( n );
86 #else
87 int r = -1;
88 while (n)
90 r++;
91 n >>= 1;
93 return r;
94 #endif
97 inline int nextpoweroftwo(int n)
99 n = n - 1;
100 const uint bitspace = sizeof(int) * 8 / 2;
101 for (uint i = 1; i != bitspace; i *= 2)
102 n = n | (n >> i);
104 return n + 1;
107 using std::size_t;
109 /** \brief base class for a callback function */
110 template <typename t>
111 class runnable
113 public:
114 virtual ~runnable(void)
117 virtual t run(void) = 0;
120 /** \brief tag for denoting a deleteable class */
121 class deleteable
125 template <class T>
126 struct default_deleter
128 void operator()(T * t)
130 delete t;
134 struct delayed_deleter
136 template <typename T>
137 inline void operator()(T *);
140 struct checked_deleter
142 template<class T>
143 void operator()(T * x) const
145 boost::checked_delete(x);
150 template <typename deleter = checked_deleter >
151 struct intrusive_refcountable:
152 public noncopyable,
153 public deleter
155 intrusive_refcountable(void):
156 use_count_(0)
159 virtual ~intrusive_refcountable(void)
162 void add_ref(void)
164 ++use_count_;
167 void release(void)
169 if(--use_count_ == 0)
170 deleter::operator()(this);
173 inline friend void intrusive_ptr_add_ref(intrusive_refcountable * p)
175 p->add_ref();
178 inline friend void intrusive_ptr_release(intrusive_refcountable * p)
180 p->release();
183 boost::detail::atomic_count use_count_;
187 template <class t, class compare = std::less<t> >
188 struct compare_by_instance
190 bool operator()(const t * lhs, const t * rhs)
192 assert(lhs and rhs);
193 compare cmp;
194 return cmp(*lhs, *rhs);
199 PURE inline std::size_t string_hash(const char * str)
201 std::size_t ret = 0;
203 // sdbm hash ... later try another function!
204 int c;
205 while ((c = *str++))
206 ret = c + (ret << 6) + (ret << 16) - ret;
208 return ret;
212 } /* namespace nova */
214 #endif /* UTILITIES_UTILS_HPP */