1e0e9324cSopenharmony_ci/* 2e0e9324cSopenharmony_ci * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3e0e9324cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4e0e9324cSopenharmony_ci * you may not use this file except in compliance with the License. 5e0e9324cSopenharmony_ci * You may obtain a copy of the License at 6e0e9324cSopenharmony_ci * 7e0e9324cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8e0e9324cSopenharmony_ci * 9e0e9324cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10e0e9324cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11e0e9324cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12e0e9324cSopenharmony_ci * See the License for the specific language governing permissions and 13e0e9324cSopenharmony_ci * limitations under the License. 14e0e9324cSopenharmony_ci */ 15e0e9324cSopenharmony_ci 16e0e9324cSopenharmony_ci#ifndef OHOS_SHARING_DATA_H 17e0e9324cSopenharmony_ci#define OHOS_SHARING_DATA_H 18e0e9324cSopenharmony_ci 19e0e9324cSopenharmony_ci#include <inttypes.h> 20e0e9324cSopenharmony_ci#include <memory> 21e0e9324cSopenharmony_ci#include <string> 22e0e9324cSopenharmony_ci#include <unordered_map> 23e0e9324cSopenharmony_ci#include <variant> 24e0e9324cSopenharmony_ci#include <vector> 25e0e9324cSopenharmony_ci 26e0e9324cSopenharmony_cinamespace OHOS { 27e0e9324cSopenharmony_cinamespace Sharing { 28e0e9324cSopenharmony_ci 29e0e9324cSopenharmony_cienum ConfigureError { CONFIGURE_ERROR_NONE, CONFIGURE_ERROR_NOT_FIND }; 30e0e9324cSopenharmony_ci 31e0e9324cSopenharmony_ciclass SharingValue { 32e0e9324cSopenharmony_cipublic: 33e0e9324cSopenharmony_ci using Ptr = std::shared_ptr<SharingValue>; 34e0e9324cSopenharmony_ci 35e0e9324cSopenharmony_ci SharingValue(bool data) : data_(data) {} 36e0e9324cSopenharmony_ci SharingValue(int32_t data) : data_(data) {} 37e0e9324cSopenharmony_ci SharingValue(std::string &data) : data_(data) {} 38e0e9324cSopenharmony_ci SharingValue(std::vector<int32_t> &data) : data_(data) {} 39e0e9324cSopenharmony_ci ~SharingValue() = default; 40e0e9324cSopenharmony_ci 41e0e9324cSopenharmony_ci template <class T> 42e0e9324cSopenharmony_ci bool GetValue(T &value) 43e0e9324cSopenharmony_ci { 44e0e9324cSopenharmony_ci value = std::get<T>(data_); 45e0e9324cSopenharmony_ci return true; 46e0e9324cSopenharmony_ci } 47e0e9324cSopenharmony_ci 48e0e9324cSopenharmony_ci template <class T> 49e0e9324cSopenharmony_ci void SetValue(T &value) 50e0e9324cSopenharmony_ci { 51e0e9324cSopenharmony_ci data_ = value; 52e0e9324cSopenharmony_ci } 53e0e9324cSopenharmony_ci 54e0e9324cSopenharmony_ci enum class SharingIndex : uint32_t { 55e0e9324cSopenharmony_ci VALUE_INDEX_INT32 = 0, 56e0e9324cSopenharmony_ci VALUE_INDEX_BOOL, 57e0e9324cSopenharmony_ci VALUE_INDEX_STRING, 58e0e9324cSopenharmony_ci VALUE_INDEX_VECTOR 59e0e9324cSopenharmony_ci }; 60e0e9324cSopenharmony_ci 61e0e9324cSopenharmony_cipublic: 62e0e9324cSopenharmony_ci void Print(); 63e0e9324cSopenharmony_ci 64e0e9324cSopenharmony_ci bool IsBool(); 65e0e9324cSopenharmony_ci bool IsInt32(); 66e0e9324cSopenharmony_ci bool IsString(); 67e0e9324cSopenharmony_ci bool IsVector(); 68e0e9324cSopenharmony_ci 69e0e9324cSopenharmony_ciprivate: 70e0e9324cSopenharmony_ci std::variant<int32_t, bool, std::string, std::vector<int32_t>> data_; 71e0e9324cSopenharmony_ci}; 72e0e9324cSopenharmony_ci 73e0e9324cSopenharmony_ciclass SharingDataGroupByTag { 74e0e9324cSopenharmony_cipublic: 75e0e9324cSopenharmony_ci using Ptr = std::shared_ptr<SharingDataGroupByTag>; 76e0e9324cSopenharmony_ci using Each = std::function<void(const std::string &key, const SharingValue::Ptr &value)>; 77e0e9324cSopenharmony_ci 78e0e9324cSopenharmony_ci explicit SharingDataGroupByTag(const std::string &tag) : tag_(tag) {} 79e0e9324cSopenharmony_ci 80e0e9324cSopenharmony_ci bool IsTag(const std::string &tag) 81e0e9324cSopenharmony_ci { 82e0e9324cSopenharmony_ci return tag_ == tag; 83e0e9324cSopenharmony_ci } 84e0e9324cSopenharmony_ci 85e0e9324cSopenharmony_cipublic: 86e0e9324cSopenharmony_ci int32_t PutSharingValue(const std::string &key, const SharingValue::Ptr value); 87e0e9324cSopenharmony_ci int32_t PutSharingValues(const std::unordered_map<std::string, SharingValue::Ptr> &values); 88e0e9324cSopenharmony_ci 89e0e9324cSopenharmony_ci int32_t GetSharingValues(std::unordered_map<std::string, SharingValue::Ptr> &values); 90e0e9324cSopenharmony_ci SharingValue::Ptr GetSharingValue(const std::string &key); 91e0e9324cSopenharmony_ci 92e0e9324cSopenharmony_ci void Print(); 93e0e9324cSopenharmony_ci void ForEach(Each each); 94e0e9324cSopenharmony_ci bool HasKey(const std::string &key); 95e0e9324cSopenharmony_ci 96e0e9324cSopenharmony_ciprivate: 97e0e9324cSopenharmony_ci std::string tag_; 98e0e9324cSopenharmony_ci std::unordered_map<std::string, SharingValue::Ptr> datas_; 99e0e9324cSopenharmony_ci}; 100e0e9324cSopenharmony_ci 101e0e9324cSopenharmony_ciclass SharingDataGroupByModule { 102e0e9324cSopenharmony_cipublic: 103e0e9324cSopenharmony_ci using Ptr = std::shared_ptr<SharingDataGroupByModule>; 104e0e9324cSopenharmony_ci using Each = std::function<void(const std::string &tag, const SharingDataGroupByTag::Ptr &value)>; 105e0e9324cSopenharmony_ci 106e0e9324cSopenharmony_ci explicit SharingDataGroupByModule(const std::string &mudule) : mudule_(mudule) {} 107e0e9324cSopenharmony_ci 108e0e9324cSopenharmony_ci bool IsModule(const std::string &mudule) 109e0e9324cSopenharmony_ci { 110e0e9324cSopenharmony_ci return mudule_ == mudule; 111e0e9324cSopenharmony_ci } 112e0e9324cSopenharmony_ci 113e0e9324cSopenharmony_cipublic: 114e0e9324cSopenharmony_ci int32_t PutSharingValues(const std::string &tag, const SharingDataGroupByTag::Ptr &value); 115e0e9324cSopenharmony_ci int32_t PutSharingValue(const std::string &tag, const std::string &key, const SharingValue::Ptr value); 116e0e9324cSopenharmony_ci int32_t PutSharingValues(const std::string &tag, const std::unordered_map<std::string, SharingValue::Ptr> &values); 117e0e9324cSopenharmony_ci 118e0e9324cSopenharmony_ci int32_t GetSharingValues(const std::string &tag, SharingDataGroupByTag::Ptr &value); 119e0e9324cSopenharmony_ci int32_t GetSharingValues(const std::string &tag, std::unordered_map<std::string, SharingValue::Ptr> &values); 120e0e9324cSopenharmony_ci SharingValue::Ptr GetSharingValue(const std::string &tag, const std::string &key); 121e0e9324cSopenharmony_ci 122e0e9324cSopenharmony_ci void Print(); 123e0e9324cSopenharmony_ci void ForEach(Each each); 124e0e9324cSopenharmony_ci bool HasTag(const std::string &tag); 125e0e9324cSopenharmony_ci bool HasKey(const std::string &tag, const std::string &key); 126e0e9324cSopenharmony_ci 127e0e9324cSopenharmony_ciprivate: 128e0e9324cSopenharmony_ci std::string mudule_; 129e0e9324cSopenharmony_ci std::unordered_map<std::string, SharingDataGroupByTag::Ptr> datass_; 130e0e9324cSopenharmony_ci}; 131e0e9324cSopenharmony_ci 132e0e9324cSopenharmony_ciclass SharingData { 133e0e9324cSopenharmony_cipublic: 134e0e9324cSopenharmony_ci using Ptr = std::shared_ptr<SharingData>; 135e0e9324cSopenharmony_ci using Each = std::function<void(const std::string &module, const SharingDataGroupByModule::Ptr &value)>; 136e0e9324cSopenharmony_ci 137e0e9324cSopenharmony_ci SharingData() = default; 138e0e9324cSopenharmony_ci ~SharingData() = default; 139e0e9324cSopenharmony_ci 140e0e9324cSopenharmony_ci int32_t PutSharingValues(const SharingDataGroupByModule::Ptr &values, const std::string &module); 141e0e9324cSopenharmony_ci int32_t PutSharingValue(const std::string &key, const SharingValue::Ptr value, const std::string &module, 142e0e9324cSopenharmony_ci const std::string &tag); 143e0e9324cSopenharmony_ci int32_t PutSharingValues(const std::unordered_map<std::string, SharingValue::Ptr> &values, 144e0e9324cSopenharmony_ci const std::string &module, const std::string &tag); 145e0e9324cSopenharmony_ci 146e0e9324cSopenharmony_ci int32_t GetSharingValues(SharingDataGroupByModule::Ptr &values, const std::string &module); 147e0e9324cSopenharmony_ci int32_t GetSharingValues(SharingDataGroupByTag::Ptr &value, const std::string &module, const std::string &tag); 148e0e9324cSopenharmony_ci int32_t GetSharingValues(std::unordered_map<std::string, SharingValue::Ptr> &values, const std::string &module, 149e0e9324cSopenharmony_ci const std::string &tag); 150e0e9324cSopenharmony_ci SharingValue::Ptr GetSharingValue(const std::string &key, const std::string &module, const std::string &tag); 151e0e9324cSopenharmony_ci 152e0e9324cSopenharmony_ci void Print(); 153e0e9324cSopenharmony_ci void ForEach(Each each); 154e0e9324cSopenharmony_ci bool HasModule(const std::string &module); 155e0e9324cSopenharmony_ci bool HasTag(const std::string &module, const std::string &tag); 156e0e9324cSopenharmony_ci bool HasKey(const std::string &key, const std::string &module, const std::string &tag); 157e0e9324cSopenharmony_ci 158e0e9324cSopenharmony_ciprivate: 159e0e9324cSopenharmony_ci std::unordered_map<std::string, SharingDataGroupByModule::Ptr> datass_; 160e0e9324cSopenharmony_ci}; 161e0e9324cSopenharmony_ci 162e0e9324cSopenharmony_ci} // namespace Sharing 163e0e9324cSopenharmony_ci} // namespace OHOS 164e0e9324cSopenharmony_ci#endif