fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 1 | /* |
| 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 SkSVGValue_DEFINED |
| 9 | #define SkSVGValue_DEFINED |
| 10 | |
| 11 | #include "SkColor.h" |
| 12 | #include "SkMatrix.h" |
| 13 | #include "SkPath.h" |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 14 | #include "SkSVGTypes.h" |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 15 | #include "SkTypes.h" |
| 16 | |
| 17 | class SkSVGValue : public SkNoncopyable { |
| 18 | public: |
| 19 | enum class Type { |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 20 | kClip, |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 21 | kColor, |
Florin Malita | e932d4b | 2016-12-01 13:35:11 -0500 | [diff] [blame] | 22 | kFillRule, |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 23 | kLength, |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 24 | kLineCap, |
| 25 | kLineJoin, |
| 26 | kNumber, |
| 27 | kPaint, |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 28 | kPath, |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 29 | kPoints, |
fmalita | cecd617 | 2016-09-13 12:56:11 -0700 | [diff] [blame] | 30 | kSpreadMethod, |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 31 | kString, |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 32 | kTransform, |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 33 | kViewBox, |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | Type type() const { return fType; } |
| 37 | |
| 38 | template <typename T> |
| 39 | const T* as() const { |
| 40 | return fType == T::TYPE ? static_cast<const T*>(this) : nullptr; |
| 41 | } |
| 42 | |
| 43 | protected: |
| 44 | SkSVGValue(Type t) : fType(t) { } |
| 45 | |
| 46 | private: |
| 47 | Type fType; |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 48 | |
| 49 | typedef SkNoncopyable INHERITED; |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 52 | template <typename T, SkSVGValue::Type ValueType> |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 53 | class SkSVGWrapperValue final : public SkSVGValue { |
| 54 | public: |
| 55 | static constexpr Type TYPE = ValueType; |
| 56 | |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 57 | explicit SkSVGWrapperValue(const T& v) |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 58 | : INHERITED(ValueType) |
| 59 | , fWrappedValue(v) { } |
| 60 | |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 61 | operator const T&() const { return fWrappedValue; } |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 62 | const T* operator->() const { return &fWrappedValue; } |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 63 | |
| 64 | private: |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 65 | // Stack-only |
| 66 | void* operator new(size_t) = delete; |
| 67 | void* operator new(size_t, void*) = delete; |
| 68 | |
| 69 | const T& fWrappedValue; |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 70 | |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 71 | typedef SkSVGValue INHERITED; |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 74 | using SkSVGClipValue = SkSVGWrapperValue<SkSVGClip , SkSVGValue::Type::kClip >; |
fmalita | cecd617 | 2016-09-13 12:56:11 -0700 | [diff] [blame] | 75 | using SkSVGColorValue = SkSVGWrapperValue<SkSVGColorType , SkSVGValue::Type::kColor >; |
Florin Malita | e932d4b | 2016-12-01 13:35:11 -0500 | [diff] [blame] | 76 | using SkSVGFillRuleValue = SkSVGWrapperValue<SkSVGFillRule , SkSVGValue::Type::kFillRule >; |
fmalita | cecd617 | 2016-09-13 12:56:11 -0700 | [diff] [blame] | 77 | using SkSVGLengthValue = SkSVGWrapperValue<SkSVGLength , SkSVGValue::Type::kLength >; |
| 78 | using SkSVGPathValue = SkSVGWrapperValue<SkPath , SkSVGValue::Type::kPath >; |
| 79 | using SkSVGTransformValue = SkSVGWrapperValue<SkSVGTransformType, SkSVGValue::Type::kTransform>; |
| 80 | using SkSVGViewBoxValue = SkSVGWrapperValue<SkSVGViewBoxType , SkSVGValue::Type::kViewBox >; |
| 81 | using SkSVGPaintValue = SkSVGWrapperValue<SkSVGPaint , SkSVGValue::Type::kPaint >; |
| 82 | using SkSVGLineCapValue = SkSVGWrapperValue<SkSVGLineCap , SkSVGValue::Type::kLineCap >; |
| 83 | using SkSVGLineJoinValue = SkSVGWrapperValue<SkSVGLineJoin , SkSVGValue::Type::kLineJoin >; |
| 84 | using SkSVGNumberValue = SkSVGWrapperValue<SkSVGNumberType , SkSVGValue::Type::kNumber >; |
| 85 | using SkSVGPointsValue = SkSVGWrapperValue<SkSVGPointsType , SkSVGValue::Type::kPoints >; |
| 86 | using SkSVGStringValue = SkSVGWrapperValue<SkSVGStringType , SkSVGValue::Type::kString >; |
| 87 | using SkSVGSpreadMethodValue = SkSVGWrapperValue<SkSVGSpreadMethod , |
| 88 | SkSVGValue::Type::kSpreadMethod>; |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 89 | |
| 90 | #endif // SkSVGValue_DEFINED |