Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef __JAUS_CORE_ADDRESS_H
00043 #define __JAUS_CORE_ADDRESS_H
00044
00045 #include "jaus/core/types.h"
00046 #include <string>
00047 #include <set>
00048 #include <vector>
00049 #include <iostream>
00050 #include <sstream>
00051 #include <cstdio>
00052
00053 namespace JAUS
00054 {
00064 class JAUS_CORE_DLL Address
00065 {
00066 public:
00067 typedef std::set<Address> Set;
00068 typedef std::vector<Address> List;
00069 static const UShort GlobalBroadcast = 0xFFFF;
00070 static const Byte LocalBroadcast = 0xFF;
00071 Address(const UShort subsystem = 0,
00072 const Byte node = 0,
00073 const Byte component = 0) : mSubsystem(subsystem),
00074 mNode(node),
00075 mComponent(component)
00076 {
00077 }
00078 Address(const Address &id) : mSubsystem(id.mSubsystem),
00079 mNode(id.mNode),
00080 mComponent(id.mComponent)
00081 {
00082
00083 }
00084 Address(const UInt id)
00085 {
00086 *this = id;
00087 }
00088 ~Address() {}
00089 inline void Clear()
00090 {
00091 mSubsystem = mNode = mComponent = 0;
00092 }
00093 inline void Print() const
00094 {
00095 std::cout << ToString() << std::endl;
00096 }
00097 std::string ToString() const
00098 {
00099 std::stringstream str;
00100 str << mSubsystem << "." << (int)mNode << "." << (int)mComponent;
00101 return str.str();
00102 }
00103 static Address FromString(const std::string& str)
00104 {
00105 int s, n, c;
00106 s = n = c = 0;
00107 sscanf(str.c_str(), "%d.%d.%d", &s, &n, &c);
00108 return Address((Byte)s, (Byte)n, (Byte)c);
00109 }
00110 inline operator UInt() const
00111 {
00112 return (UInt)( ( mSubsystem << 24 ) |
00113 ( mNode << 8 ) |
00114 ( mComponent ) );
00115 }
00116 inline bool IsValid() const
00117 {
00118 if ( mSubsystem == 0 ||
00119 mNode == 0 ||
00120 mComponent == 0 )
00121 {
00122 return false;
00123 }
00124 return true;
00125 }
00126
00127 inline bool IsBroadcast() const
00128 {
00129 if( mSubsystem == GlobalBroadcast ||
00130 mNode == LocalBroadcast ||
00131 mComponent == LocalBroadcast )
00132 {
00133 return true;
00134 }
00135 return false;
00136 }
00137 inline UInt ToUInt() const
00138 {
00139 return (UInt)( ( mSubsystem << 16 ) |
00140 ( mNode << 8 ) |
00141 ( mComponent ) );
00142 }
00143
00144 static inline bool DestinationMatch(const Address& dest, const Address id)
00145 {
00146 if( (dest.mSubsystem == GlobalBroadcast || dest.mSubsystem == id.mSubsystem) &&
00147 (dest.mNode == LocalBroadcast || dest.mNode == id.mNode) &&
00148 (dest.mComponent == LocalBroadcast || dest.mComponent == id.mComponent))
00149 {
00150 return true;
00151 }
00152
00153 return false;
00154 }
00173 inline static bool SetCompare(const Set& prev,
00174 const Set& current,
00175 Set& newData,
00176 Set& lostData,
00177 const bool clear = true)
00178 {
00179 bool result = false;
00180 if(clear)
00181 {
00182 lostData.clear();
00183 newData.clear();
00184 }
00185 Address::Set::const_iterator id1, id2;
00186 for(id1 = current.begin();
00187 id1 != current.end();
00188 id1++)
00189 {
00190 id2 = prev.find(*id1);
00191 if(id2 == prev.end())
00192 {
00193
00194 newData.insert(*id1);
00195 result = true;
00196 }
00197 }
00198 for(id1 = prev.begin();
00199 id1 != prev.end();
00200 id1++)
00201 {
00202 id2 = current.find(*id1);
00203 if(id2 == current.end())
00204 {
00205
00206 lostData.insert(*id1);
00207 result = true;
00208 }
00209 }
00210 return result;
00211 }
00212 inline static Address::List ToList(const Address::Set& s)
00213 {
00214 Address::List list;
00215 Address::Set::const_iterator i;
00216 for(i = s.begin();
00217 i != s.end();
00218 i++)
00219 {
00220 list.push_back(*i);
00221 }
00222 return list;
00223 }
00224 inline static Address::Set ToSet(const Address::List& list)
00225 {
00226 Address::Set s;
00227 Address::List::const_iterator i;
00228 for(i = list.begin();
00229 i != list.end();
00230 i++)
00231 {
00232 s.insert(*i);
00233 }
00234 return s;
00235 }
00236 inline bool operator==(const Address &id) const
00237 {
00238 if( mSubsystem == id.mSubsystem &&
00239 mNode == id.mNode &&
00240 mComponent == id.mComponent)
00241 {
00242 return true;
00243 }
00244 return false;
00245 }
00246 inline bool operator==(const UInt id) const
00247 {
00248 return id == this->ToUInt();
00249 }
00250 inline bool operator!=(const Address &id) const
00251 {
00252 if( mSubsystem != id.mSubsystem ||
00253 mNode != id.mNode ||
00254 mComponent != id.mComponent )
00255 {
00256 return true;
00257 }
00258 return false;
00259 }
00260 inline bool operator<(const Address &id) const
00261 {
00262 return ToUInt() < id.ToUInt();
00263 }
00264 inline bool operator<=(const Address &id) const
00265 {
00266 return ToUInt() <= id.ToUInt();
00267 }
00268 inline bool operator>(const Address &id) const
00269 {
00270 return ToUInt() > id.ToUInt();
00271 }
00272 inline bool operator>=(const Address &id) const
00273 {
00274 return ToUInt() >= id.ToUInt();
00275 }
00276 Address &operator()(const UShort subsystem, const Byte node, const Byte component)
00277 {
00278 mSubsystem = subsystem;
00279 mNode = node;
00280 mComponent = component;
00281 return *this;
00282 }
00283 inline Address &operator=(const Address &id)
00284 {
00285 mSubsystem = id.mSubsystem;
00286 mNode = id.mNode;
00287 mComponent = id.mComponent;
00288 return *this;
00289 }
00290 inline Address &operator=(const UInt id)
00291 {
00292 mSubsystem = (UShort)((id & 0xFFFF0000) >> 16);
00293 mNode = (Byte)((id & 0x00F00FF00) >> 8);
00294 mComponent = (Byte)((id & 0x000000FF));
00295 return *this;
00296 }
00297 static inline bool IsReservedComponentID(const Byte value)
00298 {
00299
00300
00301 if(value >= 32 && value <= 35 ||
00302 value >= 37 && value <= 38 ||
00303 value >= 40 && value <= 59)
00304 {
00305 return true;
00306 }
00307 return false;
00308 }
00309 UShort mSubsystem;
00310 Byte mNode;
00311 Byte mComponent;
00312 };
00313
00314 }
00315
00316 #endif
00317
00318