1/* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 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 18WriterAdapter::WriterAdapter(bool isProtobufSerialize) 19{ 20 writerStruct_.write = &WriterAdapter::WriteFunc; 21 writerStruct_.flush = &WriterAdapter::FlushFunc; 22 writerStruct_.startReport = &WriterAdapter::StartReportFunc; 23 writerStruct_.finishReport = &WriterAdapter::FinishReportFunc; 24 writerStruct_.isProtobufSerialize = isProtobufSerialize; 25} 26 27WriterAdapter::~WriterAdapter() {} 28 29void WriterAdapter::SetWriter(const WriterPtr& writer) 30{ 31 writer_ = writer; 32} 33 34WriterPtr WriterAdapter::GetWriter() 35{ 36 return writer_; 37} 38 39const WriterStruct* WriterAdapter::GetStruct() 40{ 41 return &writerStruct_; 42} 43 44long WriterAdapter::WriteFunc(WriterStruct* writer, const void* data, size_t size) 45{ 46 static_assert(offsetof(WriterAdapter, writerStruct_) == 0, "unexpected alignment of writerStruct_!"); 47 WriterAdapter* writerAdaptor = reinterpret_cast<WriterAdapter*>(writer); // 转成 WriterAdapter* 48 if (writerAdaptor && writerAdaptor->writer_) { 49 return writerAdaptor->writer_->Write(data, size); 50 } 51 return 0; 52} 53 54bool WriterAdapter::FlushFunc(WriterStruct* writer) 55{ 56 WriterAdapter* writerAdaptor = reinterpret_cast<WriterAdapter*>(writer); 57 if (writerAdaptor && writerAdaptor->writer_) { 58 return writerAdaptor->writer_->Flush(); 59 } 60 return false; 61} 62 63RandomWriteCtx* WriterAdapter::StartReportFunc(WriterStruct* writer) 64{ 65 WriterAdapter* writerAdaptor = reinterpret_cast<WriterAdapter*>(writer); 66 if (writerAdaptor && writerAdaptor->writer_) { 67 return writerAdaptor->writer_->StartReport(); 68 } 69 return nullptr; 70} 71 72void WriterAdapter::FinishReportFunc(WriterStruct* writer, int32_t size) 73{ 74 WriterAdapter* writerAdaptor = reinterpret_cast<WriterAdapter*>(writer); 75 if (writerAdaptor && writerAdaptor->writer_) { 76 writerAdaptor->writer_->FinishReport(size); 77 } 78}