diff --git a/frameworks/native/common/src/datashare_template.cpp b/frameworks/native/common/src/datashare_template.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a6224d6a1c2924d8dd5ebf4cc364d30d8546ac3a --- /dev/null +++ b/frameworks/native/common/src/datashare_template.cpp @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "datashare_template.h" + +namespace OHOS { +namespace DataShare { +PublishedDataItem::~PublishedDataItem() +{ + AshmemNode *node = std::get_if(&value_); + if (node != nullptr) { + if (node->isManaged && node->ashmem != nullptr) { + node->ashmem->UnmapAshmem(); + node->ashmem->CloseAshmem(); + } + } +} + +PublishedDataItem::PublishedDataItem( + const std::string &key, int64_t subscriberId, std::variant, std::string> value) + + : key_(key), subscriberId_(subscriberId) +{ + if (value.index() == 0) { + std::vector &vecValue = std::get>(value); + auto size = vecValue.size() * sizeof(uint32_t); + sptr mem = Ashmem::CreateAshmem((key_ + std::to_string(subscriberId_)).c_str(), size); + if (mem == nullptr) { + return; + } + if (!mem->MapReadAndWriteAshmem()) { + mem->CloseAshmem(); + return; + } + if (!mem->WriteToAshmem(vecValue.data(), size, 0)) { + mem->UnmapAshmem(); + mem->CloseAshmem(); + return; + } + AshmemNode node = { mem, true }; + value_ = std::move(node); + } else { + value_ = std::move(std::get(value)); + } +} + +PublishedDataItem::PublishedDataItem(PublishedDataItem &&item) +{ + key_ = std::move(item.key_); + subscriberId_ = std::move(item.subscriberId_); + value_ = std::move(item.value_); + if (item.IsAshmem()) { + item.MoveOutAshmem(); + } +} + +PublishedDataItem &PublishedDataItem::operator=(PublishedDataItem &&item) +{ + key_ = std::move(item.key_); + subscriberId_ = std::move(item.subscriberId_); + value_ = std::move(item.value_); + if (item.IsAshmem()) { + item.MoveOutAshmem(); + } + return *this; +} + +sptr PublishedDataItem::MoveOutAshmem() +{ + if (IsAshmem()) { + AshmemNode &node = std::get(value_); + if (!node.isManaged) { + return nullptr; + } + node.isManaged = false; + return std::move(node.ashmem); + } + return nullptr; +} + +bool PublishedDataItem::IsAshmem() const +{ + return value_.index() == 0; +} + +bool PublishedDataItem::IsString() const +{ + return value_.index() == 1; +} + +void PublishedDataItem::Set(const std::string &value) +{ + value_ = value; +} + +std::variant, std::string> PublishedDataItem::GetData() const +{ + if (IsAshmem()) { + const AshmemNode &node = std::get(value_); + if (node.ashmem != nullptr) { + node.ashmem->MapReadOnlyAshmem(); + uint32_t *data = (uint32_t *)node.ashmem->ReadFromAshmem(node.ashmem->GetAshmemSize(), 0); + if (data == nullptr) { + return std::vector(); + } + return std::vector(data, data + node.ashmem->GetAshmemSize()/sizeof(uint32_t)); + } + return std::vector(); + } else { + return std::get(value_); + } +} + +void PublishedDataItem::SetAshmem(sptr ashmem, bool isManaged) +{ + AshmemNode node = { ashmem, isManaged }; + value_ = std::move(node); +} +} // namespace DataShare +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_api/common/BUILD.gn b/interfaces/inner_api/common/BUILD.gn index 3871042c5ccf336fee49277c48499c13fa6ceb1e..943e660c686035c835c60e559bfa5982b6943eca 100644 --- a/interfaces/inner_api/common/BUILD.gn +++ b/interfaces/inner_api/common/BUILD.gn @@ -52,6 +52,7 @@ ohos_shared_library("datashare_common") { "${datashare_common_native_path}/src/datashare_block_writer_impl.cpp", "${datashare_common_native_path}/src/datashare_itypes_utils.cpp", "${datashare_common_native_path}/src/datashare_result_set.cpp", + "${datashare_common_native_path}/src/datashare_template.cpp", "${datashare_common_native_path}/src/ishared_result_set.cpp", "${datashare_common_native_path}/src/ishared_result_set_proxy.cpp", "${datashare_common_native_path}/src/ishared_result_set_stub.cpp", diff --git a/interfaces/inner_api/common/include/datashare_template.h b/interfaces/inner_api/common/include/datashare_template.h index 79167ffa6329aac38700bf54d94e63341ee5ddc8..75ff2e90b8c6bc53428ef974e35fff3f2841caad 100644 --- a/interfaces/inner_api/common/include/datashare_template.h +++ b/interfaces/inner_api/common/include/datashare_template.h @@ -19,7 +19,6 @@ #include #include #include -#include "hilog/log.h" #include "iremote_object.h" namespace OHOS { @@ -104,114 +103,24 @@ struct PublishedDataItem { PublishedDataItem(){}; PublishedDataItem(const PublishedDataItem &) = delete; PublishedDataItem &operator=(const PublishedDataItem &) = delete; - virtual ~PublishedDataItem() - { - AshmemNode *node = std::get_if(&value_); - if (node != nullptr) { - if (node->isManaged && node->ashmem != nullptr) { - node->ashmem->UnmapAshmem(); - node->ashmem->CloseAshmem(); - } - } - } - PublishedDataItem( - const std::string &key, int64_t subscriberId, std::variant, std::string> value) - : key_(key), subscriberId_(subscriberId) - { - if (value.index() == 0) { - std::vector &vecValue = std::get>(value); - sptr mem = Ashmem::CreateAshmem((key_ + std::to_string(subscriberId_)).c_str(), vecValue.size()); - if (mem == nullptr) { - return; - } - if (!mem->MapReadAndWriteAshmem()) { - mem->CloseAshmem(); - return; - } - if (!mem->WriteToAshmem(vecValue.data(), vecValue.size(), 0)) { - mem->UnmapAshmem(); - mem->CloseAshmem(); - return; - } - AshmemNode node = { mem, true }; - value_ = std::move(node); - } else { - value_ = std::move(std::get(value)); - } - } + virtual ~PublishedDataItem(); + PublishedDataItem(const std::string &key, + int64_t subscriberId, std::variant, std::string> value); + PublishedDataItem(PublishedDataItem &&item); - PublishedDataItem(PublishedDataItem &&item) - { - key_ = std::move(item.key_); - subscriberId_ = std::move(item.subscriberId_); - value_ = std::move(item.value_); - if (item.IsAshmem()) { - item.MoveOutAshmem(); - } - } + PublishedDataItem &operator=(PublishedDataItem &&item); - PublishedDataItem &operator=(PublishedDataItem &&item) - { - key_ = std::move(item.key_); - subscriberId_ = std::move(item.subscriberId_); - value_ = std::move(item.value_); - if (item.IsAshmem()) { - item.MoveOutAshmem(); - } - return *this; - } + bool IsAshmem() const; - inline bool IsAshmem() const - { - return value_.index() == 0; - } + bool IsString() const; - inline bool IsString() const - { - return value_.index() == 1; - } + sptr MoveOutAshmem(); - sptr MoveOutAshmem() - { - if (IsAshmem()) { - AshmemNode &node = std::get(value_); - if (!node.isManaged) { - return nullptr; - } - node.isManaged = false; - return std::move(node.ashmem); - } - return nullptr; - } + void SetAshmem(sptr ashmem, bool isManaged = false); - void SetAshmem(sptr ashmem, bool isManaged = false) - { - AshmemNode node = { ashmem, isManaged }; - value_ = std::move(node); - } + void Set(const std::string &value); - void Set(const std::string &value) - { - value_ = value; - } - - std::variant, std::string> GetData() const - { - if (IsAshmem()) { - const AshmemNode &node = std::get(value_); - if (node.ashmem != nullptr) { - node.ashmem->MapReadOnlyAshmem(); - uint8_t *data = (uint8_t *)node.ashmem->ReadFromAshmem(node.ashmem->GetAshmemSize(), 0); - if (data == nullptr) { - return std::vector(); - } - return std::vector(data, data + node.ashmem->GetAshmemSize()); - } - return std::vector(); - } else { - return std::get(value_); - } - } + std::variant, std::string> GetData() const; }; /** Specifies the published data structure. */ diff --git a/interfaces/inner_api/common/libdatashare_common.map b/interfaces/inner_api/common/libdatashare_common.map index 673d7496974e344af4a067755c9aa7831e5631bf..c6fb083287baf5633e019d20d7ed9caa24bff126 100644 --- a/interfaces/inner_api/common/libdatashare_common.map +++ b/interfaces/inner_api/common/libdatashare_common.map @@ -25,6 +25,7 @@ *DataShareManagerImpl*; *DataShareServiceProxy*; *DataShareKvServiceProxy*; + *PublishedDataItem*; local: *; }; \ No newline at end of file diff --git a/interfaces/inner_api/consumer/libdatashare_consumer.map b/interfaces/inner_api/consumer/libdatashare_consumer.map index ab2ae67dfdbc2c877577d266999df60be9b9e858..198530350c0762dab372ae5d23f5f68280fd9f2a 100644 --- a/interfaces/inner_api/consumer/libdatashare_consumer.map +++ b/interfaces/inner_api/consumer/libdatashare_consumer.map @@ -18,6 +18,7 @@ *DataShareAbsResultSet*; *DataShareSharedResultSet*; *DataShareProxy*; + *PublishedDataItem*; local: *; }; \ No newline at end of file