1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "writer_adapter.h"
17
WriterAdapter()18 WriterAdapter::WriterAdapter()
19 {
20 writerStruct_.write = &WriterAdapter::WriteFunc;
21 writerStruct_.flush = &WriterAdapter::FlushFunc;
22 }
23
~WriterAdapter()24 WriterAdapter::~WriterAdapter() {}
25
SetWriter(const WriterPtr & writer)26 void WriterAdapter::SetWriter(const WriterPtr& writer)
27 {
28 writer_ = writer;
29 }
30
GetWriter()31 WriterPtr WriterAdapter::GetWriter()
32 {
33 return writer_;
34 }
35
GetStruct()36 const WriterStruct* WriterAdapter::GetStruct()
37 {
38 return &writerStruct_;
39 }
40
WriteFunc(WriterStruct * writer,const void * data,size_t size)41 long WriterAdapter::WriteFunc(WriterStruct* writer, const void* data, size_t size)
42 {
43 static_assert(offsetof(WriterAdapter, writerStruct_) == 0, "unexpected alignment of writerStruct_!");
44 WriterAdapter* writerAdaptor = reinterpret_cast<WriterAdapter*>(writer); // 转成 WriterAdapter*
45 if (writerAdaptor && writerAdaptor->writer_) {
46 return writerAdaptor->writer_->Write(data, size);
47 }
48 return 0;
49 }
50
FlushFunc(WriterStruct * writer)51 bool WriterAdapter::FlushFunc(WriterStruct* writer)
52 {
53 WriterAdapter* writerAdaptor = reinterpret_cast<WriterAdapter*>(writer);
54 if (writerAdaptor && writerAdaptor->writer_) {
55 return writerAdaptor->writer_->Flush();
56 }
57 return false;
58 }
59