[Assorted-commits] SF.net SVN: assorted:[1275] cpp-commons/trunk/src/commons
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-03-09 22:56:40
|
Revision: 1275 http://assorted.svn.sourceforge.net/assorted/?rev=1275&view=rev Author: yangzhang Date: 2009-03-09 22:56:37 +0000 (Mon, 09 Mar 2009) Log Message: ----------- - added fast_map::set_size - added c++0x unique_ptr - added memory.h with memput, memget, raw_reader, raw_writer - use proper moving/forwarding in st_channel - made array moveable, non-copyable Modified Paths: -------------- cpp-commons/trunk/src/commons/array.h cpp-commons/trunk/src/commons/fast_map.h cpp-commons/trunk/src/commons/st/st.h Added Paths: ----------- cpp-commons/trunk/src/commons/memory.h cpp-commons/trunk/src/commons/unique_ptr.h Modified: cpp-commons/trunk/src/commons/array.h =================================================================== --- cpp-commons/trunk/src/commons/array.h 2009-03-09 22:55:47 UTC (rev 1274) +++ cpp-commons/trunk/src/commons/array.h 2009-03-09 22:56:37 UTC (rev 1275) @@ -2,15 +2,14 @@ #define COMMONS_ARRAY_H #include <algorithm> -#include <boost/unique_ptr.hpp> #include <commons/algo.h> #include <commons/check.h> #include <commons/nullptr.h> +#include <commons/unique_ptr.h> #include <commons/utility.h> namespace commons { - using namespace boost; using namespace std; template<typename T> class array; @@ -48,9 +47,26 @@ template<typename T> class array { EXPAND(unique_ptr<T[]>) + NONCOPYABLE(array) friend void swap<>(array<T> &a, array<T> &b); public: explicit array(size_t n) : p_(new T[n]), n_(n) {} + array() : p_(), n_(0) {} + array(array<T> &&src) : p_(src.release()), n_(src.n_) {} +#if 0 + array(array<T> src) : p_(new T[src.size()]), n_(src.size()) { + memcpy(p_.get(), src.get(), size()); + } +#endif + array<T> &operator=(array<T> &&src) { + if (this != &src) { + p_.reset(src.release()); + n_ = src.n_; + } + return *this; + } + operator const T*() const { return p_.get(); } + operator T*() { return p_.get(); } size_t size() const { return n_; } T *get() const { return p_.get(); } T *release() { return p_.release(); } @@ -71,7 +87,7 @@ void swap(array<T> &a, array<T> &b) { - boost::swap(a.p_, b.p_); + std::swap(a.p_, b.p_); swap(a.n_, b.n_); } @@ -95,9 +111,7 @@ NONCOPYABLE(managed_array) public: managed_array(T *p, bool scoped) : p_(p), scoped_(scoped) {} -#ifdef __GXX_EXPERIMENTAL_CXX0X__ managed_array(managed_array<T> &&a) : p_(a.p_), scoped_(a.scoped_) { a.release(); } -#endif ~managed_array() { if (scoped_) delete [] p_; } T *release() { T *p = p_; p_ = nullptr; scoped_ = false; return p; } T *get() { return p_; } Modified: cpp-commons/trunk/src/commons/fast_map.h =================================================================== --- cpp-commons/trunk/src/commons/fast_map.h 2009-03-09 22:55:47 UTC (rev 1274) +++ cpp-commons/trunk/src/commons/fast_map.h 2009-03-09 22:56:37 UTC (rev 1275) @@ -193,6 +193,7 @@ resize(initsize); } size_t size() const { return count; } + void set_size(size_t size) { count = size; } void erase(iterator) { throw_not_implemented(); } array<value_type> &get_table() { return table; } const array<value_type> &get_table() const { return table; } @@ -225,6 +226,7 @@ if (table[pos].first == empty_key) return end(); if (table[pos].first == k) return const_iterator(*this, &table[pos]); pos = (pos + ++probe) & mask; + assert(probe < table.size()); } } @@ -235,6 +237,7 @@ if (table[pos].first == empty_key) return end(); if (table[pos].first == k) return iterator(*this, &table[pos]); pos = (pos + ++probe) & mask; + assert(probe < table.size()); } #if 0 for (; Added: cpp-commons/trunk/src/commons/memory.h =================================================================== --- cpp-commons/trunk/src/commons/memory.h (rev 0) +++ cpp-commons/trunk/src/commons/memory.h 2009-03-09 22:56:37 UTC (rev 1275) @@ -0,0 +1,57 @@ +#ifndef COMMONS_MEMORY_H +#define COMMONS_MEMORY_H + +namespace commons +{ + + /** + * Copy a datum directly to a memory location. Useful for serializing small + * data (e.g. ints). Faster than memcpy. + */ + template<typename T> + void memput(void *dst, const T &src) { + *reinterpret_cast<T*>(dst) = src; + } + + template<typename T> + void memget(void *src, T &dst) { + dst = *reinterpret_cast<T*>(src); + } + + template<typename T> + T &memget(void *src) { + return *reinterpret_cast<T*>(src); + } + + /** + * Lets you write a bunch of values to a buffer. + */ + class raw_writer + { + private: + char *p_; + public: + /** Initialize the pointer to point to p. */ + raw_writer(void *p) : p_(reinterpret_cast<char*>(p)) {} + /** Write a datum to the buffer, advancing the pointer. */ + template<typename T> + void write(const T &x) { memput(p_, x); p_ += sizeof(T); } + /** Get the current value of the pointer. */ + void *ptr() const { return p_; } + }; + + class raw_reader + { + private: + char *p_; + public: + raw_reader(void *p) : p_(reinterpret_cast<char*>(p)) {} + template<typename T> + void read(T& x) { memget(p_, x); p_ += sizeof(T); } + template<typename T> + T &read() { void *p = p_; p_ += sizeof(T); return memget<T>(p_); } + }; + +} + +#endif Modified: cpp-commons/trunk/src/commons/st/st.h =================================================================== --- cpp-commons/trunk/src/commons/st/st.h 2009-03-09 22:55:47 UTC (rev 1274) +++ cpp-commons/trunk/src/commons/st/st.h 2009-03-09 22:56:37 UTC (rev 1275) @@ -18,6 +18,7 @@ #include <sstream> #include <st.h> #include <stx.h> +#include <utility> #define foreach BOOST_FOREACH #define shared_ptr boost::shared_ptr @@ -196,21 +197,17 @@ class st_channel { public: - void push(T &&x) { - q_.push(boost::move(x)); + template<typename U> void push(U &&x) { + q_.push(forward<U>(x)); empty_.signal(); } - void push(const T &x) { - q_.push(x); - empty_.signal(); - } T take() { while (q_.empty()) { empty_.wait(); } - T x = boost::move(front()); + T x = move(front()); q_.pop(); - return boost::move(x); + return x; } const T& front() const { return q_.front(); } T& front() { return q_.front(); } Added: cpp-commons/trunk/src/commons/unique_ptr.h =================================================================== --- cpp-commons/trunk/src/commons/unique_ptr.h (rev 0) +++ cpp-commons/trunk/src/commons/unique_ptr.h 2009-03-09 22:56:37 UTC (rev 1275) @@ -0,0 +1,459 @@ +// unique_ptr implementation -*- C++ -*- + +// Copyright (C) 2008, 2009 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02110-1301, USA. + +// As a special exception, you may use this file as part of a free software +// library without restriction. Specifically, if other files instantiate +// templates or use macros or inline functions from this file, or you compile +// this file and link it with other files to produce an executable, this +// file does not by itself cause the resulting executable to be covered by +// the GNU General Public License. This exception does not however +// invalidate any other reasons why the executable file might be covered by +// the GNU General Public License. + +/** @file unique_ptr.h + * This is an internal header file, included by other library headers. + * You should not attempt to use it directly. + */ + +#ifndef _UNIQUE_PTR_H +#define _UNIQUE_PTR_H 1 + +#ifndef __GXX_EXPERIMENTAL_CXX0X__ +# include <c++0x_warning.h> +#endif + +#include <bits/c++config.h> +#include <debug/debug.h> +#include <type_traits> +#include <utility> +#include <tuple> + +_GLIBCXX_BEGIN_NAMESPACE(std) + + /** + * @addtogroup pointer_abstractions + * @{ + */ + + /// Primary template, default_delete. + template<typename _Tp> + struct default_delete + { + default_delete() { } + + template<typename _Up> + default_delete(const default_delete<_Up>&) { } + + void + operator()(_Tp* __ptr) const + { + static_assert(sizeof(_Tp)>0, + "can't delete pointer to incomplete type"); + delete __ptr; + } + }; + + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // DR 740 - omit specialization for array objects with a compile time length + /// Specialization, default_delete. + template<typename _Tp> + struct default_delete<_Tp[]> + { + void + operator()(_Tp* __ptr) const + { + static_assert(sizeof(_Tp)>0, + "can't delete pointer to incomplete type"); + delete [] __ptr; + } + }; + + /// 20.7.12.2 unique_ptr for single objects. + template <typename _Tp, typename _Tp_Deleter = default_delete<_Tp> > + class unique_ptr + { + typedef std::tuple<_Tp*, _Tp_Deleter> __tuple_type; + typedef __tuple_type unique_ptr::* __unspecified_bool_type; + typedef _Tp* unique_ptr::* __unspecified_pointer_type; + + public: + typedef _Tp* pointer; + typedef _Tp element_type; + typedef _Tp_Deleter deleter_type; + + // Constructors. + unique_ptr() + : _M_t(pointer(), deleter_type()) + { static_assert(!std::is_pointer<deleter_type>::value, + "constructed with null function pointer deleter"); } + + explicit + unique_ptr(pointer __p) + : _M_t(__p, deleter_type()) + { static_assert(!std::is_pointer<deleter_type>::value, + "constructed with null function pointer deleter"); } + + unique_ptr(pointer __p, + typename std::conditional<std::is_reference<deleter_type>::value, + deleter_type, const deleter_type&>::type __d) + : _M_t(__p, __d) { } + + unique_ptr(pointer __p, + typename std::remove_reference<deleter_type>::type&& __d) + : _M_t(std::move(__p), std::move(__d)) + { static_assert(!std::is_reference<deleter_type>::value, + "rvalue deleter bound to reference"); } + + // Move constructors. + unique_ptr(unique_ptr&& __u) + : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { } + + template<typename _Up, typename _Up_Deleter> + unique_ptr(unique_ptr<_Up, _Up_Deleter>&& __u) + : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) + { } + + // Destructor. + ~unique_ptr() { reset(); } + + // Assignment. + unique_ptr& + operator=(unique_ptr&& __u) + { + reset(__u.release()); + get_deleter() = std::move(__u.get_deleter()); + return *this; + } + + template<typename _Up, typename _Up_Deleter> + unique_ptr& + operator=(unique_ptr<_Up, _Up_Deleter>&& __u) + { + reset(__u.release()); + get_deleter() = std::move(__u.get_deleter()); + return *this; + } + + unique_ptr& + operator=(__unspecified_pointer_type) + { + reset(); + return *this; + } + + // Observers. + typename std::add_lvalue_reference<element_type>::type operator*() const + { + _GLIBCXX_DEBUG_ASSERT(get() != 0); + return *get(); + } + + pointer + operator->() const + { + _GLIBCXX_DEBUG_ASSERT(get() != 0); + return get(); + } + + pointer + get() const + { return std::get<0>(_M_t); } + + typename std::add_lvalue_reference<deleter_type>::type + get_deleter() + { return std::get<1>(_M_t); } + + typename std::add_lvalue_reference< + typename std::add_const<deleter_type>::type + >::type + get_deleter() const + { return std::get<1>(_M_t); } + + operator __unspecified_bool_type () const + { return get() == 0 ? 0 : &unique_ptr::_M_t; } + + // Modifiers. + pointer + release() + { + pointer __p = get(); + std::get<0>(_M_t) = 0; + return __p; + } + + void + reset(pointer __p = pointer()) + { + if (__p != get()) + { + get_deleter()(get()); + std::get<0>(_M_t) = __p; + } + } + + void + swap(unique_ptr&& __u) + { + using std::swap; + swap(_M_t, __u._M_t); + } + + private: + // Disable copy from lvalue. + unique_ptr(const unique_ptr&); + + template<typename _Up, typename _Up_Deleter> + unique_ptr(const unique_ptr<_Up, _Up_Deleter>&); + + unique_ptr& operator=(const unique_ptr&); + + template<typename _Up, typename _Up_Deleter> + unique_ptr& operator=(const unique_ptr<_Up, _Up_Deleter>&); + + private: + __tuple_type _M_t; + }; + + /// 20.7.12.3 unique_ptr for array objects with a runtime length + // [unique.ptr.runtime] + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // DR 740 - omit specialization for array objects with a compile time length + template<typename _Tp, typename _Tp_Deleter> + class unique_ptr<_Tp[], _Tp_Deleter> + { + typedef std::tuple<_Tp*, _Tp_Deleter> __tuple_type; + typedef __tuple_type unique_ptr::* __unspecified_bool_type; + typedef _Tp* unique_ptr::* __unspecified_pointer_type; + + public: + typedef _Tp* pointer; + typedef _Tp element_type; + typedef _Tp_Deleter deleter_type; + + // Constructors. + unique_ptr() + : _M_t(pointer(), deleter_type()) + { static_assert(!std::is_pointer<deleter_type>::value, + "constructed with null function pointer deleter"); } + + explicit + unique_ptr(pointer __p) + : _M_t(__p, deleter_type()) + { static_assert(!std::is_pointer<deleter_type>::value, + "constructed with null function pointer deleter"); } + + unique_ptr(pointer __p, + typename std::conditional<std::is_reference<deleter_type>::value, + deleter_type, const deleter_type&>::type __d) + : _M_t(__p, __d) { } + + unique_ptr(pointer __p, + typename std::remove_reference<deleter_type>::type && __d) + : _M_t(std::move(__p), std::move(__d)) + { static_assert(!std::is_reference<deleter_type>::value, + "rvalue deleter bound to reference"); } + + // Move constructors. + unique_ptr(unique_ptr&& __u) + : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { } + + template<typename _Up, typename _Up_Deleter> + unique_ptr(unique_ptr<_Up, _Up_Deleter>&& __u) + : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) + { } + + // Destructor. + ~unique_ptr() { reset(); } + + // Assignment. + unique_ptr& + operator=(unique_ptr&& __u) + { + reset(__u.release()); + get_deleter() = std::move(__u.get_deleter()); + return *this; + } + + template<typename _Up, typename _Up_Deleter> + unique_ptr& + operator=(unique_ptr<_Up, _Up_Deleter>&& __u) + { + reset(__u.release()); + get_deleter() = std::move(__u.get_deleter()); + return *this; + } + + unique_ptr& + operator=(__unspecified_pointer_type) + { + reset(); + return *this; + } + + // Observers. + typename std::add_lvalue_reference<element_type>::type + operator[](size_t __i) const + { + _GLIBCXX_DEBUG_ASSERT(get() != 0); + return get()[__i]; + } + + pointer + get() const + { return std::get<0>(_M_t); } + + typename std::add_lvalue_reference<deleter_type>::type + get_deleter() + { return std::get<1>(_M_t); } + + typename std::add_lvalue_reference< + typename std::add_const<deleter_type>::type + >::type + get_deleter() const + { return std::get<1>(_M_t); } + + operator __unspecified_bool_type () const + { return get() == 0 ? 0 : &unique_ptr::_M_t; } + + // Modifiers. + pointer + release() + { + pointer __p = get(); + std::get<0>(_M_t) = 0; + return __p; + } + + void + reset(pointer __p = pointer()) + { + if (__p != get()) + { + get_deleter()(get()); + std::get<0>(_M_t) = __p; + } + } + + private: + // DR 821. + template<typename _Up> + void reset(_Up); + + public: + void + swap(unique_ptr&& __u) + { + using std::swap; + swap(_M_t, __u._M_t); + } + + private: + // Disable copy from lvalue. + unique_ptr(const unique_ptr&); + unique_ptr& operator=(const unique_ptr&); + + // Disable construction from convertible pointer types. + // (N2315 - 20.6.5.3.1) + template<typename _Up> + unique_ptr(_Up*, typename + std::conditional<std::is_reference<deleter_type>::value, + deleter_type, const deleter_type&>::type, + typename std::enable_if<std::is_convertible<_Up*, + pointer>::value>::type* = 0); + + template<typename _Up> + unique_ptr(_Up*, typename std::remove_reference<deleter_type>::type&&, + typename std::enable_if<std::is_convertible<_Up*, + pointer>::value>::type* = 0); + + template<typename _Up> + explicit + unique_ptr(_Up*, typename std::enable_if<std::is_convertible<_Up*, + pointer>::value>::type* = 0); + + private: + __tuple_type _M_t; + }; + + template<typename _Tp, typename _Tp_Deleter> + inline void + swap(unique_ptr<_Tp, _Tp_Deleter>& __x, + unique_ptr<_Tp, _Tp_Deleter>& __y) + { __x.swap(__y); } + + template<typename _Tp, typename _Tp_Deleter> + inline void + swap(unique_ptr<_Tp, _Tp_Deleter>&& __x, + unique_ptr<_Tp, _Tp_Deleter>& __y) + { __x.swap(__y); } + + template<typename _Tp, typename _Tp_Deleter> + inline void + swap(unique_ptr<_Tp, _Tp_Deleter>& __x, + unique_ptr<_Tp, _Tp_Deleter>&& __y) + { __x.swap(__y); } + + template<typename _Tp, typename _Tp_Deleter, + typename _Up, typename _Up_Deleter> + inline bool + operator==(const unique_ptr<_Tp, _Tp_Deleter>& __x, + const unique_ptr<_Up, _Up_Deleter>& __y) + { return __x.get() == __y.get(); } + + template<typename _Tp, typename _Tp_Deleter, + typename _Up, typename _Up_Deleter> + inline bool + operator!=(const unique_ptr<_Tp, _Tp_Deleter>& __x, + const unique_ptr<_Up, _Up_Deleter>& __y) + { return !(__x.get() == __y.get()); } + + template<typename _Tp, typename _Tp_Deleter, + typename _Up, typename _Up_Deleter> + inline bool + operator<(const unique_ptr<_Tp, _Tp_Deleter>& __x, + const unique_ptr<_Up, _Up_Deleter>& __y) + { return __x.get() < __y.get(); } + + template<typename _Tp, typename _Tp_Deleter, + typename _Up, typename _Up_Deleter> + inline bool + operator<=(const unique_ptr<_Tp, _Tp_Deleter>& __x, + const unique_ptr<_Up, _Up_Deleter>& __y) + { return !(__y.get() < __x.get()); } + + template<typename _Tp, typename _Tp_Deleter, + typename _Up, typename _Up_Deleter> + inline bool + operator>(const unique_ptr<_Tp, _Tp_Deleter>& __x, + const unique_ptr<_Up, _Up_Deleter>& __y) + { return __y.get() < __x.get(); } + + template<typename _Tp, typename _Tp_Deleter, + typename _Up, typename _Up_Deleter> + inline bool + operator>=(const unique_ptr<_Tp, _Tp_Deleter>& __x, + const unique_ptr<_Up, _Up_Deleter>& __y) + { return !(__x.get() < __y.get()); } + + // @} group pointer_abstractions + +_GLIBCXX_END_NAMESPACE + +#endif /* _UNIQUE_PTR_H */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |