Changeset 2437 for source/ariba
- Timestamp:
- Feb 17, 2009, 12:05:25 PM (16 years ago)
- Location:
- source/ariba
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
source/ariba/Name.cpp
r2409 r2437 39 39 #include "Name.h" 40 40 41 namespace ariba {42 const Name Name::UNSPECIFIED;43 }44 45 41 std::ostream& operator<<( std::ostream& s, const ariba::Name& n ) { 46 42 return s << n.toString(); 47 43 } 48 44 45 namespace ariba { 46 47 const Name Name::UNSPECIFIED; 48 49 void Name::init(const char* name, int len, bool copy, bool hreadable) { 50 51 // delete the old buffer 52 if( _bytes != NULL ) { 53 delete[] _bytes; 54 _bytes = NULL; 55 _length = 0; 56 } 57 58 // alloc new stuff 59 if (len == -1) 60 len = strlen(name); 61 62 if (copy) { 63 64 if ((name!=NULL) && (len>0)){ 65 _bytes = new uint8_t[len]; 66 memcpy( _bytes, name, len ); 67 } else { 68 len = 0; 69 _bytes = NULL; 70 } 71 72 } else { 73 _bytes = (uint8_t*)name; 74 } 75 76 _copy = copy; 77 _length = len; 78 _hreadable = hreadable; 79 } 80 81 Name::Name() 82 : _bytes( NULL ), _length( 0 ), _copy( false ), _hreadable( false) { 83 } 84 85 Name::Name(const char* name, int len, bool copy) 86 : _bytes( NULL ), _length( 0 ), _copy( false ), _hreadable( false) { 87 init(name, len, copy, len == -1); 88 } 89 90 Name::Name(string name) 91 : _bytes( NULL ), _length( 0 ), _copy( false ), _hreadable( false) { 92 init(name.c_str(), name.length(), true, true); 93 } 94 95 Name::Name(const Name& name) 96 : _bytes( NULL ), _length( 0 ), _copy( false ), _hreadable( false) { 97 init((const char*)name.bytes(), name.length(), true, name._hreadable); 98 } 99 100 Name::~Name() { 101 if (_copy && (_bytes!=NULL)){ 102 delete[] _bytes; 103 _bytes = NULL; 104 _length = 0; 105 } 106 } 107 108 const uint8_t* Name::bytes() const { 109 return _bytes; 110 } 111 112 const size_t Name::length() const { 113 return _length; 114 } 115 116 bool Name::operator==(const Name& name) const { 117 118 // unspecified Name objects 119 if (_bytes == NULL && name._bytes == NULL && 120 length() == name.length()) return true; 121 122 // specified name objects 123 if (_bytes == NULL || name._bytes == NULL) return false; 124 if (name.length() != length()) return false; 125 return (memcmp(name.bytes(), bytes(), length()) == 0); 126 } 127 128 bool Name::operator!=(const Name& name) const { 129 return !(*this == name); 130 } 131 132 bool Name::isUnspecified() const { 133 return *this == UNSPECIFIED; 134 } 135 136 Name Name::random() { 137 char name[17]; 138 for (int i=0;i<16; i++) 139 name[i] = ("abcdefghijklmnopqrstuvwxyz")[((unsigned)rand())%26]; 140 name[16] = 0; 141 142 // force use of the std::string ctor with this 143 return Name( string(name) ); 144 } 145 146 string Name::toString() const { 147 if (_hreadable) { 148 char str[256]; 149 for (int i=0; i<length(); i++) str[i] = bytes()[i]; 150 str[length()] = 0; 151 return string(str); 152 } else { 153 return string("<not readable>"); 154 } 155 } 156 157 NodeID Name::toNodeId() const { 158 if( bytes()==NULL || length()==0 ) return NodeID::UNSPECIFIED; 159 return NodeID( Identifier::sha1(bytes(),length()) ); 160 } 161 162 SpoVNetID Name::toSpoVNetId() const { 163 if( bytes()==NULL || length()==0 ) return SpoVNetID::UNSPECIFIED; 164 return SpoVNetID( Identifier::sha1(bytes(),length()) ); 165 } 166 167 } // namespace ariba -
source/ariba/Name.h
r2409 r2437 42 42 #include <iostream> 43 43 #include <memory.h> 44 #include <string> 45 46 using std::string; 44 47 45 48 // forward declaration … … 60 63 class Name { 61 64 friend std::ostream& operator<<( std::ostream&, const ::ariba::Name& ); 62 63 private:64 bool _hreadable;65 bool _copy;66 int _length;67 uint8_t* _bytes;68 69 inline void init(const char* name, int len, bool copy, bool hreadable) {70 if (len == -1)71 len = strlen(name);72 73 if (copy) {74 _bytes = new uint8_t[len];75 memcpy(_bytes, name, len);76 } else {77 _bytes = (uint8_t*)name;78 }79 80 _copy = copy;81 _length = len;82 _hreadable = hreadable;83 }84 85 65 public: 86 66 static const Name UNSPECIFIED; 87 67 88 public:89 68 /** 90 69 * Constructs a new, yet unspecified name. 91 70 */ 92 inline Name() { 93 _bytes = NULL; 94 _length = 0; 95 _copy = false; 96 _hreadable = false; 97 } 71 Name(); 98 72 99 73 /** … … 105 79 * @param copy A flag, whether the name's memory needs to be copied 106 80 */ 107 inline Name(const char* name, int len = -1, bool copy = false) { 108 init(name, len, copy, len == -1); 109 } 81 Name(const char* name, int len = -1, bool copy = false); 110 82 111 83 /** … … 114 86 * @param name A human readable name 115 87 */ 116 inline Name(std::string name) { 117 init(name.c_str(), -1, true, true); 118 } 88 Name(string name); 119 89 120 90 /** 121 91 * The copy constructor. 122 92 */ 123 inline Name(const Name& name) { 124 init((const char*)name.bytes(), name.length(), true, name._hreadable); 125 } 93 Name(const Name& name); 126 94 127 95 /** 128 96 * Destroys the name and releases underlying memory if this name is a copy. 129 97 */ 130 inline ~Name() { 131 if (_copy) delete _bytes; 132 } 98 virtual ~Name(); 133 99 134 100 /** … … 137 103 * @return The binary data 138 104 */ 139 inline const uint8_t* bytes() const { 140 return _bytes; 141 } 105 const uint8_t* bytes() const; 142 106 143 107 /** … … 146 110 * @return The length of the name 147 111 */ 148 inline const size_t length() const { 149 return _length; 150 } 112 const size_t length() const; 151 113 152 114 /** 153 115 * The common implementation of the "equal" operator. 154 116 */ 155 inline bool operator==(const Name& name) const { 156 if (_bytes == NULL || name._bytes == NULL) return false; 157 if (name.length() != length()) return false; 158 return (memcmp(name.bytes(), bytes(), length()) == 0); 159 } 117 bool operator==(const Name& name) const; 160 118 161 119 /** 162 120 * The common implementation of the "unequal" operator. 163 121 */ 164 inline bool operator!=(const Name& name) const { 165 return !(*this == name); 166 } 122 bool operator!=(const Name& name) const; 167 123 168 124 /** 169 125 * Returns true, if the name is yet unspecified 170 126 */ 171 inline bool isUnspecified() { 172 return *this == UNSPECIFIED; 173 } 127 bool isUnspecified() const; 174 128 175 129 /** 176 130 * Returns a random name. 177 131 */ 178 inline static Name random() { 179 char name[17]; 180 for (int i=0;i<16; i++) 181 name[i] = ("abcdefghijklmnopqrstuvwxyz")[((unsigned)rand())%26]; 182 name[16] = 0; 183 return Name(name); 184 } 132 static Name random(); 185 133 186 134 /** 187 135 * Returns a human-readable representation of this name 188 136 */ 189 inline std::string toString() const { 190 if (_hreadable) { 191 char str[256]; 192 for (int i=0; i<length(); i++) str[i] = bytes()[i]; 193 str[length()] = 0; 194 return std::string(str); 195 } else { 196 return std::string("<not readable>"); 197 } 198 } 137 string toString() const; 199 138 200 139 // hack: to be changed! 201 inline NodeID toNodeId() const { 202 return NodeID::sha1( bytes(), length() ); 203 } 140 NodeID toNodeId() const; 204 141 205 142 // hack: to be changed! 206 inline SpoVNetID toSpoVNetId() const { 207 return SpoVNetID::sha1(bytes(), length() ); 208 } 143 SpoVNetID toSpoVNetId() const; 144 145 private: 146 bool _hreadable; 147 bool _copy; 148 int _length; 149 uint8_t* _bytes; 150 151 void init(const char* name, int len, bool copy, bool hreadable); 209 152 }; 210 153
Note:
See TracChangeset
for help on using the changeset viewer.