00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2009 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 #ifndef metaprogramming_H 00029 #define metaprogramming_H 00030 00031 #include <mrpt/utils/CSerializable.h> 00032 00033 namespace mrpt 00034 { 00035 namespace utils 00036 { 00037 /** A set of utility objects for metaprogramming with STL algorithms. 00038 */ 00039 namespace metaprogramming 00040 { 00041 /** An object for deleting pointers (intended for STL algorithms) */ 00042 struct ObjectDelete { 00043 template<typename T> 00044 void operator()(const T *ptr) { 00045 delete ptr; 00046 } 00047 }; 00048 00049 /** An object for clearing an object (invokes its method "->clear()") given a pointer or smart-pointer, intended for being used in STL algorithms. */ 00050 struct ObjectClear { 00051 template<typename T> 00052 void operator()(T ptr) { 00053 ptr->clear(); 00054 } 00055 }; 00056 00057 /** An object for clearing an object (invokes its method ".clear()") given a pointer or smart-pointer, intended for being used in STL algorithms. */ 00058 struct ObjectClear2 { 00059 template<typename T> 00060 void operator()(T ptr){ 00061 ptr.clear(); 00062 } 00063 }; 00064 00065 /** An object for clearing an object->second (invokes its method "clear()") given a pointer or smart-pointer, intended for being used in STL algorithms. */ 00066 struct ObjectClearSecond { 00067 template<typename T> 00068 void operator()(T obj) { 00069 obj.second.clear(); 00070 } 00071 }; 00072 00073 /** An object for transforming between types/classes, intended for being used in STL algorithms. 00074 * Example of usage: 00075 * \code 00076 * vector_int v1(10); // Input 00077 * vector_double v2(10); // Output 00078 * std::transform(v1.begin(),v1.end(), v2.begin(), ObjectConvert<double> ); 00079 * \endcode 00080 */ 00081 template <typename TARGET_TYPE> 00082 struct ObjectConvert { 00083 template<typename T> 00084 TARGET_TYPE operator()(const T &val) { 00085 return TARGET_TYPE(val); 00086 } 00087 }; 00088 00089 /** An object for making smart pointers unique (ie, making copies if necessary), intended for being used in STL algorithms. */ 00090 struct ObjectMakeUnique { 00091 void operator()(CSerializablePtr &ptr) { 00092 ptr.make_unique(); 00093 } 00094 }; 00095 00096 /** An object for making smart pointers unique (ie, making copies if necessary), intended for being used in STL algorithms. */ 00097 struct ObjectPairMakeUnique { 00098 template <typename T> 00099 void operator()(T &ptr) { 00100 ptr.first.make_unique(); 00101 ptr.second.make_unique(); 00102 } 00103 }; 00104 00105 /** An object for making smart pointers unique (ie, making copies if necessary), intended for being used in STL algorithms. */ 00106 struct ObjectClearUnique { 00107 void operator()(CSerializablePtr &ptr) 00108 { 00109 ptr.clear_unique(); 00110 } 00111 }; 00112 00113 /** An object for reading objects from a stream, intended for being used in STL algorithms. */ 00114 struct ObjectReadFromStream 00115 { 00116 private: 00117 CStream *m_stream; 00118 public: 00119 ObjectReadFromStream(CStream *stream) : m_stream(stream) { } 00120 00121 // T can be CSerializablePtr, CSerializable, or any other class implementing ">>" 00122 template <typename T> 00123 void operator()(T &obj) 00124 { 00125 (*m_stream) >> obj; 00126 } 00127 }; 00128 00129 /** An object for writing objects to a stream, intended for being used in STL algorithms. */ 00130 struct ObjectWriteToStream 00131 { 00132 private: 00133 CStream *m_stream; 00134 public: 00135 ObjectWriteToStream(CStream *stream) : m_stream(stream) { } 00136 00137 // T can be CSerializablePtr, CSerializable, or any other class implementing "<<" 00138 template <typename T> 00139 void operator()(const T &ptr) 00140 { 00141 (*m_stream) << ptr; 00142 } 00143 }; 00144 00145 /** Behaves like std::copy but allows the source and target iterators to be of different types through static typecasting. 00146 * \note As in std::copy, the target iterator must point to the first "slot" where to put the first transformed element, and sufficient space must be allocated in advance. 00147 * \sa copy_container_typecasting 00148 */ 00149 template<typename it_src, typename it_dst> 00150 void copy_typecasting(it_src first, it_src last,it_dst target) 00151 { 00152 for (it_src i=first; i!=last ; ++i,++target) 00153 *target = static_cast<typename it_src::value_type>(*i); 00154 } 00155 00156 /** Copy all the elements in a container (vector, deque, list) into a different one performing the appropriate typecasting. 00157 * The target container is automatically resized to the appropriate size, and previous contents are lost. 00158 * This can be used to assign std::vector's of different types: 00159 * \code 00160 * std::vector<int> vi(10); 00161 * std::vector<float> vf; 00162 * vf = vi; // Compiler error 00163 * mrpt::utils::metaprogramming::copy_container_typecasting(v1,vf); // Ok 00164 * \endcode 00165 */ 00166 template<typename src_container, typename dst_container> 00167 void copy_container_typecasting(const src_container &src, dst_container &trg) 00168 { 00169 trg.resize( src.size() ); 00170 copy_typecasting(src.begin(),src.end(),trg.begin()); 00171 } 00172 00173 00174 } // end metaprogramming 00175 } // End of namespace 00176 } // end of namespace 00177 #endif
Page generated by Doxygen 1.5.9 for MRPT 0.7.1 SVN: at Mon Aug 17 22:32:05 EDT 2009 |