Changeset 2437 for source/ariba/Name.h


Ignore:
Timestamp:
Feb 17, 2009, 12:05:25 PM (15 years ago)
Author:
Christoph Mayer
Message:

-memory leaks und corruption gefixt

File:
1 edited

Legend:

Unmodified
Added
Removed
  • source/ariba/Name.h

    r2409 r2437  
    4242#include <iostream>
    4343#include <memory.h>
     44#include <string>
     45
     46using std::string;
    4447
    4548// forward declaration
     
    6063class Name {
    6164        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 
    8565public:
    8666        static const Name UNSPECIFIED;
    8767
    88 public:
    8968        /**
    9069         * Constructs a new, yet unspecified name.
    9170         */
    92         inline Name() {
    93                 _bytes = NULL;
    94                 _length = 0;
    95                 _copy = false;
    96                 _hreadable = false;
    97         }
     71        Name();
    9872
    9973        /**
     
    10579         * @param copy A flag, whether the name's memory needs to be copied
    10680         */
    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);
    11082
    11183        /**
     
    11486         * @param name A human readable name
    11587         */
    116         inline Name(std::string name) {
    117                 init(name.c_str(), -1, true, true);
    118         }
     88        Name(string name);
    11989
    12090        /**
    12191         * The copy constructor.
    12292         */
    123         inline Name(const Name& name) {
    124                 init((const char*)name.bytes(), name.length(), true, name._hreadable);
    125         }
     93        Name(const Name& name);
    12694
    12795        /**
    12896         * Destroys the name and releases underlying memory if this name is a copy.
    12997         */
    130         inline ~Name() {
    131                 if (_copy) delete _bytes;
    132         }
     98        virtual ~Name();
    13399
    134100        /**
     
    137103         * @return The binary data
    138104         */
    139         inline const uint8_t* bytes() const {
    140                 return _bytes;
    141         }
     105        const uint8_t* bytes() const;
    142106
    143107        /**
     
    146110         * @return The length of the name
    147111         */
    148         inline const size_t length() const {
    149                 return _length;
    150         }
     112        const size_t length() const;
    151113
    152114        /**
    153115         * The common implementation of the "equal" operator.
    154116         */
    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;
    160118
    161119        /**
    162120         * The common implementation of the "unequal" operator.
    163121         */
    164         inline bool operator!=(const Name& name) const {
    165                 return !(*this == name);
    166         }
     122        bool operator!=(const Name& name) const;
    167123
    168124        /**
    169125         * Returns true, if the name is yet unspecified
    170126         */
    171         inline bool isUnspecified() {
    172                 return *this == UNSPECIFIED;
    173         }
     127        bool isUnspecified() const;
    174128
    175129        /**
    176130         * Returns a random name.
    177131         */
    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();
    185133
    186134        /**
    187135         * Returns a human-readable representation of this name
    188136         */
    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;
    199138
    200139        // hack: to be changed!
    201         inline NodeID toNodeId() const {
    202                 return NodeID::sha1( bytes(), length() );
    203         }
     140        NodeID toNodeId() const;
    204141
    205142        // hack: to be changed!
    206         inline SpoVNetID toSpoVNetId() const {
    207                 return SpoVNetID::sha1(bytes(), length() );
    208         }
     143        SpoVNetID toSpoVNetId() const;
     144
     145private:
     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);
    209152};
    210153
Note: See TracChangeset for help on using the changeset viewer.