summaryrefslogtreecommitdiff
path: root/include/hiddenpointer.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/hiddenpointer.h')
-rw-r--r--include/hiddenpointer.h168
1 files changed, 46 insertions, 122 deletions
diff --git a/include/hiddenpointer.h b/include/hiddenpointer.h
index f432f65..96bb26a 100644
--- a/include/hiddenpointer.h
+++ b/include/hiddenpointer.h
@@ -29,21 +29,20 @@ public:
*/
HiddenPointer(Type *pointer, const char *text, int size=-1)
{
- // calculate size
+ // calculate size of the text
if (size < 0) size = strlen(text);
- // reserve enough room for the text and the pointer
- _data.reserve(size + sizeof(Type *));
-
- // store the pointer
- _data.assign(std::string((const char *)&pointer, sizeof(Type *)));
+ // allocate data + trailing null + size of pointer
+ char *buffer = new char[size + 1 + sizeof(Type *)];
+
+ // copy pointer into the buffer
+ memcpy(buffer, &pointer, sizeof(Type *));
- // append the text
- _data.append(text, size);
+ // copy the name into the buffer
+ memcpy(buffer + sizeof(Type *), text, size + 1);
- // store pointers
- _pointer = pointer;
- _text = _data.c_str() + sizeof(Type *);
+ // store in member
+ _buffer = buffer;
}
/**
@@ -51,160 +50,85 @@ public:
* @param pointer
* @param text
*/
- HiddenPointer(Type *pointer, const std::string &text) : HiddenPointer(pointer, text.c_str(), text.size()) {}
+ HiddenPointer(Type *pointer, const std::string &text) :
+ HiddenPointer(pointer, text.c_str(), text.size()) {}
/**
* Constructor to retrieve the object given a buffer
* @param text The visible text
- * @param size Size of the text
*/
- HiddenPointer(const char *text, int size=-1)
- {
- // calculate size
- if (size < 0) size = strlen(text);
-
- // the pointer is stored right in front of the name
- _pointer = *((Type **)(text - sizeof(Type *)));
- _text = text;
- }
+ HiddenPointer(const char *text) :
+ _buffer(text - sizeof(Type *)), // the buffer starts before the actual text
+ _allocated(false) {} // no memory was allocated
/**
* Copy constructor
* @param that
*/
- HiddenPointer(const HiddenPointer<Type> &that) : _pointer(that._pointer), _text(that._text), _data(that._data)
+ HiddenPointer(const HiddenPointer<Type> &that) : _allocated(that._allocated)
{
- // if data is filled, the text is located inside the data
- if (_data.size() > 0) _text = _data.c_str() + sizeof(Type *);
+ // is the other object allocated?
+ if (_allocated)
+ {
+ // allocate this object too, call other constructor
+ HiddenPointer(that, that);
+ }
+ else
+ {
+ // just copy the data
+ _buffer = that._buffer;
+ }
}
/**
* Destructor
*/
- virtual ~HiddenPointer() {}
+ virtual ~HiddenPointer()
+ {
+ // destruct data
+ if (_allocated) delete[] _buffer;
+ }
/**
* Assignment operator
* @param that
* @return HiddenPointer
*/
- HiddenPointer<Type> operator=(const HiddenPointer &that)
- {
- // skip self assignment
- if (&that == this) return *this;
-
- // copy members
- _pointer = that._pointer;
- _text = that._text;
- _data = that._data;
-
- // if data is filled, the text is located inside the data
- if (_data.size() > 0) _text = _data.c_str() + sizeof(Type *);
- }
+ HiddenPointer<Type> operator=(const HiddenPointer &that) = delete;
/**
* Retrieve the pointer
* @return Type*
*/
- Type *pointer() const
+ operator Type * () const
{
- return _pointer;
- }
-
- /**
- * Change the pointer
- * @param Type*
- */
- void setPointer(Type *pointer)
- {
- // store pointer
- _pointer = pointer;
-
- // overwrite in data
- _data.replace(0, sizeof(Type *), (const char *)&_pointer, sizeof(Type *));
-
- // for safety reasons, we recalculate text pointer
- _text = _data.c_str() + sizeof(Type *);
+ // type is stored in front of the buffer
+ return *((Type **)_buffer);
}
/**
* Retrieve the text
* @return const char *
*/
- const char *text() const
- {
- return _text;
- }
-
- /**
- * Change the text
- * @param text
- * @param size
- */
- void setText(const char *text, int size=-1)
- {
- // check if size was set
- if (size < 0) size = strlen(text);
-
- // reserve enough room for the text and the pointer
- _data.reserve(size + sizeof(Type *));
-
- // store the pointer
- _data.assign(std::string((const char *)&_pointer, sizeof(Type *)));
-
- // append the text
- _data.append(text, size);
-
- // store new text
- _text = _data.c_str() + sizeof(Type *);
- }
-
- /**
- * Cast to the pointer
- * @return Type*
- */
- operator Type* () const
- {
- return _pointer;
- }
-
- /**
- * Cast to text
- * @return const char *
- */
operator const char * () const
{
- return _text;
+ // name starts a number of bytes further
+ return _buffer + sizeof(Type *);
}
- /**
- * Length of the text
- * @return int
- */
- int length() const
- {
- return _data.size() - sizeof(Type *);
- }
-
private:
/**
- * The actual pointer
- * @var Type*
+ * Buffer that holds both the pointer and the text - the text starts
+ * a number of bytes further up
+ * @var buffer
*/
- Type *_pointer;
-
- /**
- * The original text
- * @var text
- */
- const char *_text;
-
+ const char *_buffer = nullptr;
+
/**
- * Optional data buffer
- * @var string
+ * Was this allocated?
+ * @var bool
*/
- std::string _data;
-
+ bool _allocated = true;
};
/**