Updating Xamarin/Microsoft file headers
[platform/upstream/libSkiaSharp.git] / src / xamarin / SkManagedStream.cpp
1 /*
2  * Copyright 2015 Xamarin Inc.
3  * Copyright 2017 Microsoft Corporation. All rights reserved.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8
9 #include "SkManagedStream.h"
10
11
12 // read stream
13 static read_delegate fRead = nullptr;
14 static peek_delegate fPeek = nullptr;
15 static isAtEnd_delegate fIsAtEnd = nullptr;
16 static hasPosition_delegate fHasPosition = nullptr;
17 static hasLength_delegate fHasLength = nullptr;
18 static rewind_delegate fRewind = nullptr;
19 static getPosition_delegate fGetPosition = nullptr;
20 static seek_delegate fSeek = nullptr;
21 static move_delegate fMove = nullptr;
22 static getLength_delegate fGetLength = nullptr;
23 static createNew_delegate fCreateNew = nullptr;
24 static destroy_delegate fDestroy = nullptr;
25
26 // write stream
27 static write_delegate fWrite = nullptr;
28 static flush_delegate fFlush = nullptr;
29 static bytesWritten_delegate fBytesWritten = nullptr;
30 static wdestroy_delegate fWDestroy = nullptr;
31
32
33 // the read stream
34
35 SkManagedStream::SkManagedStream() {
36     this->address = (size_t)this;
37 }
38
39 SkManagedStream::~SkManagedStream() {
40     ::fDestroy(address);
41 }
42
43 void SkManagedStream::setDelegates(const read_delegate pRead,
44                                    const peek_delegate pPeek,
45                                    const isAtEnd_delegate pIsAtEnd,
46                                    const hasPosition_delegate pHasPosition,
47                                    const hasLength_delegate pHasLength,
48                                    const rewind_delegate pRewind,
49                                    const getPosition_delegate pGetPosition,
50                                    const seek_delegate pSeek,
51                                    const move_delegate pMove,
52                                    const getLength_delegate pGetLength,
53                                    const createNew_delegate pCreateNew,
54                                    const destroy_delegate pDestroy)
55 {
56     ::fRead = (pRead);
57     ::fPeek = (pPeek);
58     ::fIsAtEnd = (pIsAtEnd);
59     ::fHasPosition = (pHasPosition);
60     ::fHasLength = (pHasLength);
61     ::fRewind = (pRewind);
62     ::fGetPosition = (pGetPosition);
63     ::fSeek = (pSeek);
64     ::fMove = (pMove);
65     ::fGetLength = (pGetLength);
66     ::fCreateNew = (pCreateNew);
67     ::fDestroy = (pDestroy);
68 }
69
70
71 size_t SkManagedStream::read(void* buffer, size_t size) {
72     return ::fRead(this, buffer, size);
73 }
74
75 size_t SkManagedStream::peek(void *buffer, size_t size) const {
76     SkManagedStream* nonConstThis = const_cast<SkManagedStream*>(this);
77     return ::fPeek(nonConstThis, buffer, size);
78 }
79
80 bool SkManagedStream::isAtEnd() const {
81     return ::fIsAtEnd(this);
82 }
83
84 bool SkManagedStream::hasPosition() const {
85     return ::fHasPosition(this);
86 }
87
88 bool SkManagedStream::hasLength() const {
89     return ::fHasLength(this);
90 }
91
92 bool SkManagedStream::rewind() {
93     return ::fRewind(this);
94 }
95
96 size_t SkManagedStream::getPosition() const {
97     return ::fGetPosition(this);
98 }
99
100 bool SkManagedStream::seek(size_t position) {
101     return ::fSeek(this, position);
102 }
103
104 bool SkManagedStream::move(long offset) {
105     return ::fMove(this, offset);
106 }
107
108 size_t SkManagedStream::getLength() const {
109     return ::fGetLength(this);
110 }
111
112 SkManagedStream* SkManagedStream::duplicate() const {
113     return ::fCreateNew(this);
114 }
115
116 SkManagedStream* SkManagedStream::fork() const {
117     std::unique_ptr<SkManagedStream> that(::fCreateNew(this));
118     that->seek(getPosition());
119     return that.release();
120 }
121
122
123 // the write stream
124
125 SkManagedWStream::SkManagedWStream() {
126     this->address = (size_t)this;
127 }
128
129 SkManagedWStream::~SkManagedWStream() {
130     ::fWDestroy(address);
131 }
132
133 void SkManagedWStream::setDelegates(const write_delegate pWrite,
134                                     const flush_delegate pFlush,
135                                     const bytesWritten_delegate pBytesWritten,
136                                     const wdestroy_delegate pDestroy)
137 {
138     ::fWrite = (pWrite);
139     ::fFlush = (pFlush);
140     ::fBytesWritten = (pBytesWritten);
141     ::fWDestroy = (pDestroy);
142 }
143
144 bool SkManagedWStream::write(const void* buffer, size_t size) {
145     return ::fWrite(this, buffer, size);
146 }
147
148 void SkManagedWStream::flush() {
149     ::fFlush(this);
150 }
151
152 size_t SkManagedWStream::bytesWritten() const {
153     return ::fBytesWritten(this);
154 }