1 /* 2 * Copyright (c) 2022-2023 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 #ifndef DISTRIBUTEDDATAMGR_PASTEBOARD_PARCEL_UTIL_H 17 #define DISTRIBUTEDDATAMGR_PASTEBOARD_PARCEL_UTIL_H 18 19 #include "api/visibility.h" 20 #include "parcel.h" 21 #include "securec.h" 22 namespace OHOS::MiscServices { 23 24 struct RawMem { 25 uintptr_t buffer; 26 size_t bufferLen; 27 // notice:Keep the parcel reference to prevent the memory in the parcel from being destructed 28 std::shared_ptr<OHOS::Parcel> parcel; 29 }; 30 31 class ParcelUtil { 32 public: 33 // parcelable to buffer 34 API_EXPORT static RawMem Parcelable2Raw(const Parcelable *value); 35 36 // buffer to parcelable 37 template<typename ParcelableType> Raw2Parcelable(const RawMem & rawMem)38 static std::shared_ptr<ParcelableType> Raw2Parcelable(const RawMem &rawMem) 39 { 40 Parcel parcel(nullptr); 41 if (!Raw2Parcel(rawMem, parcel)) { 42 return nullptr; 43 } 44 return std::shared_ptr<ParcelableType>(ParcelableType::Unmarshalling(parcel)); 45 } 46 // buffer to parcelable 47 API_EXPORT static bool Raw2Parcel(const RawMem &rawMem, Parcel &parcel); 48 }; 49 } // namespace OHOS::MiscServices 50 #endif //DISTRIBUTEDDATAMGR_PASTEBOARD_PARCEL_UTIL_H 51