attribute_map_collection.inl

Go to the documentation of this file.
00001 /*
00002     mesh3d
00003     Copyright (C) 2010  Timo Suoranta
00004 
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 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00027 #ifndef MESH3D__ATTRIBUTE_MAP_COLLECTION__INL
00028 #define MESH3D__ATTRIBUTE_MAP_COLLECTION__INL
00029 
00030 #include "mesh3d/exception.h"
00031 
00032 namespace mesh3d {
00033 
00034 namespace detail {
00035     void throw_incompatible_attribute_type(
00036         std::string const   &name,
00037         char const          *type_name, 
00038         char const          *expected_type_name
00039     );
00040 }
00041 
00042 template<typename Key>
00043 inline attribute_map_collection<Key>::attribute_map_collection()
00044 {
00045 }
00046 
00047 template<typename Key>
00048 inline void attribute_map_collection<Key>::clear()
00049 {
00050     m_maps.clear();
00051 }
00052 
00053 
00054 template<typename Key>
00055 inline size_t attribute_map_collection<Key>::size() const
00056 {
00057     return m_maps.size();
00058 }
00059 
00060 template<typename Key>
00061 inline void attribute_map_collection<Key>::insert(std::string const &name,
00062     std::tr1::shared_ptr<attribute_map_base<Key> > const &map)
00063 {
00064     assert(m_maps.find(name) == m_maps.end());
00065 
00066     m_maps.insert(map_map::value_type(name, map));
00067 }
00068 
00069 template<typename Key>
00070 inline void attribute_map_collection<Key>::remove(std::string const &name)
00071 {
00072     m_maps.erase(name);
00073 }
00074 
00075 template<typename Key>
00076 inline bool attribute_map_collection<Key>::has(std::string const &name) const
00077 {
00078     return m_maps.find(name) != m_maps.end();
00079 }
00080 
00081 template<typename Key>
00082 inline std::tr1::shared_ptr<attribute_map_base<Key> >
00083     attribute_map_collection<Key>::find_any(std::string const &name) const
00084 {
00085     const_iterator i = m_maps.find(name);
00086 
00087     if(i == m_maps.end())
00088     {
00089         throw attribute_map_not_found_exception(name);
00090     }
00091 
00092     assert(i != m_maps.end());
00093     return i->second;
00094 }
00095 
00096 template<typename Key>
00097 inline typename attribute_map_collection<Key>::const_iterator
00098     attribute_map_collection<Key>::begin() const
00099 {
00100     return m_maps.begin();
00101 }
00102 
00103 template<typename Key>
00104 inline typename attribute_map_collection<Key>::const_iterator
00105     attribute_map_collection<Key>::end() const
00106 {
00107     return m_maps.end();
00108 }
00109 
00110 template<typename Key>
00111 template<typename Value>
00112 inline std::tr1::shared_ptr<attribute_map<Key, Value> >
00113     attribute_map_collection<Key>::create(std::string const &name)
00114 {
00115     std::tr1::shared_ptr<attribute_map<Key, Value> > p(new attribute_map<Key, Value>);
00116     insert(name, p);
00117     return p;
00118 }
00119 
00120 template<typename Key>
00121 template<typename Value>
00122 inline std::tr1::shared_ptr<attribute_map<Key, Value> >
00123     attribute_map_collection<Key>::find(std::string const &name) const
00124 {
00125     std::tr1::shared_ptr<attribute_map_base<Key> > p = find_any(name);
00126 
00127     if(
00128         std::tr1::shared_ptr<attribute_map<Key, Value> > 
00129             t = std::tr1::dynamic_pointer_cast<attribute_map<Key, Value> >(p)
00130     )
00131     {
00132         return t;
00133     }
00134     else
00135     {
00136         throw incompatible_attribute_type_exception(
00137             name, 
00138             p->value_type_id().name(),
00139             typeid(Value).name()
00140         );
00141 
00142         //return std::tr1::shared_ptr<attribute_map<Key, Value> >();
00143     }
00144 }
00145 
00146 template<typename Key>
00147 template<typename Value>
00148 inline std::tr1::shared_ptr<attribute_map<Key, Value> >
00149     attribute_map_collection<Key>::find_or_create(std::string const &name)
00150 {
00151     map_map::iterator i = m_maps.find(name);
00152     if(i != m_maps.end())
00153     {
00154         if(
00155             std::tr1::shared_ptr<attribute_map<Key, Value> > 
00156                 t = std::tr1::dynamic_pointer_cast<attribute_map<Key, Value> >(i->second)
00157         )
00158         {
00159             return t;
00160         }
00161         else
00162         {
00163             throw incompatible_attribute_type_exception(
00164                 name, 
00165                 t->value_type_id().name(),
00166                 typeid(Value).name()
00167             );
00168 
00169             //return std::tr1::shared_ptr<attribute_map<Key, Value> >();
00170         }
00171     }
00172     else
00173     {
00174         return create<Value>(name);
00175     }
00176 }
00177 
00178 }
00179 
00180 #endif 
00181 
Generated on Sun Apr 11 12:23:08 2010 for RenderStack by  doxygen 1.6.3