blob: 5ff9eadc25c7916ede7e71548c75c0c404d65a5c [file] [log] [blame]
mtkleinf074e702016-01-14 11:43:13 -08001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkValue_DEFINED
9#define SkValue_DEFINED
10
11#include "SkTypes.h"
12#include <functional>
13
halcanary76097f82016-01-20 10:00:26 -080014class SkData;
15
mtkleinf074e702016-01-14 11:43:13 -080016class SkValue {
17public:
18 enum Type : uint32_t {
19 // 0-255 are reserved for built-in SkValue types.
20 Null,
21 Byte , S16 , U16 , S32 , U32 , S64 , U64 , F32 , F64 ,
22 Bytes, S16s, U16s, S32s, U32s, S64s, U64s, F32s, F64s,
23 Array,
24
halcanary76097f82016-01-20 10:00:26 -080025 kMaxBuiltin = 0xFF,
mtkleinf074e702016-01-14 11:43:13 -080026 // 256-2147483647 may be used by Skia for public Object types.
27
halcanary76097f82016-01-20 10:00:26 -080028 Matrix,
mtkleinf074e702016-01-14 11:43:13 -080029
halcanary76097f82016-01-20 10:00:26 -080030 kMaxPublicObject = 0x7FFFFFFF,
31 // 2147483648+ won't be used by Skia. They're open for
32 // client-specific use, testing, etc.
mtkleinf074e702016-01-14 11:43:13 -080033 };
34
halcanary76097f82016-01-20 10:00:26 -080035 // Each Object type may define its own namespace of Key values,
36 // so there are no pre-defined Keys here.
37 //
38 // 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).
40 typedef uint32_t Key;
mtkleinf074e702016-01-14 11:43:13 -080041
42 SkValue();
43 SkValue(const SkValue&);
44 SkValue(SkValue&&);
45
46 SkValue& operator=(const SkValue&);
47 SkValue& operator=(SkValue&&);
48
49 ~SkValue();
50
51 static SkValue FromS32(int32_t);
52 static SkValue FromU32(uint32_t);
53 static SkValue FromF32(float);
halcanary76097f82016-01-20 10:00:26 -080054 static SkValue FromBytes(SkData*);
55 static SkValue FromU16s(SkData*);
56 static SkValue FromU32s(SkData*);
57 static SkValue FromF32s(SkData*);
58 static SkValue ValueArray();
mtkleinf074e702016-01-14 11:43:13 -080059 static SkValue Object(Type);
60
halcanary76097f82016-01-20 10:00:26 -080061 Type type() const { return fType; }
mtkleinf074e702016-01-14 11:43:13 -080062
63 // These remaining methods may assert they're called on a value of the appropriate type.
64
65 int32_t s32() const;
66 uint32_t u32() const;
67 float f32() const;
halcanary76097f82016-01-20 10:00:26 -080068 SkData* bytes() const;
mtkleinf074e702016-01-14 11:43:13 -080069
halcanary76097f82016-01-20 10:00:26 -080070 const uint16_t* u16s(int* count) const;
71 const uint32_t* u32s(int* count) const;
72 const float* f32s(int* count) const;
mtkleinf074e702016-01-14 11:43:13 -080073
halcanary76097f82016-01-20 10:00:26 -080074 // Object
mtkleinf074e702016-01-14 11:43:13 -080075 void set(Key, SkValue);
mtkleinf074e702016-01-14 11:43:13 -080076 void foreach(std::function<void(Key, const SkValue&)>) const;
77
halcanary76097f82016-01-20 10:00:26 -080078 // Array
79 size_t length() const;
80 const SkValue& at(size_t) const;
81 void append(SkValue);
82
mtkleinf074e702016-01-14 11:43:13 -080083private:
halcanary76097f82016-01-20 10:00:26 -080084 class Obj;
85 class Arr;
mtkleinf074e702016-01-14 11:43:13 -080086
87 Type fType;
88 union {
halcanary76097f82016-01-20 10:00:26 -080089 int32_t fS32;
90 uint32_t fU32;
91 float fF32;
92 SkData* fBytes;
93 Obj* fObject;
94 Arr* fArray;
mtkleinf074e702016-01-14 11:43:13 -080095 };
halcanary76097f82016-01-20 10:00:26 -080096
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;
mtkleinf074e702016-01-14 11:43:13 -0800108};
109
halcanary76097f82016-01-20 10:00:26 -0800110template <typename T>
111SkValue SkToValue(const T&);
112
113template <typename T>
114bool SkFromValue(const SkValue&, T*);
115
116#endif // SkValue_DEFINED