Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: src/core/SkValue.h

Issue 1604253002: SkValue: implementation, unit test (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: another unit test Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gyp/core.gypi ('k') | src/core/SkValue.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkValue_DEFINED 8 #ifndef SkValue_DEFINED
9 #define SkValue_DEFINED 9 #define SkValue_DEFINED
10 10
11 #include "SkTypes.h" 11 #include "SkTypes.h"
12 #include <functional> 12 #include <functional>
13 13
14 class SkData;
15
14 class SkValue { 16 class SkValue {
15 public: 17 public:
16 enum Type : uint32_t { 18 enum Type : uint32_t {
17 // 0-255 are reserved for built-in SkValue types. 19 // 0-255 are reserved for built-in SkValue types.
18 Null, 20 Null,
19 Byte , S16 , U16 , S32 , U32 , S64 , U64 , F32 , F64 , 21 Byte , S16 , U16 , S32 , U32 , S64 , U64 , F32 , F64 ,
20 Bytes, S16s, U16s, S32s, U32s, S64s, U64s, F32s, F64s, 22 Bytes, S16s, U16s, S32s, U32s, S64s, U64s, F32s, F64s,
21 Array, 23 Array,
22 24
25 kMaxBuiltin = 0xFF,
23 // 256-2147483647 may be used by Skia for public Object types. 26 // 256-2147483647 may be used by Skia for public Object types.
24 27
28 Matrix,
25 29
26 // 2147483648+ won't be used by Skia. They're open for client-specific use, testing, etc. 30 kMaxPublicObject = 0x7FFFFFFF,
31 // 2147483648+ won't be used by Skia. They're open for
32 // client-specific use, testing, etc.
27 }; 33 };
28 34
29 enum Key : uint32_t { 35 // Each Object type may define its own namespace of Key values,
30 // Each Object type may define its own namespace of Key values, 36 // so there are no pre-defined Keys here.
31 // so there are no pre-defined Keys here. 37 //
32 // 38 // This is just a reminder that they must fit in a uint32_t,
33 // This is just a reminder that they must fit in a uint32_t, 39 // and that their namespace is distinct from other uint32_ts (e.g. Type).
34 // and that their namespace is distinct from other uint32_ts (e.g. Type) . 40 typedef uint32_t Key;
35 };
36 41
37 SkValue(); 42 SkValue();
38 SkValue(const SkValue&); 43 SkValue(const SkValue&);
39 SkValue(SkValue&&); 44 SkValue(SkValue&&);
40 45
41 SkValue& operator=(const SkValue&); 46 SkValue& operator=(const SkValue&);
42 SkValue& operator=(SkValue&&); 47 SkValue& operator=(SkValue&&);
43 48
44 ~SkValue(); 49 ~SkValue();
45 50
46 static SkValue FromS32(int32_t); 51 static SkValue FromS32(int32_t);
47 static SkValue FromU32(uint32_t); 52 static SkValue FromU32(uint32_t);
48 static SkValue FromF32(float); 53 static SkValue FromF32(float);
49 static SkValue FromBytes(const void*, size_t); // Copies. 54 static SkValue FromBytes(SkData*);
55 static SkValue FromU16s(SkData*);
56 static SkValue FromU32s(SkData*);
57 static SkValue FromF32s(SkData*);
58 static SkValue ValueArray();
50 static SkValue Object(Type); 59 static SkValue Object(Type);
51 60
52 Type type() const; 61 Type type() const { return fType; }
53 62
54 // These remaining methods may assert they're called on a value of the appro priate type. 63 // These remaining methods may assert they're called on a value of the appro priate type.
55 64
56 int32_t s32() const; 65 int32_t s32() const;
57 uint32_t u32() const; 66 uint32_t u32() const;
58 float f32() const; 67 float f32() const;
68 SkData* bytes() const;
59 69
60 const void* bytes() const; 70 const uint16_t* u16s(int* count) const;
61 size_t count() const; 71 const uint32_t* u32s(int* count) const;
72 const float* f32s(int* count) const;
62 73
74 // Object
63 void set(Key, SkValue); 75 void set(Key, SkValue);
64 const SkValue* get(Key) const;
65 void foreach(std::function<void(Key, const SkValue&)>) const; 76 void foreach(std::function<void(Key, const SkValue&)>) const;
66 77
78 // Array
79 size_t length() const;
80 const SkValue& at(size_t) const;
81 void append(SkValue);
82
67 private: 83 private:
68 class Bytes; 84 class Obj;
69 class Object; 85 class Arr;
70 86
71 Type fType; 87 Type fType;
72 union { 88 union {
73 int32_t fS32; 89 int32_t fS32;
74 uint32_t fU32; 90 uint32_t fU32;
75 float fF32; 91 float fF32;
76 class Bytes* fBytes; 92 SkData* fBytes;
77 class Object* fObject; 93 Obj* fObject;
94 Arr* fArray;
78 }; 95 };
96
97 SkValue(Type);
98 bool isObject() const { return fType > kMaxBuiltin; }
99 bool isData() const {
100 return Bytes == fType
101 || U16s == fType
102 || U32s == fType
103 || F32s == fType;
104 }
105 template <typename T> static SkValue FromT(SkValue::Type, T SkValue::*, T);
106 template <typename T> static SkValue FromTs(SkValue::Type, SkData*);
107 template <typename T> const T* asTs(SkValue::Type, int*) const;
79 }; 108 };
80 109
81 #endif//SkValue_DEFINED 110 template <typename T>
111 SkValue SkToValue(const T&);
112
113 template <typename T>
114 bool SkFromValue(const SkValue&, T*);
115
116 #endif // SkValue_DEFINED
OLDNEW
« no previous file with comments | « gyp/core.gypi ('k') | src/core/SkValue.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698