Changeset 9695 for source/ariba/utility
- Timestamp:
- Mar 24, 2011, 10:40:14 AM (14 years ago)
- Location:
- source/ariba/utility
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
source/ariba/utility/serialization/Data.hpp
r9684 r9695 351 351 } 352 352 public: 353 DefaultDataModel() {353 finline DefaultDataModel() { 354 354 bufferPtr = NULL; 355 355 bufferLen = -1; -
source/ariba/utility/types/LinkID.cpp
r9684 r9695 39 39 #include "LinkID.h" 40 40 41 #include <boost/unordered_map.hpp>42 43 41 namespace ariba { 44 42 namespace utility { 45 43 46 /// the unspecified link id 47 const LinkID LinkID::UNSPECIFIED; 44 const LinkID LinkID::UNSPECIFIED; // for this link isvalid is always false! 48 45 49 const char* UNSPECIFIED_LINK = "<LINKID-UNSPECIFIED>"; 50 const char* UNKNOWN_LINK = "<LINKID-UNKNOWN>"; 51 const char* NULL_INFO = "<NO-INFO-AVAILABLE>"; 52 53 boost::unordered_map<uint16_t, const char*> link_ids; 54 55 bool LinkID::isValid( const LinkID& id ) { 56 return link_ids.count(id.local_id)!=0; 46 LinkID::LinkID() : isvalid(false) { 57 47 } 58 48 59 const char* LinkID::getInfo( const LinkID& id ) { 60 if (!id.valid()) 61 return UNSPECIFIED_LINK; 62 if ( link_ids.count(id.local_id) == 0 ) 63 return UNKNOWN_LINK; 64 const char* info = link_ids.find( id.local_id )->second; 65 if (info == NULL) 66 return NULL_INFO; 67 return info; 49 LinkID::LinkID(const Identifier& identifier) : Identifier(identifier), isvalid(true) { 68 50 } 69 51 70 /// create a new locally unique link id 71 LinkID LinkID::create( const char* info ) { 72 assert( link_ids.size() != 0xFFFE ); 73 uint16_t id; 74 do { 75 id = rand() & 0xFFFF; 76 } while (id == 0 || link_ids.count(id) != 0); 77 link_ids.insert( std::make_pair(id, info) ); 78 return LinkID(id); 52 LinkID::~LinkID() { 79 53 } 80 54 81 /// free a locally unique link id 82 void LinkID::destroy( const LinkID& id ) { 83 link_ids.erase(id.local_id); 55 LinkID::LinkID(const LinkID& rh) : Identifier(rh) { 84 56 } 85 57 86 std::ostream& operator<<(std::ostream& s, const LinkID& id ) { 87 return s << id.toString(); 58 LinkID& LinkID::operator=(const LinkID& rh){ 59 60 Identifier::operator=( rh ); 61 this->isvalid = rh.isvalid; 62 63 return *this; 64 } 65 66 bool LinkID::valid(){ 67 return isvalid; 68 } 69 70 LinkID LinkID::create() { 71 return LinkID( Identifier::random() ); 88 72 } 89 73 -
source/ariba/utility/types/LinkID.h
r9684 r9695 40 40 #define LINKID_H_ 41 41 42 #include <stdlib.h>43 #include <stdint.h>44 #include <stdio.h>45 46 42 #include <vector> 47 #include <string> 48 49 #include "ariba/utility/serialization.h" 43 #include "ariba/utility/types/Identifier.h" 50 44 51 45 using std::vector; 46 using ariba::utility::Identifier; 52 47 53 48 namespace ariba { … … 55 50 56 51 /** 57 * The link id identifies a link between two devices/nodes. 58 * 59 * Its a 32-bit value, that is composed out of two 16-bit values 60 * identifing a initiator and acceptor id. 52 * Represents a link from the base communication. 61 53 */ 62 class LinkID { 63 private: 64 uint16_t local_id; 65 uint16_t initiator_id; 66 uint16_t acceptor_id; 67 68 69 LinkID(uint16_t local_id) { 70 this->local_id = local_id; 71 this->initiator_id = 0; 72 this->acceptor_id = 0; 73 } 74 75 /// returns the full id 76 inline uint32_t getFullId() const { 77 return (initiator_id << 16) + acceptor_id; 78 } 79 80 inline int compareTo( const LinkID& rhs ) const { 81 // compare local id 82 if (rhs.isLocal() && this->isLocal()) 83 return local_id - rhs.local_id; 84 85 // compare initiator id 86 else if ((initiator_id == 0 || rhs.initiator_id == 0) 87 && (acceptor_id == rhs.acceptor_id) ) return 0; 88 89 // compare acceptor id 90 else if ((acceptor_id == 0 || rhs.acceptor_id == 0) 91 && (initiator_id == rhs.initiator_id) ) return 0; 92 93 // compare full id 94 else if (getFullId() == rhs.getFullId()) return 0; 95 else if (getFullId() < rhs.getFullId()) return -1; 96 else if (getFullId() > rhs.getFullId()) return 1; 97 return -1; 98 } 99 100 static const char* getInfo( const LinkID& id ); 101 102 static bool isValid( const LinkID& id ); 103 54 class LinkID : public Identifier { 104 55 public: 105 /// the unspecified link id106 56 static const LinkID UNSPECIFIED; 107 57 108 /// create a new locally unique link id 109 static LinkID create( const char* info = NULL ); 58 LinkID(); 59 LinkID(const LinkID& rh); 60 LinkID(const Identifier& identifier); 61 virtual ~LinkID(); 62 LinkID& operator=(const LinkID& rh); 110 63 111 /// free a locally unique link id112 static void destroy( const LinkID& id);64 bool valid(); 65 static LinkID create(); 113 66 114 /// construct a unspecified id 115 LinkID() { 116 local_id = 0; 117 initiator_id = 0; 118 acceptor_id = 0; 119 } 120 121 /// copy constructor 122 LinkID( const LinkID& rh ); 123 124 /// assigns another link id 125 LinkID& operator=(const LinkID& rh) { 126 local_id = rh.local_id; 127 initiator_id = rh.initiator_id; 128 acceptor_id = rh.acceptor_id; 129 return *this; 130 } 131 132 /// returns true, if the local link id is known and registered 133 bool isValid() const { 134 return isValid(*this); 135 } 136 137 bool isUnspecified() const { 138 return local_id == 0 && initiator_id == 0 && acceptor_id == 0; 139 } 140 141 /// returns true, if this is a local id only 142 bool isLocal() const { 143 return acceptor_id == 0 && initiator_id == 0; 144 } 145 146 /// returns true, if this is a remote id only 147 bool isRemote() const { 148 return local_id == 0; 149 } 150 151 /// returns true, if this is a full link id (with acceptor/initiator id) 152 bool isLink() const { 153 return acceptor_id != 0 && initiator_id != 0; 154 } 155 156 /// returns the local id 157 uint16_t getLocalId() const { 158 return local_id; 159 } 160 161 /// returns the remote id 162 uint16_t getRemoteId() const { 163 return local_id == initiator_id ? acceptor_id : initiator_id; 164 } 165 166 /// returns the initiators local link id 167 uint16_t getInitiatorId() const { 168 return initiator_id; 169 } 170 171 /// returns the acceptors local link id 172 uint16_t getAcceptorId() const { 173 return acceptor_id; 174 } 175 176 /// sets the local initiator id of the link 177 /// if id is unspecified a new local id is used as initiator id 178 void setInitiatorId( const LinkID& id = UNSPECIFIED ) { 179 assert(initiator_id == 0); 180 if ( id == UNSPECIFIED ) { 181 assert(local_id == 0); 182 local_id = LinkID::create().local_id; 183 initiator_id = local_id; 184 } else { 185 assert(local_id == acceptor_id && id.local_id == 0 && id.initiator_id != 0); 186 initiator_id = id.initiator_id; 187 } 188 } 189 190 /// sets the local acceptor id of the link 191 /// if id is unspecified a new local id is used as acceptor id 192 void setAcceptorId( const LinkID& id = UNSPECIFIED ) { 193 assert(acceptor_id == 0); 194 if ( id == UNSPECIFIED ) { 195 assert(local_id == 0); 196 local_id = LinkID::create().local_id; 197 acceptor_id = local_id; 198 } else { 199 assert(local_id == initiator_id && id.local_id == 0 && id.acceptor_id != 0); 200 acceptor_id = id.acceptor_id; 201 } 202 } 203 204 void combine( const LinkID& id ) { 205 206 } 207 208 /// returns a string representation of the link id 209 std::string toString() const { 210 char str[20]; 211 if (isLocal()) 212 sprintf(str, "l%04x", local_id); 213 else 214 sprintf(str, "i%04x.a%04x", initiator_id, acceptor_id ); 215 return std::string(str); 216 } 217 218 /// returns the info of the link id 219 const char* getInfo() const { 220 return getInfo(*this); 221 } 222 223 /// convenience operators 224 bool operator==( const LinkID& rhs ) const { return compareTo(rhs) == 0; } 225 bool operator!=( const LinkID& rhs ) const { return compareTo(rhs) != 0; } 226 bool operator< ( const LinkID& rhs ) const { return compareTo(rhs) < 0; } 227 bool operator<=( const LinkID& rhs ) const { return compareTo(rhs) <= 0; } 228 bool operator> ( const LinkID& rhs ) const { return compareTo(rhs) > 0; } 229 bool operator>=( const LinkID& rhs ) const { return compareTo(rhs) >= 0; } 67 private: 68 bool isvalid; 230 69 }; 231 232 std::ostream& operator<<(std::ostream& s, const LinkID& id );233 70 234 71 typedef vector<LinkID> LinkIDs; … … 236 73 }} // namespace ariba, utility 237 74 238 sznBeginDefault( ariba::utility::LinkID, X ) {239 if (X.isDeserializer()) local_id = 0;240 X && initiator_id && acceptor_id;241 } sznEnd();242 243 244 75 #endif /* LINKID_H_ */
Note:
See TracChangeset
for help on using the changeset viewer.