| VRS - The Virtual Rendering System |
| version 3.3 |
00001 /********************************************************************** 00002 VRS - The Virtual Rendering System 00003 Copyright (C) 2001 Computer Graphics Systems Group at the 00004 Hasso-Plattner-Institute, Potsdam, Germany. 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 This library is distributed in the hope that it will be useful, but 00010 WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00012 See the GNU Lesser General Public License for more details. 00013 You should have received a copy of the GNU Lesser+ General Public 00014 License along with this library; if not, write to the FreeSoftware 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA, 02111-1307, USA. 00016 **********************************************************************/ 00017 // $Id: discretemapper.h 4633 2005-01-03 01:43:11Z klimetschek $ 00018 // $Date: 2005-01-03 02:43:11 +0100 (Mon, 03 Jan 2005) $ 00019 // $Revision: 4633 $ 00020 // $State$ 00021 // $Author: klimetschek $ 00022 // 00023 // $Log$ 00024 // Revision 1.19 2005/01/03 01:43:07 klimetschek 00025 // - modified win32 project layout, splitted vrs.vcproj into vrs_container, vrs_core, vrs_sg, vrs_image, vrs_opengl and vrs_io 00026 // - removed vrs.vcproj 00027 // minor things: 00028 // - added all glutexamples to vrs4glut.sln 00029 // - fixed some problems in glutexamples 00030 // - removed all project references from all VS projects (using solution wide project dependencies instead) 00031 // 00032 // Revision 1.18 2004/11/05 13:58:09 basch 00033 // Class comment changed (Doxygen style) 00034 // 00035 // Revision 1.17 2004/03/12 16:28:38 baumann 00036 // macros VRS_NAMESPACE_BEGIN/_END expanded and removed 00037 // 00038 // Revision 1.16 2004/02/12 08:29:07 gerber 00039 // added macro VRS_SERIALIZATION_PARENT_CLASS for parent serialization 00040 // 00041 // Revision 1.15 2004/01/28 15:20:30 neumann 00042 // serialization mechanism: 00043 // - adjusted method serialize for usage with new class SerializationManager 00044 // 00045 // Revision 1.14 2004/01/19 11:43:56 baumann 00046 // serialization mechanism improved 00047 // 00048 // Revision 1.13 2003/11/07 14:19:23 kirsch 00049 // removed leading underscore from include guards 00050 // 00051 // Revision 1.12 2003/02/25 22:00:46 skirsch 00052 // correction and improvement of the comments 00053 // 00054 // Revision 1.11 2002/08/06 16:24:01 baumann 00055 // VRS_CORE_API removed for template classes 00056 // 00057 // Revision 1.10 2002/07/11 14:21:53 kersting 00058 // added version for serialize method 00059 // 00060 // Revision 1.9 2002/04/25 08:12:05 kirsch 00061 // added typename where necessary 00062 // 00063 // Revision 1.8 2002/03/03 21:44:24 kosta 00064 // VRS_TYPEINFO-macro rewritten 00065 // 00066 // Revision 1.7 2002/02/19 10:34:07 kosta 00067 // undone all changes since 2002-02-15 00068 // 00069 // Revision 1.4 2002/02/14 09:37:20 kosta 00070 // automatic serialization registration for template classes implemented 00071 // 00072 // Revision 1.3 2002/02/05 07:48:25 kosta 00073 // new persistency macros: 00074 // VRS_SERIALIZABLE(CLASS_NAME); 00075 // VRS_SERIALIZABLE_ABSTRACT_CLASS(CLASS_NAME); 00076 // VRS_SERIALIZABLE_NO_SO_CLASS(CLASS_NAME) 00077 // 00078 // Revision 1.2 2002/02/04 13:07:19 kosta 00079 // VRS_SERIALIZABLE macros completely rewritten 00080 // 00081 // Revision 1.1 2002/01/27 12:22:44 kosta 00082 // initial version 00083 // 00084 00085 #ifndef VRS_DISCRETEMAPPER_H 00086 #define VRS_DISCRETEMAPPER_H 00087 00088 #include <vrs/mapper.h> 00089 00090 #include <map> 00091 00092 namespace VRS { 00093 00096 template<typename TARGET_TYPE, typename SOURCE_TYPE> 00097 class DiscreteMapper : public Mapper<TARGET_TYPE, SOURCE_TYPE> { 00098 00099 public: 00100 DiscreteMapper() { } 00102 00103 bool add(SOURCE_TYPE source, TARGET_TYPE target); 00109 bool remove(SOURCE_TYPE source) { return (valueMap_.erase(source) > 0); } 00115 virtual TARGET_TYPE map(SOURCE_TYPE source); 00127 VRS_TYPEINFO(DiscreteMapper, VRS_TEMPLATE_ARGS_2(TARGET_TYPE, SOURCE_TYPE, Mapper)); 00128 VRS_SERIALIZABLE(DiscreteMapper); 00129 00130 private: 00131 typedef std::map<SOURCE_TYPE, TARGET_TYPE> ValueMap; 00132 ValueMap valueMap_; 00133 }; 00134 00135 template<typename TARGET_TYPE, typename SOURCE_TYPE> 00136 bool DiscreteMapper<TARGET_TYPE, SOURCE_TYPE>::add(SOURCE_TYPE source, TARGET_TYPE target) { 00137 return valueMap_.insert(ValueMap::value_type(source, target)).second; 00138 } 00139 00140 template<typename TARGET_TYPE, typename SOURCE_TYPE> 00141 TARGET_TYPE DiscreteMapper<TARGET_TYPE, SOURCE_TYPE>::map(SOURCE_TYPE source) { 00142 VRS_Assertion(valueMap_.size() > 0, "no mapping defined"); 00143 00144 // find the first source value greater than the specified one 00145 typename ValueMap::const_iterator iter = valueMap_.upper_bound(source); 00146 if(iter != valueMap_.begin()) { 00147 --iter; 00148 } 00149 00150 // case: (iter_element <= source) || (source < first_element) 00151 return (*iter).second; 00152 } 00153 00154 template<typename TARGET_TYPE, typename SOURCE_TYPE> 00155 void DiscreteMapper<TARGET_TYPE, SOURCE_TYPE>::serialize(SerializationManager& doc) { 00156 UINT8 version = 0; 00157 doc.classVersion(version); 00158 00159 // typedef, because the serialization macro doesn't like templates with 2 template parameter 00160 typedef Mapper<TARGET_TYPE, SOURCE_TYPE> Parent; 00161 VRS_SERIALIZATION_PARENT_CLASS(doc, Parent); 00162 00163 typename ValueMap::size_type size = valueMap_.size(); 00164 serialization(doc, "size", size); 00165 00166 if(doc.isReading()) { 00167 SOURCE_TYPE source; 00168 TARGET_TYPE target; 00169 00170 valueMap_.clear(); 00171 for(typename ValueMap::size_type i = 0; i < size; ++i) { 00172 serialization(doc, "source", source); 00173 serialization(doc, "target", target); 00174 valueMap_.insert(ValueMap::value_type(source, target)); 00175 } 00176 } else { 00177 for(typename ValueMap::const_iterator iter = valueMap_.begin(), iterEnd = valueMap_.end(); iter != iterEnd; ++iter) { 00178 SOURCE_TYPE source = (*iter).first; 00179 TARGET_TYPE target = (*iter).second; 00180 serialization(doc, "source", source); 00181 serialization(doc, "target", target); 00182 } 00183 } 00184 } 00185 00186 VRS_SERIALIZATION_REGISTRATION_TEMPLATE_2(DiscreteMapper) 00187 00188 } // namespace VRS 00189 00190 #endif // VRS_DISCRETEMAPPER_H