Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017 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 ok_DEFINED |
| 9 | #define ok_DEFINED |
| 10 | |
| 11 | #include "SkCanvas.h" |
| 12 | #include <functional> |
| 13 | #include <map> |
| 14 | #include <memory> |
| 15 | #include <string> |
| 16 | |
| 17 | // Not really ok-specific, but just kind of generally handy. |
| 18 | template <typename T> |
| 19 | static std::unique_ptr<T> move_unique(T& v) { |
| 20 | return std::unique_ptr<T>{new T{std::move(v)}}; |
| 21 | } |
| 22 | |
| 23 | struct Src { |
| 24 | virtual ~Src() {} |
| 25 | virtual std::string name() = 0; // ok always calls Src methods in order: |
| 26 | virtual SkISize size() = 0; // name() -> size() -> draw(), possibly |
| 27 | virtual void draw(SkCanvas*) = 0; // stopping after calling name(). |
| 28 | }; |
| 29 | |
| 30 | struct Stream { |
| 31 | virtual ~Stream() {} |
| 32 | virtual std::unique_ptr<Src> next() = 0; |
| 33 | }; |
| 34 | |
| 35 | struct Dst { |
| 36 | virtual ~Dst() {} |
| 37 | virtual SkCanvas* canvas() = 0; |
| 38 | virtual void write(std::string path_prefix) = 0; // All but the file extension. |
| 39 | }; |
| 40 | |
| 41 | class Options { |
| 42 | std::map<std::string, std::string> kv; |
| 43 | public: |
| 44 | explicit Options(std::string str = ""); |
| 45 | std::string operator()(std::string k, std::string fallback = "") const; |
| 46 | }; |
| 47 | |
| 48 | // Create globals to register your new type of Stream or Dst. |
| 49 | struct Register { |
| 50 | Register(const char* name, std::unique_ptr<Stream> (*factory)(Options)); |
| 51 | Register(const char* name, std::unique_ptr<Dst> (*factory)(SkISize, Options)); |
| 52 | }; |
| 53 | |
| 54 | #endif//ok_DEFINED |