Chromium Code Reviews| Index: src/utils/SkHashDigest.h |
| =================================================================== |
| --- src/utils/SkHashDigest.h (revision 0) |
| +++ src/utils/SkHashDigest.h (revision 0) |
| @@ -0,0 +1,74 @@ |
| +/* |
| + * Copyright 2013 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#ifndef SkHashDigest_DEFINED |
| +#define SkHashDigest_DEFINED |
| + |
| +#include "SkData.h" |
| + |
| +/** |
| + * A mutable hash digest; it can be filled with a bytearray of any |
| + * length, anytime. |
| + */ |
| +class SkHashDigest { |
| +public: |
| + SkHashDigest() : fSkDataPtr(SkData::NewEmpty()) {} |
| + SkHashDigest(const void *data, size_t length) : fSkDataPtr(SkData::NewWithCopy(data, length)) {} |
| + |
| + /** |
| + * Destructor: if this object is the only one with a reference to the data |
| + * that makes up the hash digest, then that data will be freed. |
| + */ |
| + ~SkHashDigest() { |
| + SkSafeUnref(fSkDataPtr); |
| + } |
| + |
| + /** |
| + * Replace the hash digest data held by this object with a copy of the |
| + * data from this pointer/length. |
| + */ |
| + void copyFrom(void *data, size_t length) { |
|
epoger
2013/04/15 19:17:03
patchset 2: renamed SkHashDigest::set() to SkHashD
|
| + fSkDataPtr->unref(); |
| + fSkDataPtr = SkData::NewWithCopy(data, length); |
| + } |
| + |
| + /** |
| + * Return a pointer to the SkData object holding the hash digest data. |
| + * |
| + * The SkData object's reference counter will be bumped to account for this call, |
| + * so the caller must call unref() on it once it is no longer needed! |
| + * |
| + * For example: |
| + * { |
| + * SkData *skDataPtr = digest.skDataPtr(); |
| + * const uint8_t* bytes = skDataPtr->bytes(); |
| + * ... |
| + * skDataPtr->unref(); |
| + * } |
| + */ |
| + SkData *skDataPtr() const { |
| + fSkDataPtr->ref(); |
| + return fSkDataPtr; |
| + } |
| + |
| + size_t size() const { |
| + return fSkDataPtr->size(); |
| + } |
| + |
| + bool equals(const SkHashDigest &other) const { |
| + return fSkDataPtr->equals(other.fSkDataPtr); |
| + } |
| + |
| +private: |
| + |
| + // fSkDataPtr may change over the lifetime of the SkHashDigest object, |
| + // but it will never be set to NULL. |
| + // (Every SkHashDigest constructor initializes it to point at *some* SkData object.) |
| + SkData *fSkDataPtr; |
| +}; |
| + |
| +#endif |