reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | #ifndef SkMetaData_DEFINED |
| 8 | #define SkMetaData_DEFINED |
| 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkScalar.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | |
Hal Canary | a1730e1 | 2019-07-24 09:44:43 -0400 | [diff] [blame] | 12 | /** A map from c-string keys to arrays of POD (int32_t, kScalar, void*, or bool) |
| 13 | values. |
| 14 | */ |
| 15 | class SkMetaData { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | public: |
Hal Canary | a1730e1 | 2019-07-24 09:44:43 -0400 | [diff] [blame] | 17 | SkMetaData() {} |
| 18 | ~SkMetaData() { if (fRec) { this->reset(); } } |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 19 | void reset(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 21 | bool findS32(const char name[], int32_t* value = nullptr) const; |
| 22 | bool findScalar(const char name[], SkScalar* value = nullptr) const; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 23 | const SkScalar* findScalars(const char name[], int* count, |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 24 | SkScalar values[] = nullptr) const; |
Hal Canary | ca9e187 | 2019-07-25 10:36:30 -0400 | [diff] [blame] | 25 | bool findPtr(const char name[], void** value = nullptr) const; |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 26 | bool findBool(const char name[], bool* value = nullptr) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 28 | bool hasS32(const char name[], int32_t value) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | int32_t v; |
| 30 | return this->findS32(name, &v) && v == value; |
| 31 | } |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 32 | bool hasScalar(const char name[], SkScalar value) const { |
| 33 | SkScalar v; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | return this->findScalar(name, &v) && v == value; |
| 35 | } |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 36 | bool hasPtr(const char name[], void* value) const { |
| 37 | void* v; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | return this->findPtr(name, &v) && v == value; |
| 39 | } |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 40 | bool hasBool(const char name[], bool value) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | bool v; |
| 42 | return this->findBool(name, &v) && v == value; |
| 43 | } |
| 44 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 45 | void setS32(const char name[], int32_t value); |
| 46 | void setScalar(const char name[], SkScalar value); |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 47 | SkScalar* setScalars(const char name[], int count, const SkScalar values[] = nullptr); |
Hal Canary | ca9e187 | 2019-07-25 10:36:30 -0400 | [diff] [blame] | 48 | void setPtr(const char name[], void* value); |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 49 | void setBool(const char name[], bool value); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 51 | bool removeS32(const char name[]); |
| 52 | bool removeScalar(const char name[]); |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 53 | bool removePtr(const char name[]); |
| 54 | bool removeBool(const char name[]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | |
| 56 | enum Type { |
| 57 | kS32_Type, |
| 58 | kScalar_Type, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | kPtr_Type, |
| 60 | kBool_Type, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | |
| 62 | kTypeCount |
| 63 | }; |
| 64 | |
| 65 | struct Rec; |
| 66 | class Iter; |
| 67 | friend class Iter; |
| 68 | |
| 69 | class Iter { |
| 70 | public: |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 71 | Iter() : fRec(nullptr) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | Iter(const SkMetaData&); |
| 73 | |
| 74 | /** Reset the iterator, so that calling next() will return the first |
| 75 | data element. This is done implicitly in the constructor. |
| 76 | */ |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 77 | void reset(const SkMetaData&); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | |
| 79 | /** Each time next is called, it returns the name of the next data element, |
| 80 | or null when there are no more elements. If non-null is returned, then the |
| 81 | element's type is returned (if not null), and the number of data values |
| 82 | is returned in count (if not null). |
| 83 | */ |
| 84 | const char* next(Type*, int* count); |
| 85 | |
| 86 | private: |
| 87 | Rec* fRec; |
| 88 | }; |
| 89 | |
| 90 | public: |
| 91 | struct Rec { |
| 92 | Rec* fNext; |
| 93 | uint16_t fDataCount; // number of elements |
| 94 | uint8_t fDataLen; // sizeof a single element |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | uint8_t fType; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | |
| 97 | const void* data() const { return (this + 1); } |
| 98 | void* data() { return (this + 1); } |
| 99 | const char* name() const { return (const char*)this->data() + fDataLen * fDataCount; } |
| 100 | char* name() { return (char*)this->data() + fDataLen * fDataCount; } |
| 101 | |
| 102 | static Rec* Alloc(size_t); |
| 103 | static void Free(Rec*); |
| 104 | }; |
Hal Canary | a1730e1 | 2019-07-24 09:44:43 -0400 | [diff] [blame] | 105 | Rec* fRec = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | |
| 107 | const Rec* find(const char name[], Type) const; |
| 108 | void* set(const char name[], const void* data, size_t len, Type, int count); |
| 109 | bool remove(const char name[], Type); |
Hal Canary | a1730e1 | 2019-07-24 09:44:43 -0400 | [diff] [blame] | 110 | |
| 111 | SkMetaData(const SkMetaData&) = delete; |
| 112 | SkMetaData& operator=(const SkMetaData&) = delete; |
Ben Wagner | 1968473 | 2020-08-21 16:52:48 -0400 | [diff] [blame] | 113 | |
| 114 | private: |
| 115 | struct FindResult { |
| 116 | SkMetaData::Rec* rec; |
| 117 | SkMetaData::Rec* prev; |
| 118 | }; |
| 119 | FindResult findWithPrev(const char name[], Type type) const; |
| 120 | void remove(FindResult); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | #endif |