Sirikata
libcore/include/sirikata/core/xdp/Defs.hpp
Go to the documentation of this file.
00001 // Copyright (c) 2011 Sirikata Authors. All rights reserved.
00002 // Use of this source code is governed by a BSD-style license that can
00003 // be found in the LICENSE file.
00004 
00005 #ifndef _SIRIKATA_LIBCORE_XDP_DEFS_HPP_
00006 #define _SIRIKATA_LIBCORE_XDP_DEFS_HPP_
00007 
00008 #include <sirikata/core/util/Platform.hpp>
00009 #include <sirikata/core/util/SpaceID.hpp>
00010 
00011 namespace Sirikata {
00016 namespace XDP {
00017 
00018 class PortID;
00019 template<typename IdentifierType>
00020 class Endpoint;
00021 
00028 class SIRIKATA_EXPORT PortID {
00029 public:
00030     PortID();
00031     PortID(uint32 rhs);
00032     PortID(const PortID& rhs);
00033 
00035     static const PortID& null();
00037     static const PortID& any();
00038 
00039     PortID& operator=(const PortID& rhs);
00040     PortID& operator=(uint32 rhs);
00041 
00042     operator uint32() const;
00043 
00044     bool operator==(const PortID& rhs) const;
00045     bool operator!=(const PortID& rhs) const;
00046     bool operator>(const PortID& rhs) const;
00047     bool operator>=(const PortID& rhs) const;
00048     bool operator<(const PortID& rhs) const;
00049     bool operator<=(const PortID& rhs) const;
00050 
00051     PortID& operator++ ();
00052     PortID operator++ (int);
00053 
00057     bool matches(const PortID& rhs) const;
00058 
00059     class Hasher {
00060     public:
00061         size_t operator()(const PortID& p) const {
00062             return std::tr1::hash<uint32>()(p.mValue);
00063         }
00064     };
00065 
00066 private:
00067     uint32 mValue;
00068 };
00069 
00074 // We can't actually define this here since it depends on the IdentifierType
00075 //typedef std::tr1::function<void(const Endpoint& src, const Endpoint& dst, MemoryReference)> MessageHandler;
00076 
00086 template<typename IdentifierType>
00087 class Endpoint {
00088 public:
00089     typedef IdentifierType Identifier;
00090 
00091     Endpoint(const SpaceID& space, const Identifier& id, const PortID& port)
00092      : mSpace(space),
00093        mID(id),
00094        mPort(port)
00095     {
00096     }
00097 
00098     bool operator==(const Endpoint& rhs) const {
00099         return (
00100             mSpace == rhs.mSpace &&
00101             mID == rhs.mID &&
00102             mPort == rhs.mPort
00103         );
00104     }
00105     bool operator!=(const Endpoint& rhs) const {
00106         return !(*this == rhs);
00107     }
00108     bool operator>(const Endpoint& rhs) const {
00109         return (
00110             mSpace > rhs.mSpace || (
00111                 mSpace == rhs.mSpace && (
00112                     mID > rhs.mID || (
00113                         mID == rhs.mID && (
00114                             mPort > rhs.mPort
00115                         )
00116                     )
00117                 )
00118             )
00119         );
00120     }
00121     bool operator>=(const Endpoint& rhs) const {
00122         return !(*this < rhs);
00123     }
00124     bool operator<(const Endpoint& rhs) const {
00125         return (
00126             mSpace < rhs.mSpace || (
00127                 mSpace == rhs.mSpace && (
00128                     mID < rhs.mID || (
00129                         mID == rhs.mID && (
00130                             mPort < rhs.mPort
00131                         )
00132                     )
00133                 )
00134             )
00135         );
00136     }
00137     bool operator<=(const Endpoint& rhs) const {
00138         return !(*this > rhs);
00139     }
00140 
00141 
00146     bool matches(const Endpoint& rhs) const {
00147         return (
00148             mSpace.matches(rhs.mSpace) &&
00149             mID.matches(rhs.mID) &&
00150             mPort.matches(rhs.mPort)
00151         );
00152     }
00153 
00154     const SpaceID& space() const { return mSpace; }
00155     const Identifier& id() const { return mID; }
00156     const PortID& port() const { return mPort; }
00157 
00158     String toString() const {
00159         std::ostringstream ss;
00160         ss << (*this);
00161         return ss.str();
00162     }
00163 
00164     class Hasher {
00165     public:
00166         size_t operator()(const Endpoint& p) const {
00167             return (
00168                 SpaceID::Hasher()(p.mSpace) ^
00169                 typename Identifier::Hasher()(p.mID) ^
00170                 PortID::Hasher()(p.mPort)
00171             );
00172         }
00173     };
00174 private:
00175     Endpoint();
00176 
00177     SpaceID mSpace;
00178     Identifier mID;
00179     PortID mPort;
00180 }; // class Endpoint
00181 
00182 template<typename IdentifierType>
00183 std::ostream& operator<<(std::ostream& os, const Sirikata::XDP::Endpoint<IdentifierType>& ep) {
00184     os << ep.space() << ":" << ep.id() << ":" << ep.port();
00185     return os;
00186 }
00187 
00188 } // namespace ODP
00189 } // namespace Sirikata
00190 
00191 #endif //_SIRIKATA_ODP_DEFS_HPP_