• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 OHOS_SHARING_DATA_H
17 #define OHOS_SHARING_DATA_H
18 
19 #include <inttypes.h>
20 #include <memory>
21 #include <string>
22 #include <unordered_map>
23 #include <variant>
24 #include <vector>
25 
26 namespace OHOS {
27 namespace Sharing {
28 
29 enum ConfigureError { CONFIGURE_ERROR_NONE, CONFIGURE_ERROR_NOT_FIND };
30 
31 class SharingValue {
32 public:
33     using Ptr = std::shared_ptr<SharingValue>;
34 
SharingValue(bool data)35     SharingValue(bool data) : data_(data) {}
SharingValue(int32_t data)36     SharingValue(int32_t data) : data_(data) {}
SharingValue(std::string & data)37     SharingValue(std::string &data) : data_(data) {}
SharingValue(std::vector<int32_t> & data)38     SharingValue(std::vector<int32_t> &data) : data_(data) {}
39     ~SharingValue() = default;
40 
41     template <class T>
GetValue(T & value)42     bool GetValue(T &value)
43     {
44         value = std::get<T>(data_);
45         return true;
46     }
47 
48     template <class T>
SetValue(T & value)49     void SetValue(T &value)
50     {
51         data_ = value;
52     }
53 
54     enum class SharingIndex : uint32_t {
55         VALUE_INDEX_INT32 = 0,
56         VALUE_INDEX_BOOL,
57         VALUE_INDEX_STRING,
58         VALUE_INDEX_VECTOR
59     };
60 
61 public:
62     void Print();
63 
64     bool IsBool();
65     bool IsInt32();
66     bool IsString();
67     bool IsVector();
68 
69 private:
70     std::variant<int32_t, bool, std::string, std::vector<int32_t>> data_;
71 };
72 
73 class SharingDataGroupByTag {
74 public:
75     using Ptr = std::shared_ptr<SharingDataGroupByTag>;
76     using Each = std::function<void(const std::string &key, const SharingValue::Ptr &value)>;
77 
SharingDataGroupByTag(const std::string & tag)78     explicit SharingDataGroupByTag(const std::string &tag) : tag_(tag) {}
79 
IsTag(const std::string & tag)80     bool IsTag(const std::string &tag)
81     {
82         return tag_ == tag;
83     }
84 
85 public:
86     int32_t PutSharingValue(const std::string &key, const SharingValue::Ptr value);
87     int32_t PutSharingValues(const std::unordered_map<std::string, SharingValue::Ptr> &values);
88 
89     int32_t GetSharingValues(std::unordered_map<std::string, SharingValue::Ptr> &values);
90     SharingValue::Ptr GetSharingValue(const std::string &key);
91 
92     void Print();
93     void ForEach(Each each);
94     bool HasKey(const std::string &key);
95 
96 private:
97     std::string tag_;
98     std::unordered_map<std::string, SharingValue::Ptr> datas_;
99 };
100 
101 class SharingDataGroupByModule {
102 public:
103     using Ptr = std::shared_ptr<SharingDataGroupByModule>;
104     using Each = std::function<void(const std::string &tag, const SharingDataGroupByTag::Ptr &value)>;
105 
SharingDataGroupByModule(const std::string & mudule)106     explicit SharingDataGroupByModule(const std::string &mudule) : mudule_(mudule) {}
107 
IsModule(const std::string & mudule)108     bool IsModule(const std::string &mudule)
109     {
110         return mudule_ == mudule;
111     }
112 
113 public:
114     int32_t PutSharingValues(const std::string &tag, const SharingDataGroupByTag::Ptr &value);
115     int32_t PutSharingValue(const std::string &tag, const std::string &key, const SharingValue::Ptr value);
116     int32_t PutSharingValues(const std::string &tag, const std::unordered_map<std::string, SharingValue::Ptr> &values);
117 
118     int32_t GetSharingValues(const std::string &tag, SharingDataGroupByTag::Ptr &value);
119     int32_t GetSharingValues(const std::string &tag, std::unordered_map<std::string, SharingValue::Ptr> &values);
120     SharingValue::Ptr GetSharingValue(const std::string &tag, const std::string &key);
121 
122     void Print();
123     void ForEach(Each each);
124     bool HasTag(const std::string &tag);
125     bool HasKey(const std::string &tag, const std::string &key);
126 
127 private:
128     std::string mudule_;
129     std::unordered_map<std::string, SharingDataGroupByTag::Ptr> datass_;
130 };
131 
132 class SharingData {
133 public:
134     using Ptr = std::shared_ptr<SharingData>;
135     using Each = std::function<void(const std::string &module, const SharingDataGroupByModule::Ptr &value)>;
136 
137     SharingData() = default;
138     ~SharingData() = default;
139 
140     int32_t PutSharingValues(const SharingDataGroupByModule::Ptr &values, const std::string &module);
141     int32_t PutSharingValue(const std::string &key, const SharingValue::Ptr value, const std::string &module,
142                             const std::string &tag);
143     int32_t PutSharingValues(const std::unordered_map<std::string, SharingValue::Ptr> &values,
144                              const std::string &module, const std::string &tag);
145 
146     int32_t GetSharingValues(SharingDataGroupByModule::Ptr &values, const std::string &module);
147     int32_t GetSharingValues(SharingDataGroupByTag::Ptr &value, const std::string &module, const std::string &tag);
148     int32_t GetSharingValues(std::unordered_map<std::string, SharingValue::Ptr> &values, const std::string &module,
149                              const std::string &tag);
150     SharingValue::Ptr GetSharingValue(const std::string &key, const std::string &module, const std::string &tag);
151 
152     void Print();
153     void ForEach(Each each);
154     bool HasModule(const std::string &module);
155     bool HasTag(const std::string &module, const std::string &tag);
156     bool HasKey(const std::string &key, const std::string &module, const std::string &tag);
157 
158 private:
159     std::unordered_map<std::string, SharingDataGroupByModule::Ptr> datass_;
160 };
161 
162 } // namespace Sharing
163 } // namespace OHOS
164 #endif