Urbi SDK Remote for C++  2.7.5
utable.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007-2010, Gostai S.A.S.
00003  *
00004  * This software is provided "as is" without warranty of any kind,
00005  * either expressed or implied, including but not limited to the
00006  * implied warranties of fitness for a particular purpose.
00007  *
00008  * See the LICENSE file for more information.
00009  */
00010 
00012 
00013 #include <libport/foreach.hh>
00014 
00015 #include <urbi/ucallbacks.hh>
00016 #include <urbi/utable.hh>
00017 #include <urbi/uvar.hh>
00018 
00019 // Lists and hashtables used.
00020 
00021 namespace urbi
00022 {
00023 
00024   namespace
00025   {
00026     // FIXME: Use libport::find0 which first needs to be educated to
00027     // support the case where the mapped_type is not a pointer type.
00028     template <typename Container>
00029     inline
00030     typename Container::mapped_type*
00031     find0(Container& c, const typename Container::key_type& k)
00032     {
00033       typename Container::iterator i = c.find(k);
00034       if (i != c.end())
00035         return &i->second;
00036       else
00037         return 0;
00038     }
00039   }
00040 
00041 
00042   /*---------.
00043   | UTable.  |
00044   `---------*/
00045 
00046   UTable::UTable()
00047   {
00048   }
00049 
00050   UTable::mapped_type*
00051   UTable::find0(const std::string& name)
00052   {
00053     return urbi::find0(*this, name);
00054   }
00055 
00058   void
00059   UTable::clean(const std::string& name)
00060   {
00061     std::list<iterator> todelete;
00062     for (UTable::iterator i = begin(); i != end(); ++i)
00063     {
00064       mapped_type& cs = i->second;
00065       for (mapped_type::iterator j = cs.begin(); j != cs.end(); ++j)
00066       {
00067         if ((*j)->objname == name)
00068         {
00069           delete *j;
00070           cs.erase(j);
00071         }
00072       }
00073 
00074       if (cs.empty())
00075         todelete.push_back(i);
00076     }
00077 
00078     foreach (iterator& i, todelete)
00079       erase(i);
00080   }
00081 
00082 
00083   /*------------.
00084   | UVarTable.  |
00085   `------------*/
00086 
00087   UVarTable::mapped_type*
00088   UVarTable::find0(const std::string& name)
00089   {
00090     return urbi::find0(*this, name);
00091   }
00092 
00095   void
00096   UVarTable::clean(const UVar& uvar)
00097   {
00098     iterator i = find(uvar.get_name());
00099     if (i != end())
00100     {
00101       for (mapped_type::iterator j = i->second.begin();
00102            j != i->second.end();)
00103         if (*j == &uvar)
00104           j = i->second.erase(j);
00105         else
00106           ++j;
00107 
00108       if (i->second.empty())
00109         erase(i);
00110     }
00111   }
00112 
00113 } // namespace urbi