[Assorted-commits] SF.net SVN: assorted:[1221] cpp-commons/trunk/src/commons
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-02-22 07:46:02
|
Revision: 1221 http://assorted.svn.sourceforge.net/assorted/?rev=1221&view=rev Author: yangzhang Date: 2009-02-22 07:45:59 +0000 (Sun, 22 Feb 2009) Log Message: ----------- added yonat C++ tools Added Paths: ----------- cpp-commons/trunk/src/commons/yonat/ cpp-commons/trunk/src/commons/yonat/pointainer.h cpp-commons/trunk/src/commons/yonat/pointerator.h cpp-commons/trunk/src/commons/yonat/stringizer.h Added: cpp-commons/trunk/src/commons/yonat/pointainer.h =================================================================== --- cpp-commons/trunk/src/commons/yonat/pointainer.h (rev 0) +++ cpp-commons/trunk/src/commons/yonat/pointainer.h 2009-02-22 07:45:59 UTC (rev 1221) @@ -0,0 +1,129 @@ +/* + * pointainer - auto-cleaning container of pointers + * + * Example usage: + * { + * pointainer< std::vector<int*> > v; + * // v can be manipulated like any std::vector<int*>. + * + * v.push_back(new int(42)); + * v.push_back(new int(17)); + * // v now owns the allocated int-s + * + * v.erase(v.begin()); + * // frees the memory allocated for the int 42, and then removes the + * // first element of v. + * } + * // v's destructor is called, and it frees the memory allocated for + * // the int 17. + * + * Notes: + * 1. Assumes all elements are unique (you don't have two elements + * pointing to the same object, otherwise you might delete it twice). + * 2. Not usable with pair associative containers (map and multimap). + * 3. For ANSI-challenged compilers, you may want to #define + * NO_MEMBER_TEMPLATES. + * + * Written 10-Jan-1999 by Yonat Sharon <yo...@oo...> + * Last updated 07-Feb-1999 + */ + +#ifndef POINTAINER_H +#define POINTAINER_H + +#ifdef NO_MEMBER_TEMPLATES +#include <functional> // for binder2nd +#endif + +template <typename Cnt> +class pointainer : public Cnt +{ +public: + using typename Cnt::size_type; + using typename Cnt::difference_type; + using typename Cnt::reference; + using typename Cnt::const_reference; + using typename Cnt::value_type; + using typename Cnt::iterator; + using typename Cnt::const_iterator; + using typename Cnt::reverse_iterator; + using typename Cnt::const_reverse_iterator; + typedef pointainer<Cnt> its_type; + + pointainer() {} + pointainer(const Cnt& c) : Cnt(c) {} + its_type& operator=(const Cnt& c) {Cnt::operator=(c); return *this;} + ~pointainer() {clean_all();} + + void clear() {clean_all(); Cnt::clear();} + iterator erase(iterator i) {clean(i); return Cnt::erase(i);} + iterator erase(iterator f, iterator l) {clean(f,l); return Cnt::erase(f,l);} + + // for associative containers: erase() a value + size_type erase(const value_type& v) + { + iterator i = find(v); + size_type found(i != end()); // can't have more than 1 + if (found) + erase(i); + return found; + } + + // for sequence containers: pop_front(), pop_back(), resize() and assign() + void pop_front() {clean(begin()); Cnt::pop_front();} + void pop_back() {iterator i(end()); clean(--i); Cnt::pop_back();} + void resize(size_type s, value_type c = value_type()) + { + if (s < size()) + clean(begin()+s, end()); + Cnt::resize(s, c); + } +#ifndef NO_MEMBER_TEMPLATES + template <class InIter> void assign(InIter f, InIter l) +#else + void assign(iterator f, iterator l) +#endif + { + clean_all(); + Cnt::assign(f,l); + } +#ifndef NO_MEMBER_TEMPLATES + template <class Size, class T> void assign(Size n, const T& t = T()) +#else + void assign(size_t n, const value_type& t = value_type()) +#endif + { + clean_all(); + Cnt::assign(n,t); + } + + // for std::list: remove() and remove_if() + void remove(const value_type& v) + { + clean( std::find(begin(), end(), v) ); + Cnt::remove(v); + } +#ifndef NO_MEMBER_TEMPLATES + template <class Pred> +#else + typedef std::binder2nd<std::not_equal_to<value_type> > Pred; +#endif + void remove_if(Pred pr) + { + for (iterator i = begin(); i != end(); ++i) + if (pr(*i)) + clean(i); + Cnt::remove_if(pr); + } + +private: + void clean(iterator i) {delete *i;} + void clean(iterator f, iterator l) {while (f != l) clean(f++);} + void clean_all() {clean( begin(), end() );} + + // we can't have two pointainers own the same objects: + pointainer(const its_type&) {} + its_type& operator=(const its_type&) {} +}; + +#endif // POINTAINER_H Added: cpp-commons/trunk/src/commons/yonat/pointerator.h =================================================================== --- cpp-commons/trunk/src/commons/yonat/pointerator.h (rev 0) +++ cpp-commons/trunk/src/commons/yonat/pointerator.h 2009-02-22 07:45:59 UTC (rev 1221) @@ -0,0 +1,117 @@ +/* + * pointerator - iterator to T* that behaves like iterator to T + * + * Example usage: + * + * std::vector<int*> v; + * v.push_back(new int(42)); + * v.push_back(new int(17)); + * + * typedef pointerator<std::vector<int*>::iterator> Iter; + * // Note: if your compiler does not support partial template + * // specialization, you should write: + * // typedef pointerator<std::vector<int*>::iterator, int> Iter; + * + * Iter i = std::find(Iter(v.begin()), Iter(v.end()), 17); + * // finds the second element of v + * + * std::cout << *i.get_iterator() << " points to " << *i; + * // *i is 17 + * + * Note: For ANSI-challenged compilers, you may want to #define + * NO_PARTIAL_SPECIALIZATION. + * + * Written 07-Feb-1999 by Yonat Sharon <yo...@oo...> + */ + +#ifndef POINTERATOR_H +#define POINTERATOR_H + +#ifndef NO_PARTIAL_SPECIALIZATION +#include <iterator> // for iterator_traits +#else +#include <cstddef> // for ptrdiff_t +#endif + +#ifndef NO_PARTIAL_SPECIALIZATION +template <typename Iter> +#else +template <typename Iter, typename Val> +#endif // NO_PARTIAL_SPECIALIZATION +class pointerator +{ +public: + +#ifndef NO_PARTIAL_SPECIALIZATION + typedef pointerator<Iter> its_type; + template <typename T> struct dereference {typedef void type;}; + template <typename T> struct dereference<T*> {typedef T type;}; + typedef typename dereference<typename Iter::value_type>::type Val; + typedef typename std::iterator_traits<Iter>::iterator_category iterator_category; + typedef typename std::iterator_traits<Iter>::difference_type difference_type; +#else + typedef pointerator<Iter,Val> its_type; + typedef ptrdiff_t difference_type; +#endif // NO_PARTIAL_SPECIALIZATION + + typedef Val value_type; + typedef Val& reference; + typedef const Val& const_reference; + typedef Val* pointer; + typedef const Val* const_pointer; + + pointerator() {} + pointerator(Iter i) : itsIter(i) {} + Iter get_iterator() const {return itsIter;} + + reference operator*() const {return **itsIter;} + pointer operator->() const {return *itsIter;} + reference operator[](difference_type n) const {return **itsIter[n];} + + its_type& operator++() {++itsIter; return *this;} + its_type& operator--() {--itsIter; return *this;} + its_type operator++(int) {its_type t(*this); ++itsIter; return t;} + its_type operator--(int) {its_type t(*this); --itsIter; return t;} + its_type& operator+=(difference_type n) {itsIter+=n; return *this;} + its_type& operator-=(difference_type n) {itsIter-=n; return *this;} + its_type operator+(difference_type n) const {return its_type(itsIter+n);} + its_type operator-(difference_type n) const {return its_type(itsIter-n);} + + bool operator==(const its_type& r) const {return itsIter == r.itsIter;} + bool operator!=(const its_type& r) const {return itsIter != r.itsIter;} + bool operator<(const its_type& r) const {return itsIter < r.itsIter;} + +private: + Iter itsIter; +}; + +#ifndef NO_PARTIAL_SPECIALIZATION +# define POINTERATOR pointerator<Iter> +# define TEMPLATE_ARGS template <typename Iter> +#else +# define POINTERATOR pointerator<Iter, T> +# define TEMPLATE_ARGS template <typename Iter, typename T> +#endif + +TEMPLATE_ARGS inline POINTERATOR +operator+(POINTERATOR ::difference_type n, const POINTERATOR& r) +{ + return POINTERATOR(x.get_iterator() - n); +} + +TEMPLATE_ARGS inline POINTERATOR ::difference_type +operator-(const POINTERATOR& l, const POINTERATOR& r) +{ + return l.get_iterator() - r.get_iterator(); +} + +TEMPLATE_ARGS inline POINTERATOR +make_pointerator(Iter it) +{ + return POINTERATOR(it); +} + +#undef POINTERATOR +#undef TEMPLATE_ARGS + +#endif // POINTERATOR_H Added: cpp-commons/trunk/src/commons/yonat/stringizer.h =================================================================== --- cpp-commons/trunk/src/commons/yonat/stringizer.h (rev 0) +++ cpp-commons/trunk/src/commons/yonat/stringizer.h 2009-02-22 07:45:59 UTC (rev 1221) @@ -0,0 +1,79 @@ +/* + * basic_stringizer - a functor that turns an object into a basic_string + * stringize/wstringize - functions that turn an object into a string/wstring + * + * The object you stringize must have the output operator defined + * (operator<< with an ostream argument). + * + * Note: For ANSI-challenged compilers, you may want to #define + * NO_PARTIAL_SPECIALIZATION. + * + * // example: creating a string from an int + * std::string s = "The answer, my friend, is " + stringize(42); + * + * // example: transforming a vector of doubles to a vector of strings + * std::vector<double> vf; + * std::vector<std::string> vs; + * vf.push_back(3.14159); + * vf.push_back(2.71828); + * vf.push_back(1.57079); + * std::transform( + * vf.begin(), vf.end(), // from + * std::back_inserter(vs), // to + * stringizer() ); // transformation + */ + +#ifndef STRINGIZER_H +#define STRINGIZER_H + +#include <sstream> + +template <typename Char> +struct basic_stringizer +{ + typedef std::basic_string<Char> String; + + template <typename T> + String operator()(const T& t) + { + std::basic_stringstream<Char> s; + s << t; + return s.str(); + } + +#ifndef NO_PARTIAL_SPECIALIZATION + template <> + String operator()<Char*>(const Char* s) + { return s; } + + template <> + String operator()<String>(const String& s) + { return s; } + + template <typename T> + String operator()<T*>(const T* p) + { return operator()<void*>(p); } + + template <> + String operator()<void*>(const void* p) + { + std::basic_stringstream<Char> s; + s << const_cast<void*>(p); + return s.str(); + } +#endif // NO_PARTIAL_SPECIALIZATION +}; + + +typedef basic_stringizer<char> stringizer; +typedef basic_stringizer<wchar_t> wstringizer; + +template <typename T> +inline std::string stringize(T t) +{ return stringizer()(t); } + +template <typename T> +inline std::wstring wstringize(T t) +{ return wstringizer()(t); } + +#endif // STRINGIZER_H This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |