searchengine/util/cpixtools/inc/public/cpixmemtools.h
changeset 0 671dee74050a
equal deleted inserted replaced
-1:000000000000 0:671dee74050a
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 /*
       
    18  * cpixmemorytools.h
       
    19  *
       
    20  *  Created on: 23.4.2009
       
    21  *      Author: arau
       
    22  */
       
    23 
       
    24 #ifndef CPIXMEMORYTOOLS_H_
       
    25 #define CPIXMEMORYTOOLS_H_
       
    26 
       
    27 #include <list>
       
    28 #include <vector>
       
    29 #include <memory>
       
    30 
       
    31 namespace Cpt
       
    32 {
       
    33 
       
    34     /**
       
    35      * Stripped down implementation of the std::vector, 
       
    36      * which holds ownership to members and releases the owned instances,
       
    37      * when destroyed. 
       
    38      */
       
    39     template<typename T>
       
    40     class auto_vector : public std::vector<T*> {
       
    41     private: 
       
    42     	// pure copy constructor is not supported.
       
    43     	// copy method will empty the given vector from all members
       
    44 		auto_vector(const auto_vector &vector);
       
    45         auto_vector& operator=(const auto_vector &vector);
       
    46 
       
    47     public: 
       
    48     	// transfers all the objects to 
       
    49         auto_vector(auto_vector &vector);
       
    50   
       
    51         auto_vector& operator=(auto_vector &vector);
       
    52    
       
    53     	auto_vector(); 
       
    54     	
       
    55     	/**
       
    56     	 * Destructor. Destroys all contained members
       
    57     	 */
       
    58     	~auto_vector(); 
       
    59     	
       
    60     	/**
       
    61     	 * Like push_back, except that it transfers ownership throught auto_ptr
       
    62     	 */
       
    63     	void donate_back(std::auto_ptr<T> ptr); 
       
    64     	
       
    65     	/**
       
    66     	 * Like pop_back, except that it transfers ownership throught auto_ptr
       
    67     	 */
       
    68     	std::auto_ptr<T> release_back(); 
       
    69     };
       
    70 
       
    71     template<typename T>
       
    72     class poly_forward_iterator {
       
    73     public: 
       
    74     	virtual ~poly_forward_iterator() {}
       
    75 		virtual T operator++(int) = 0; 
       
    76 		virtual operator bool() = 0;
       
    77     };
       
    78     
       
    79     
       
    80     template<typename T, typename I>
       
    81 	class auto_iterator_ref {
       
    82 	public: 
       
    83 		auto_iterator_ref(I* p) : p_(p) {} 
       
    84 		I* p_;
       
    85 	};
       
    86     
       
    87 	template<typename T, typename I>
       
    88 	class auto_iterator {
       
    89 	public: 
       
    90 		inline explicit auto_iterator(I* iterator) : iterator_(iterator) {}; 
       
    91 		inline ~auto_iterator() 			{}
       
    92 		inline T operator++(int) 			{ return (*iterator_)++; }
       
    93 		inline operator bool()  			{ return *iterator_; } 
       
    94 	
       
    95 		// special conversions enabling returning as return value
       
    96 		inline auto_iterator(auto_iterator_ref<T, I> ref) throw()
       
    97 		  : iterator_(ref.p_) {}
       
    98 		operator auto_iterator_ref<T, I>() throw() {
       
    99 			return auto_iterator_ref<T, I>(iterator_.release());
       
   100 	    }
       
   101 	private: 
       
   102 		std::auto_ptr<I> iterator_;
       
   103 	};
       
   104 	
       
   105 }
       
   106 
       
   107 /**************************************************************
       
   108  *
       
   109  *  IMPLEMENTATION OF TEMPLATES
       
   110  *
       
   111  */
       
   112 namespace Cpt
       
   113 {
       
   114 
       
   115 	template<typename T>
       
   116 	auto_vector<T>::auto_vector() {}
       
   117 	
       
   118 	template<typename T>
       
   119 	auto_vector<T>::~auto_vector() 
       
   120 	{
       
   121 		for (int i = 0; i < this->size(); i++) delete (*this)[i]; 
       
   122 	}
       
   123 	
       
   124 	template<typename T>
       
   125 	auto_vector<T>::auto_vector(auto_vector<T> &vector) 
       
   126 	{
       
   127 		// without using fororder, the order would be inversed
       
   128 		operator=(vector); 
       
   129 	}
       
   130 	
       
   131 	
       
   132 	template<typename T>
       
   133 	auto_vector<T>& auto_vector<T>::operator=(auto_vector<T> &vector) 
       
   134 	{
       
   135 		auto_vector<T> fororder; 
       
   136 		while (!vector.empty()) fororder.donate_back(vector.release_back()); 
       
   137 		while (!fororder.empty()) donate_back(fororder.release_back()); 
       
   138 		return *this;
       
   139 	}
       
   140 	
       
   141 	/**
       
   142 	 * Grants the ownership of the item for this vector
       
   143 	 */
       
   144 	template<typename T>
       
   145 	void auto_vector<T>::donate_back(std::auto_ptr<T> ptr) 
       
   146 	{
       
   147 		this->push_back(ptr.release()); 
       
   148 	}
       
   149 		
       
   150 	/**
       
   151 	 * Releases the ownership of the item
       
   152 	 */
       
   153 	template<typename T>
       
   154 	std::auto_ptr<T> auto_vector<T>::release_back() 
       
   155 	{
       
   156 		T* v = this->back();
       
   157 		std::auto_ptr<T> ret(v); 
       
   158 		this->pop_back(); 
       
   159 		return ret;
       
   160 	}
       
   161 
       
   162 }
       
   163 
       
   164 #endif /* CPIXMEMORYTOOLS_H_ */