Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

PacketRegistry.ih

Go to the documentation of this file.
00001 // $Id: PacketRegistry.ih 1758 2011-01-10 16:58:02Z tho $
00002 //
00003 // Copyright (C) 2006
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 #ifndef IH_SENF_Packets_PacketRegistry_
00027 #define IH_SENF_Packets_PacketRegistry_ 1
00028 
00029 // Custom includes
00030 #include <limits>
00031 #include <boost/multi_index_container.hpp>
00032 #include <boost/multi_index/ordered_index.hpp>
00033 #include <boost/multi_index/composite_key.hpp>
00034 #include <boost/multi_index/member.hpp>
00035 #include <boost/multi_index/mem_fun.hpp>
00036 #include <boost/intrusive_ptr.hpp>
00037 #include <boost/utility.hpp> // for boost::noncopyable
00038 #include <senf/Utils/TypeIdValue.hh>
00039 
00040 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00041 
00042 namespace senf {
00043 namespace detail {
00044 
00045     struct TypeInfoCompare
00046     {
00047         bool operator()(std::type_info const & a, std::type_info const & b) const
00048             { return a.before(b); }
00049     };
00050 
00055     class PacketRegistryImplBase
00056         : private boost::noncopyable
00057     {
00058     public:
00059         virtual ~PacketRegistryImplBase();
00060 
00061         static void dump(std::ostream & os);
00062         static void clear();
00063 
00064     protected:
00065         typedef std::map<std::string, PacketRegistryImplBase*>  RegistryMap;
00066         static RegistryMap & registries();
00067 
00068     private:
00069         virtual bool v_empty() const = 0;
00070         virtual void v_dump(std::ostream & os) const = 0;
00071         virtual void v_clear() = 0;
00072     };
00073 
00078     template <class KeyType>
00079     class PacketRegistryImpl
00080         : public PacketRegistryImplBase
00081     {
00082     public:
00083         typedef KeyType key_t;
00084 
00085         struct Entry : public intrusive_refcount
00086         {
00087             typedef boost::intrusive_ptr<Entry> ptr;
00088 
00089             Entry(KeyType const & key_, int priority_);
00090             virtual ~Entry();
00091 
00092             virtual Packet::factory_t factory() const = 0;
00093 
00094             virtual std::string name() const = 0;
00095             virtual std::type_info const & type() const = 0;
00096 
00097             KeyType key;
00098             int priority;
00099         };
00100 
00101     private:
00102         struct ByKey {};
00103         struct ByType {};
00104 
00105         struct RegistryIndices
00106             : public boost::multi_index::indexed_by<
00107                 boost::multi_index::ordered_unique<
00108                     boost::multi_index::tag<ByKey>,
00109                     boost::multi_index::composite_key<
00110                         Entry,
00111                         boost::multi_index::member<Entry,KeyType,&Entry::key>,
00112                         boost::multi_index::member<Entry,int,&Entry::priority> >,
00113                     boost::multi_index::composite_key_compare<
00114                         std::less<KeyType>,
00115                         std::greater<int> > >,
00116                 boost::multi_index::ordered_unique<
00117                     boost::multi_index::tag<ByType>,
00118                     boost::multi_index::mem_fun<Entry const,std::type_info const &,&Entry::type>,
00119                     TypeInfoCompare> >
00120         {};
00121 
00122         typedef boost::multi_index_container<typename Entry::ptr, RegistryIndices> Registry;
00123 
00124         template <class PacketType>
00125         struct EntryImpl : public Entry
00126         {
00127             EntryImpl(KeyType const & key, int priority);
00128 
00129             virtual Packet::factory_t factory() const;
00130             virtual std::string name() const;
00131             virtual std::type_info const & type() const;
00132         };
00133 
00134     public:
00135         //-/////////////////////////////////////////////////////////////////////////////////////////
00136         // Types
00137 
00138         typedef typename Registry::template index<ByKey>::type::const_iterator iterator;
00139 
00140         //-/////////////////////////////////////////////////////////////////////////////////////////
00142         //\{
00143 
00144         PacketRegistryImpl(std::string const & name);
00145 
00146         //\}
00147         //-/////////////////////////////////////////////////////////////////////////////////////////
00148 
00149         template <class PacketType>
00150         void registerPacket(key_t key, int priority=0);
00151 
00152         template <class PacketType>
00153         void unregisterPacket();
00154         void unregisterPacket(key_t key, int priority=0);
00155 
00156         key_t key(senf::TypeIdValue const & type);
00157         boost::optional<key_t> key(senf::TypeIdValue const & type, bool);
00158 
00159         Entry const & lookup(key_t key);
00160         Entry const * lookup(key_t key, bool);
00161 
00162         iterator begin() const;
00163         iterator end() const;
00164 
00165     protected:
00166 
00167     private:
00168         virtual bool v_empty() const;
00169         virtual void v_dump(std::ostream & os) const;
00170         virtual void v_clear();
00171 
00172         Registry registry_;
00173     };
00174 
00175     template <class KeyType, bool is_integral=std::numeric_limits<KeyType>::is_integer>
00176     struct DumpKey
00177     {
00178         static void dump(KeyType const & v, std::ostream & os);
00179     };
00180 
00181     template <class KeyType>
00182     struct DumpKey<KeyType, true>
00183     {
00184         static void dump(KeyType const & v, std::ostream & os);
00185     };
00186 
00187 }}
00188 
00189 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00190 #endif
00191 
00192 
00193 // Local Variables:
00194 // mode: c++
00195 // fill-column: 100
00196 // c-file-style: "senf"
00197 // indent-tabs-mode: nil
00198 // ispell-local-dictionary: "american"
00199 // compile-command: "scons -u test"
00200 // comment-column: 40
00201 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research