#ifndef _H_CONSTDATA
#define _H_CONSTDATA
#include <Security/utilities.h>
#include <Security/refcount.h>
namespace Security {
class ConstData {
private:
class Blob : public RefCount {
public:
Blob() : mData(NULL), mSize(0) { }
Blob(const void *base, size_t size, bool takeOwnership = false);
~Blob() { delete[] reinterpret_cast<const char *>(mData); }
const void *data() const { return mData; }
size_t length() const { return mSize; }
private:
const void *mData;
size_t mSize;
};
public:
ConstData() { } ConstData(const void *base, size_t size, bool takeOwnership = false)
: mBlob(new Blob(base, size, takeOwnership)) { }
template <class T>
static ConstData wrap(const T &obj, bool takeOwnership)
{ return ConstData(&obj, sizeof(obj), takeOwnership); }
public:
const void *data() const { return mBlob ? mBlob->data() : NULL; }
size_t length() const { return mBlob ? mBlob->length() : 0; }
operator bool() const { return mBlob; }
bool operator !() const { return !mBlob; }
template <class T> operator const T *() const
{ return reinterpret_cast<const T *>(data()); }
template <class T> const T &as() const
{ return *static_cast<const T *>(reinterpret_cast<const T *>(data())); }
private:
RefPointer<Blob> mBlob;
};
}
#endif //_H_CONSTDATA