2011-10-28 00:05:31 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef SkData_DEFINED
|
|
|
|
#define SkData_DEFINED
|
|
|
|
|
2012-08-23 11:34:29 -07:00
|
|
|
#include "SkFlattenable.h"
|
2011-10-28 00:05:31 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SkData holds an immutable data buffer. Not only is the data immutable,
|
|
|
|
* but the actual ptr that is returned (by data() or bytes()) is guaranteed
|
|
|
|
* to always be the same for the life of this instance.
|
|
|
|
*/
|
2012-08-23 11:34:29 -07:00
|
|
|
class SK_API SkData : public SkFlattenable {
|
2011-10-28 00:05:31 -07:00
|
|
|
public:
|
2012-08-23 11:34:29 -07:00
|
|
|
SK_DECLARE_INST_COUNT(SkData)
|
|
|
|
|
2011-10-28 00:05:31 -07:00
|
|
|
/**
|
|
|
|
* Returns the number of bytes stored.
|
|
|
|
*/
|
|
|
|
size_t size() const { return fSize; }
|
|
|
|
|
2012-08-23 11:34:29 -07:00
|
|
|
bool isEmpty() const { return 0 == fSize; }
|
|
|
|
|
2011-10-28 00:05:31 -07:00
|
|
|
/**
|
|
|
|
* Returns the ptr to the data.
|
|
|
|
*/
|
|
|
|
const void* data() const { return fPtr; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like data(), returns a read-only ptr into the data, but in this case
|
|
|
|
* it is cast to uint8_t*, to make it easy to add an offset to it.
|
|
|
|
*/
|
|
|
|
const uint8_t* bytes() const {
|
|
|
|
return reinterpret_cast<const uint8_t*>(fPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to copy a range of the data into a caller-provided buffer.
|
|
|
|
* Returns the actual number of bytes copied, after clamping offset and
|
|
|
|
* length to the size of the data. If buffer is NULL, it is ignored, and
|
|
|
|
* only the computed number of bytes is returned.
|
|
|
|
*/
|
|
|
|
size_t copyRange(size_t offset, size_t length, void* buffer) const;
|
|
|
|
|
2012-08-23 11:34:29 -07:00
|
|
|
/**
|
|
|
|
* Returns true if these two objects have the same length and contents,
|
|
|
|
* effectively returning 0 == memcmp(...)
|
|
|
|
*/
|
|
|
|
bool equals(const SkData* other) const;
|
|
|
|
|
2011-10-28 00:05:31 -07:00
|
|
|
/**
|
|
|
|
* Function that, if provided, will be called when the SkData goes out
|
|
|
|
* of scope, allowing for custom allocation/freeing of the data.
|
|
|
|
*/
|
|
|
|
typedef void (*ReleaseProc)(const void* ptr, size_t length, void* context);
|
2012-08-23 11:34:29 -07:00
|
|
|
|
2011-10-28 00:05:31 -07:00
|
|
|
/**
|
|
|
|
* Create a new dataref by copying the specified data
|
|
|
|
*/
|
|
|
|
static SkData* NewWithCopy(const void* data, size_t length);
|
|
|
|
|
2012-08-23 11:34:29 -07:00
|
|
|
/**
|
|
|
|
* Create a new dataref by copying the specified c-string
|
|
|
|
* (a null-terminated array of bytes). The returned SkData will have size()
|
|
|
|
* equal to strlen(cstr) + 1. If cstr is NULL, it will be treated the same
|
|
|
|
* as "".
|
|
|
|
*/
|
|
|
|
static SkData* NewWithCString(const char cstr[]);
|
|
|
|
|
2011-10-28 00:05:31 -07:00
|
|
|
/**
|
|
|
|
* Create a new dataref, taking the data ptr as is, and using the
|
|
|
|
* releaseproc to free it. The proc may be NULL.
|
|
|
|
*/
|
|
|
|
static SkData* NewWithProc(const void* data, size_t length,
|
|
|
|
ReleaseProc proc, void* context);
|
|
|
|
|
|
|
|
/**
|
2012-05-23 11:43:41 -07:00
|
|
|
* Create a new dataref from a pointer allocated by malloc. The Data object
|
|
|
|
* takes ownership of that allocation, and will handling calling sk_free.
|
2011-10-28 00:05:31 -07:00
|
|
|
*/
|
|
|
|
static SkData* NewFromMalloc(const void* data, size_t length);
|
|
|
|
|
2013-04-02 13:49:07 -07:00
|
|
|
/**
|
|
|
|
* Create a new dataref from a pointer allocated by mmap. The Data object
|
|
|
|
* will handle calling munmap().
|
|
|
|
*/
|
|
|
|
static SkData* NewFromMMap(const void* data, size_t length);
|
|
|
|
|
2011-10-28 00:05:31 -07:00
|
|
|
/**
|
|
|
|
* Create a new dataref using a subset of the data in the specified
|
|
|
|
* src dataref.
|
|
|
|
*/
|
|
|
|
static SkData* NewSubset(const SkData* src, size_t offset, size_t length);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a new empty dataref (or a reference to a shared empty dataref).
|
|
|
|
* New or shared, the caller must see that unref() is eventually called.
|
|
|
|
*/
|
|
|
|
static SkData* NewEmpty();
|
|
|
|
|
2012-08-23 11:34:29 -07:00
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkData)
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkData(SkFlattenableReadBuffer&);
|
|
|
|
virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
|
|
|
|
|
2011-10-28 00:05:31 -07:00
|
|
|
private:
|
|
|
|
ReleaseProc fReleaseProc;
|
|
|
|
void* fReleaseProcContext;
|
|
|
|
|
|
|
|
const void* fPtr;
|
|
|
|
size_t fSize;
|
|
|
|
|
|
|
|
SkData(const void* ptr, size_t size, ReleaseProc, void* context);
|
2012-08-23 11:34:29 -07:00
|
|
|
virtual ~SkData();
|
|
|
|
|
|
|
|
// This is here because SkAutoTUnref creates an internal helper class
|
|
|
|
// that derives from SkData (i.e., BlockRef) to prevent refs\unrefs.
|
|
|
|
// This helper class generates a compiler warning on Windows since the
|
|
|
|
// SkData's destructor is private. This friending gives the helper class
|
|
|
|
// access to the destructor.
|
|
|
|
friend class SkAutoTUnref<SkData>::BlockRef<SkData>;
|
|
|
|
|
|
|
|
typedef SkFlattenable INHERITED;
|
2011-10-28 00:05:31 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specialized version of SkAutoTUnref<SkData> for automatically unref-ing a
|
2013-04-02 13:49:07 -07:00
|
|
|
* SkData.
|
2011-10-28 00:05:31 -07:00
|
|
|
*/
|
|
|
|
class SkAutoDataUnref : SkNoncopyable {
|
|
|
|
public:
|
2012-08-23 11:34:29 -07:00
|
|
|
SkAutoDataUnref(SkData* data) : fRef(data) {}
|
2011-10-28 00:05:31 -07:00
|
|
|
~SkAutoDataUnref() {
|
|
|
|
SkSafeUnref(fRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkData* get() const { return fRef; }
|
|
|
|
|
|
|
|
void release() {
|
|
|
|
if (fRef) {
|
|
|
|
fRef->unref();
|
|
|
|
fRef = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-23 11:34:29 -07:00
|
|
|
SkData *operator->() const { return fRef; }
|
|
|
|
operator SkData*() { return fRef; }
|
|
|
|
|
2011-10-28 00:05:31 -07:00
|
|
|
private:
|
|
|
|
SkData* fRef;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|