• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_TLV_COUNTABLE_H
17 #define DISTRIBUTEDDATAMGR_PASTEBOARD_TLV_COUNTABLE_H
18 
19 #include "tlv_buffer.h"
20 #include "tlv_utils.h"
21 
22 namespace OHOS::MiscServices {
23 
24 class TLVCountable {
25 public:
26     virtual size_t CountTLV() const = 0;
27 
Count(bool value)28     static inline size_t Count(bool value)
29     {
30         return sizeof(value) + sizeof(TLVHead);
31     }
32 
Count(int8_t value)33     static inline size_t Count(int8_t value)
34     {
35         return sizeof(value) + sizeof(TLVHead);
36     }
37 
Count(int16_t value)38     static inline size_t Count(int16_t value)
39     {
40         return sizeof(value) + sizeof(TLVHead);
41     }
42 
Count(int32_t value)43     static inline size_t Count(int32_t value)
44     {
45         return sizeof(value) + sizeof(TLVHead);
46     }
47 
Count(double value)48     static inline size_t Count(double value)
49     {
50         return sizeof(value) + sizeof(TLVHead);
51     }
52 
Count(int64_t value)53     static inline size_t Count(int64_t value)
54     {
55         return sizeof(value) + sizeof(TLVHead);
56     }
57 
Count(uint32_t value)58     static inline size_t Count(uint32_t value)
59     {
60         return sizeof(value) + sizeof(TLVHead);
61     }
62 
Count(const std::string & value)63     static inline size_t Count(const std::string &value)
64     {
65         return value.size() + sizeof(TLVHead);
66     }
67 
Count(const RawMem & value)68     static inline size_t Count(const RawMem &value)
69     {
70         return value.bufferLen + sizeof(TLVHead);
71     }
72 
Count(const TLVCountable & value)73     static inline size_t Count(const TLVCountable &value)
74     {
75         return value.CountTLV() + sizeof(TLVHead);
76     }
77 
78     template<typename T>
Count(const std::shared_ptr<T> & value)79     static inline size_t Count(const std::shared_ptr<T> &value)
80     {
81         if (value == nullptr) {
82             return 0;
83         }
84         return Count(*value);
85     }
86 
87     template<typename T>
Count(const std::vector<T> & value)88     static inline size_t Count(const std::vector<T> &value)
89     {
90         size_t expectSize = sizeof(TLVHead);
91         for (const auto &item : value) {
92             expectSize += Count(item);
93         }
94         return expectSize;
95     }
96 
Count(const std::vector<uint8_t> & value)97     static inline size_t Count(const std::vector<uint8_t> &value)
98     {
99         size_t expectSize = sizeof(TLVHead);
100         expectSize += value.size();
101         return expectSize;
102     }
103 
Count(const std::map<std::string,std::vector<uint8_t>> & value)104     static inline size_t Count(const std::map<std::string, std::vector<uint8_t>> &value)
105     {
106         size_t expectSize = sizeof(TLVHead);
107         for (const auto &item : value) {
108             expectSize += Count(item.first);
109             expectSize += Count(item.second);
110         }
111         return expectSize;
112     }
113 
Count(const Details & value)114     static inline size_t Count(const Details &value)
115     {
116         size_t expectSize = sizeof(TLVHead);
117         for (const auto &item : value) {
118             expectSize += Count(item.first);
119             expectSize += Count(item.second);
120         }
121         return expectSize;
122     }
123 
Count(const std::shared_ptr<AAFwk::Want> & value)124     static inline size_t Count(const std::shared_ptr<AAFwk::Want> &value)
125     {
126         if (value == nullptr) {
127             return 0;
128         }
129         size_t expectSize = sizeof(TLVHead);
130         return expectSize + Count(TLVUtils::Parcelable2Raw(value.get()));
131     }
132 
Count(const std::shared_ptr<Media::PixelMap> value)133     static inline size_t Count(const std::shared_ptr<Media::PixelMap> value)
134     {
135         if (value == nullptr) {
136             return 0;
137         }
138         size_t expectSize = sizeof(TLVHead);
139         return expectSize + Count(TLVUtils::PixelMap2Vector(value));
140     }
141 
Count(const std::shared_ptr<Object> & value)142     static inline size_t Count(const std::shared_ptr<Object> &value)
143     {
144         if (value == nullptr) {
145             return 0;
146         }
147         size_t expectSize = sizeof(TLVHead);
148         for (auto &item : value->value_) {
149             expectSize += Count(item.first);
150             expectSize += Count(item.second);
151         }
152         return expectSize;
153     }
154 
Count(const std::monostate & value)155     static inline size_t Count(const std::monostate &value)
156     {
157         (void)value;
158         return sizeof(TLVHead);
159     }
160 
Count(const void * value)161     static inline size_t Count(const void *value)
162     {
163         (void)value;
164         return sizeof(TLVHead);
165     }
166 
167     template<typename _InTp>
CountVariant(uint32_t step,const _InTp & input)168     static inline size_t CountVariant(uint32_t step, const _InTp &input)
169     {
170         (void)step;
171         (void)input;
172         return 0;
173     }
174 
175     template<typename _InTp, typename _First, typename... _Rest>
CountVariant(uint32_t step,const _InTp & input)176     static inline size_t CountVariant(uint32_t step, const _InTp &input)
177     {
178         if (step == input.index()) {
179             return Count(step) + Count(std::get<_First>(input));
180         }
181         return CountVariant<_InTp, _Rest...>(step + 1, input);
182     }
183 
184     template<typename... _Types>
Count(const std::variant<_Types...> & input)185     static inline size_t Count(const std::variant<_Types...> &input)
186     {
187         size_t expectSize = sizeof(TLVHead);
188         return expectSize + CountVariant<decltype(input), _Types...>(0, input);
189     }
190 };
191 } // namespace OHOS::MiscServices
192 #endif // DISTRIBUTEDDATAMGR_PASTEBOARD_TLV_COUNTABLE_H
193