• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #include "attr_data.h"
17 #include <cstdint>
18 #include <memory>
19 #include <utility>
20 #include <functional>
21 #include "__tree"
22 #include "cstdint"
23 #include "hilog/log_c.h"
24 #include "hilog/log_cpp.h"
25 #include "iosfwd"
26 #include "iterator"
27 #include "log_tags.h"
28 #include "new"
29 #include "plugin_errors.h"
30 #ifndef _WIN32
31 #include "securec.h"
32 #else
33 #include "memory.h"
34 #endif
35 #include "set"
36 #include "string"
37 #include "type_traits"
38 
39 namespace OHOS {
40 namespace MultimediaPlugin {
41 using std::set;
42 using std::string;
43 using namespace OHOS::HiviewDFX;
44 
45 static constexpr HiLogLabel LABEL = { LOG_CORE, LOG_TAG_DOMAIN_ID_PLUGIN, "AttrData" };
46 
AttrData()47 AttrData::AttrData() : type_(AttrDataType::ATTR_DATA_NULL)
48 {}
49 
AttrData(bool value)50 AttrData::AttrData(bool value) : type_(AttrDataType::ATTR_DATA_BOOL)
51 {
52     value_.boolValue = value;
53 }
54 
AttrData(uint32_t value)55 AttrData::AttrData(uint32_t value) : type_(AttrDataType::ATTR_DATA_UINT32)
56 {
57     value_.uint32Value = value;
58 }
59 
AttrData(const string & value)60 AttrData::AttrData(const string &value) : type_(AttrDataType::ATTR_DATA_STRING)
61 {
62     value_.stringValue = new (std::nothrow) string(value);
63     if (value_.stringValue == nullptr) {
64         HiLog::Error(LABEL, "AttrData: alloc stringValue result null!");
65         type_ = AttrDataType::ATTR_DATA_NULL;
66     }
67 }
68 
AttrData(string && value)69 AttrData::AttrData(string &&value) : type_(AttrDataType::ATTR_DATA_STRING)
70 {
71     value_.stringValue = new (std::nothrow) string(std::move(value));
72     if (value_.stringValue == nullptr) {
73         HiLog::Error(LABEL, "AttrData: alloc stringValue result null!");
74         type_ = AttrDataType::ATTR_DATA_NULL;
75     }
76 }
77 
AttrData(uint32_t lowerBound,uint32_t upperBound)78 AttrData::AttrData(uint32_t lowerBound, uint32_t upperBound) : type_(AttrDataType::ATTR_DATA_UINT32_RANGE)
79 {
80     if (lowerBound > upperBound) {
81         type_ = AttrDataType::ATTR_DATA_NULL;
82     }
83 
84     value_.uint32Rang[LOWER_BOUND_INDEX] = lowerBound;
85     value_.uint32Rang[UPPER_BOUND_INDEX] = upperBound;
86 }
87 
AttrData(const AttrData & data)88 AttrData::AttrData(const AttrData &data)
89 {
90     switch (data.type_) {
91         case AttrDataType::ATTR_DATA_BOOL: {
92             value_.boolValue = data.value_.boolValue;
93             type_ = AttrDataType::ATTR_DATA_BOOL;
94             break;
95         }
96         case AttrDataType::ATTR_DATA_UINT32: {
97             value_.uint32Value = data.value_.uint32Value;
98             type_ = AttrDataType::ATTR_DATA_UINT32;
99             break;
100         }
101         case AttrDataType::ATTR_DATA_STRING: {
102             (void)InitStringAttrData(data);
103             break;
104         }
105         case AttrDataType::ATTR_DATA_UINT32_SET: {
106             (void)InitUint32SetAttrData(data);
107             break;
108         }
109         case AttrDataType::ATTR_DATA_STRING_SET: {
110             (void)InitStringSetAttrData(data);
111             break;
112         }
113         case AttrDataType::ATTR_DATA_UINT32_RANGE: {
114             value_.uint32Rang[LOWER_BOUND_INDEX] = data.value_.uint32Rang[LOWER_BOUND_INDEX];
115             value_.uint32Rang[UPPER_BOUND_INDEX] = data.value_.uint32Rang[UPPER_BOUND_INDEX];
116             type_ = AttrDataType::ATTR_DATA_UINT32_RANGE;
117             break;
118         }
119         default: {
120             HiLog::Debug(LABEL, "AttrData: null or unexpected type in copy constructor: %{public}d.", data.type_);
121             type_ = AttrDataType::ATTR_DATA_NULL;
122         }
123     }
124 }
125 
AttrData(AttrData && data)126 AttrData::AttrData(AttrData &&data) noexcept
127 {
128     if (memcpy_s(&value_, sizeof(value_), &data.value_, sizeof(data.value_)) == EOK) {
129         type_ = data.type_;
130         data.type_ = AttrDataType::ATTR_DATA_NULL;
131     } else {
132         type_ = AttrDataType::ATTR_DATA_NULL;
133         HiLog::Error(LABEL, "memcpy error in assignment operator!");
134     }
135 }
136 
~AttrData()137 AttrData::~AttrData()
138 {
139     ClearData();
140 }
141 
operator =(const AttrData & data)142 AttrData &AttrData::operator=(const AttrData &data)
143 {
144     // make a copy, avoid self-assignment problems.
145     AttrData temp(data);
146     ClearData();
147     if (memcpy_s(&value_, sizeof(value_), &temp.value_, sizeof(temp.value_)) == 0) {
148         type_ = temp.type_;
149         temp.type_ = AttrDataType::ATTR_DATA_NULL;
150     } else {
151         type_ = AttrDataType::ATTR_DATA_NULL;
152         HiLog::Error(LABEL, "memcpy error in assignment operator!");
153     }
154 
155     return *this;
156 }
157 
operator =(AttrData && data)158 AttrData &AttrData::operator=(AttrData &&data) noexcept
159 {
160     // case if self-assignment.
161     if (&data == this) {
162         return *this;
163     }
164 
165     ClearData();
166     if (memcpy_s(&value_, sizeof(value_), &data.value_, sizeof(data.value_)) == EOK) {
167         type_ = data.type_;
168         data.type_ = AttrDataType::ATTR_DATA_NULL;
169     } else {
170         type_ = AttrDataType::ATTR_DATA_NULL;
171         HiLog::Error(LABEL, "memcpy error in assignment operator!");
172     }
173 
174     return *this;
175 }
176 
SetData(bool value)177 void AttrData::SetData(bool value)
178 {
179     ClearData();
180     value_.boolValue = value;
181     type_ = AttrDataType::ATTR_DATA_BOOL;
182 }
183 
SetData(uint32_t value)184 void AttrData::SetData(uint32_t value)
185 {
186     ClearData();
187     value_.uint32Value = value;
188     type_ = AttrDataType::ATTR_DATA_UINT32;
189 }
190 
SetData(const string & value)191 uint32_t AttrData::SetData(const string &value)
192 {
193     if (type_ == AttrDataType::ATTR_DATA_STRING) {
194         *(value_.stringValue) = value;
195         return SUCCESS;
196     }
197 
198     string *newValue = new (std::nothrow) string(value);
199     if (newValue == nullptr) {
200         HiLog::Error(LABEL, "SetData: alloc string result null!");
201         return ERR_INTERNAL;
202     }
203 
204     ClearData();
205     value_.stringValue = newValue;
206     type_ = AttrDataType::ATTR_DATA_STRING;
207     return SUCCESS;
208 }
209 
SetData(string && value)210 uint32_t AttrData::SetData(string &&value)
211 {
212     if (type_ == AttrDataType::ATTR_DATA_STRING) {
213         *(value_.stringValue) = std::move(value);
214         return SUCCESS;
215     }
216 
217     string *newValue = new (std::nothrow) string(std::move(value));
218     if (newValue == nullptr) {
219         HiLog::Error(LABEL, "SetData: alloc string result null!");
220         return ERR_INTERNAL;
221     }
222 
223     ClearData();
224     value_.stringValue = newValue;
225     type_ = AttrDataType::ATTR_DATA_STRING;
226     return SUCCESS;
227 }
228 
SetData(uint32_t lowerBound,uint32_t upperBound)229 uint32_t AttrData::SetData(uint32_t lowerBound, uint32_t upperBound)
230 {
231     if (lowerBound > upperBound) {
232         HiLog::Error(LABEL, "SetData: lowerBound is upper than upperBound, lower: %{public}u, upper: %{public}u.",
233                      lowerBound, upperBound);
234         return ERR_INVALID_PARAMETER;
235     }
236 
237     ClearData();
238     value_.uint32Rang[LOWER_BOUND_INDEX] = lowerBound;
239     value_.uint32Rang[UPPER_BOUND_INDEX] = upperBound;
240     type_ = AttrDataType::ATTR_DATA_UINT32_RANGE;
241     return SUCCESS;
242 }
243 
ClearData()244 void AttrData::ClearData()
245 {
246     switch (type_) {
247         case AttrDataType::ATTR_DATA_STRING: {
248             if (value_.stringValue != nullptr) {
249                 delete value_.stringValue;
250                 value_.stringValue = nullptr;
251             }
252             break;
253         }
254         case AttrDataType::ATTR_DATA_UINT32_SET: {
255             if (value_.uint32Set != nullptr) {
256                 delete value_.uint32Set;
257                 value_.uint32Set = nullptr;
258             }
259             break;
260         }
261         case AttrDataType::ATTR_DATA_STRING_SET: {
262             if (value_.stringSet != nullptr) {
263                 delete value_.stringSet;
264                 value_.stringSet = nullptr;
265             }
266             break;
267         }
268         default: {
269             // do nothing
270             HiLog::Debug(LABEL, "ClearData: do nothing for type %{public}d.", type_);
271             break;
272         }
273     }
274 
275     type_ = AttrDataType::ATTR_DATA_NULL;
276 }
277 
InsertSet(uint32_t value)278 uint32_t AttrData::InsertSet(uint32_t value)
279 {
280     if (type_ == AttrDataType::ATTR_DATA_NULL) {
281         value_.uint32Set = new (std::nothrow) set<uint32_t>({ value });
282         if (value_.uint32Set == nullptr) {
283             HiLog::Error(LABEL, "InsertSet: alloc uint32Set result null!");
284             return ERR_INTERNAL;
285         }
286 
287         type_ = AttrDataType::ATTR_DATA_UINT32_SET;
288         return SUCCESS;
289     }
290 
291     if (type_ != AttrDataType::ATTR_DATA_UINT32_SET) {
292         HiLog::Error(LABEL, "InsertSet: AttrData type is not uint32Set or null, type: %{public}d.", type_);
293         return ERR_UNSUPPORTED;
294     }
295 
296     auto result = value_.uint32Set->insert(value);
297     if (!result.second) {
298         HiLog::Error(LABEL, "InsertSet: set insert error!");
299         return ERR_GENERAL;
300     }
301 
302     return SUCCESS;
303 }
304 
InsertSet(const string & value)305 uint32_t AttrData::InsertSet(const string &value)
306 {
307     if (type_ == AttrDataType::ATTR_DATA_NULL) {
308         value_.stringSet = new (std::nothrow) set<string>({ value });
309         if (value_.stringSet == nullptr) {
310             HiLog::Error(LABEL, "InsertSet: alloc stringSet result null!");
311             return ERR_INTERNAL;
312         }
313 
314         type_ = AttrDataType::ATTR_DATA_STRING_SET;
315         return SUCCESS;
316     }
317 
318     if (type_ != AttrDataType::ATTR_DATA_STRING_SET) {
319         HiLog::Error(LABEL, "InsertSet: AttrData type is not stringSet or null, type: %{public}d.", type_);
320         return ERR_UNSUPPORTED;
321     }
322 
323     auto result = value_.stringSet->insert(value);
324     if (!result.second) {
325         HiLog::Error(LABEL, "InsertSet: set insert error!");
326         return ERR_INTERNAL;
327     }
328 
329     return SUCCESS;
330 }
331 
InsertSet(string && value)332 uint32_t AttrData::InsertSet(string &&value)
333 {
334     if (type_ == AttrDataType::ATTR_DATA_NULL) {
335         value_.stringSet = new (std::nothrow) set<string>;
336         if (value_.stringSet == nullptr) {
337             HiLog::Error(LABEL, "InsertSet: alloc stringSet result null!");
338             return ERR_INTERNAL;
339         }
340 
341         auto result = value_.stringSet->insert(std::move(value));
342         if (!result.second) {
343             delete value_.stringSet;
344             value_.stringSet = nullptr;
345             HiLog::Error(LABEL, "InsertSet: set insert error!");
346             return ERR_INTERNAL;
347         }
348 
349         type_ = AttrDataType::ATTR_DATA_STRING_SET;
350         return SUCCESS;
351     }
352 
353     if (type_ != AttrDataType::ATTR_DATA_STRING_SET) {
354         HiLog::Error(LABEL, "InsertSet: AttrData type is not stringSet or null, type: %{public}d.", type_);
355         return ERR_UNSUPPORTED;
356     }
357 
358     auto result = value_.stringSet->insert(std::move(value));
359     if (!result.second) {
360         HiLog::Error(LABEL, "InsertSet: set insert error!");
361         return ERR_INTERNAL;
362     }
363 
364     return SUCCESS;
365 }
366 
InRange(bool value) const367 bool AttrData::InRange(bool value) const
368 {
369     if (type_ != AttrDataType::ATTR_DATA_BOOL) {
370         HiLog::Error(LABEL, "InRange: comparison of bool type with non-bool type: %{public}d.", type_);
371         return false;
372     }
373 
374     return value == value_.boolValue;
375 }
376 
InRange(uint32_t value) const377 bool AttrData::InRange(uint32_t value) const
378 {
379     switch (type_) {
380         case AttrDataType::ATTR_DATA_UINT32: {
381             return value == value_.uint32Value;
382         }
383         case AttrDataType::ATTR_DATA_UINT32_SET: {
384             return value_.uint32Set->find(value) != value_.uint32Set->end();
385         }
386         case AttrDataType::ATTR_DATA_UINT32_RANGE: {
387             return InRangeUint32Range(value);
388         }
389         default: {
390             HiLog::Error(LABEL, "InRange: comparison of uint32 type with non-uint32 type: %{public}d.", type_);
391             return false;
392         }
393     }
394 }
395 
InRange(const string & value) const396 bool AttrData::InRange(const string &value) const
397 {
398     switch (type_) {
399         case AttrDataType::ATTR_DATA_STRING: {
400             return value == *(value_.stringValue);
401         }
402         case AttrDataType::ATTR_DATA_STRING_SET: {
403             return value_.stringSet->find(value) != value_.stringSet->end();
404         }
405         default: {
406             HiLog::Error(LABEL, "InRange: comparison of string type with non-string type: %{public}d.", type_);
407             return false;
408         }
409     }
410 }
411 
InRange(const AttrData & data) const412 bool AttrData::InRange(const AttrData &data) const
413 {
414     switch (data.type_) {
415         case AttrDataType::ATTR_DATA_NULL: {
416             return type_ == AttrDataType::ATTR_DATA_NULL;
417         }
418         case AttrDataType::ATTR_DATA_BOOL: {
419             return InRange(data.value_.boolValue);
420         }
421         case AttrDataType::ATTR_DATA_UINT32: {
422             return InRange(data.value_.uint32Value);
423         }
424         case AttrDataType::ATTR_DATA_STRING: {
425             return InRange(*(data.value_.stringValue));
426         }
427         case AttrDataType::ATTR_DATA_UINT32_SET: {
428             return InRange(*(data.value_.uint32Set));
429         }
430         case AttrDataType::ATTR_DATA_STRING_SET: {
431             return InRange(*(data.value_.stringSet));
432         }
433         case AttrDataType::ATTR_DATA_UINT32_RANGE: {
434             return InRange(data.value_.uint32Rang);
435         }
436         default: {
437             HiLog::Error(LABEL, "InRange: unexpected AttrData type: %{public}d.", data.type_);
438             return false;
439         }
440     }
441 }
442 
GetType() const443 AttrDataType AttrData::GetType() const
444 {
445     return type_;
446 }
447 
GetMinValue(uint32_t & value) const448 uint32_t AttrData::GetMinValue(uint32_t &value) const
449 {
450     switch (type_) {
451         case AttrDataType::ATTR_DATA_UINT32: {
452             value = value_.uint32Value;
453             return SUCCESS;
454         }
455         case AttrDataType::ATTR_DATA_UINT32_SET: {
456             auto iter = value_.uint32Set->begin();
457             if (iter == value_.uint32Set->end()) {
458                 HiLog::Error(LABEL, "GetMinValue: uint32Set is empty.");
459                 return ERR_GENERAL;
460             }
461             value = *iter;
462             return SUCCESS;
463         }
464         case AttrDataType::ATTR_DATA_UINT32_RANGE: {
465             value = value_.uint32Rang[LOWER_BOUND_INDEX];
466             return SUCCESS;
467         }
468         default: {
469             HiLog::Error(LABEL, "GetMinValue: invalid data type for uint32: %{public}d.", type_);
470             return ERR_INVALID_PARAMETER;
471         }
472     }
473 }
474 
GetMaxValue(uint32_t & value) const475 uint32_t AttrData::GetMaxValue(uint32_t &value) const
476 {
477     switch (type_) {
478         case AttrDataType::ATTR_DATA_UINT32: {
479             value = value_.uint32Value;
480             return SUCCESS;
481         }
482         case AttrDataType::ATTR_DATA_UINT32_SET: {
483             auto iter = value_.uint32Set->rbegin();
484             if (iter == value_.uint32Set->rend()) {
485                 HiLog::Error(LABEL, "GetMaxValue: GetMaxValue: uint32Set is empty.");
486                 return ERR_GENERAL;
487             }
488 
489             value = *iter;
490             return SUCCESS;
491         }
492         case AttrDataType::ATTR_DATA_UINT32_RANGE: {
493             value = value_.uint32Rang[UPPER_BOUND_INDEX];
494             return SUCCESS;
495         }
496         default: {
497             HiLog::Error(LABEL, "GetMaxValue: invalid data type for uint32: %{public}d.", type_);
498             return ERR_INVALID_PARAMETER;
499         }
500     }
501 }
502 
GetMinValue(const string * & value) const503 uint32_t AttrData::GetMinValue(const string *&value) const
504 {
505     switch (type_) {
506         case AttrDataType::ATTR_DATA_STRING: {
507             value = value_.stringValue;
508             return SUCCESS;
509         }
510         case AttrDataType::ATTR_DATA_STRING_SET: {
511             auto iter = value_.stringSet->begin();
512             if (iter == value_.stringSet->end()) {
513                 HiLog::Error(LABEL, "GetMinValue: stringSet is empty.");
514                 return ERR_GENERAL;
515             }
516 
517             value = (&(*iter));
518             return SUCCESS;
519         }
520         default: {
521             HiLog::Error(LABEL, "GetMinValue: invalid data type for string: %{public}d.", type_);
522             return ERR_INVALID_PARAMETER;
523         }
524     }
525 }
526 
GetMaxValue(const string * & value) const527 uint32_t AttrData::GetMaxValue(const string *&value) const
528 {
529     switch (type_) {
530         case AttrDataType::ATTR_DATA_STRING: {
531             value = value_.stringValue;
532             return SUCCESS;
533         }
534         case AttrDataType::ATTR_DATA_STRING_SET: {
535             auto iter = value_.stringSet->rbegin();
536             if (iter == value_.stringSet->rend()) {
537                 HiLog::Error(LABEL, "GetMaxValue: stringSet is empty.");
538                 return ERR_GENERAL;
539             }
540 
541             value = (&(*iter));
542             return SUCCESS;
543         }
544         default: {
545             HiLog::Error(LABEL, "GetMaxValue: invalid data type for string: %{public}d.", type_);
546             return ERR_INVALID_PARAMETER;
547         }
548     }
549 }
550 
GetValue(bool & value) const551 uint32_t AttrData::GetValue(bool &value) const
552 {
553     if (type_ != AttrDataType::ATTR_DATA_BOOL) {
554         HiLog::Error(LABEL, "Get uint32 value: not a bool AttrData type: %{public}d.", type_);
555         return ERR_INVALID_PARAMETER;
556     }
557 
558     value = value_.boolValue;
559     return SUCCESS;
560 }
561 
GetValue(uint32_t & value) const562 uint32_t AttrData::GetValue(uint32_t &value) const
563 {
564     if (type_ != AttrDataType::ATTR_DATA_UINT32) {
565         HiLog::Error(LABEL, "Get uint32 value: not a uint32 AttrData type: %{public}d.", type_);
566         return ERR_INVALID_PARAMETER;
567     }
568 
569     value = value_.uint32Value;
570     return SUCCESS;
571 }
572 
GetValue(string & value) const573 uint32_t AttrData::GetValue(string &value) const
574 {
575     if (type_ != AttrDataType::ATTR_DATA_STRING) {
576         HiLog::Error(LABEL, "Get string value by reference: not a string AttrData type: %{public}d.", type_);
577         return ERR_INVALID_PARAMETER;
578     }
579 
580     value = *(value_.stringValue);
581     return SUCCESS;
582 }
583 
GetValue(const string * & value) const584 uint32_t AttrData::GetValue(const string *&value) const
585 {
586     if (type_ != AttrDataType::ATTR_DATA_STRING) {
587         HiLog::Error(LABEL, "Get string value: not a string AttrData type: %{public}d.", type_);
588         return ERR_INVALID_PARAMETER;
589     }
590 
591     value = value_.stringValue;
592     return SUCCESS;
593 }
594 
595 // ------------------------------- private method -------------------------------
InitStringAttrData(const AttrData & data)596 uint32_t AttrData::InitStringAttrData(const AttrData &data)
597 {
598     value_.stringValue = new (std::nothrow) string(*(data.value_.stringValue));
599     if (value_.stringValue == nullptr) {
600         HiLog::Error(LABEL, "InitStringAttrData: alloc stringValue result null!");
601         type_ = AttrDataType::ATTR_DATA_NULL;
602         return ERR_INTERNAL;
603     }
604     type_ = AttrDataType::ATTR_DATA_STRING;
605     return SUCCESS;
606 }
607 
InitUint32SetAttrData(const AttrData & data)608 uint32_t AttrData::InitUint32SetAttrData(const AttrData &data)
609 {
610     value_.uint32Set = new (std::nothrow) set<uint32_t>(*(data.value_.uint32Set));
611     if (value_.uint32Set == nullptr) {
612         HiLog::Error(LABEL, "InitUint32SetAttrData: alloc uint32Set result null!");
613         type_ = AttrDataType::ATTR_DATA_NULL;
614         return ERR_INTERNAL;
615     }
616     type_ = AttrDataType::ATTR_DATA_UINT32_SET;
617     return SUCCESS;
618 }
619 
InitStringSetAttrData(const AttrData & data)620 uint32_t AttrData::InitStringSetAttrData(const AttrData &data)
621 {
622     value_.stringSet = new (std::nothrow) set<string>(*(data.value_.stringSet));
623     if (value_.stringSet == nullptr) {
624         HiLog::Error(LABEL, "InitStringSetAttrData: alloc stringSet result null!");
625         type_ = AttrDataType::ATTR_DATA_NULL;
626         return ERR_INTERNAL;
627     }
628     type_ = AttrDataType::ATTR_DATA_STRING_SET;
629     return SUCCESS;
630 }
631 
InRangeUint32Range(uint32_t value) const632 bool AttrData::InRangeUint32Range(uint32_t value) const
633 {
634     return value >= value_.uint32Rang[LOWER_BOUND_INDEX] && value <= value_.uint32Rang[UPPER_BOUND_INDEX];
635 }
636 
InRange(const set<uint32_t> & uint32Set) const637 bool AttrData::InRange(const set<uint32_t> &uint32Set) const
638 {
639     if (uint32Set.empty()) {
640         return false;
641     }
642 
643     for (uint32_t value : uint32Set) {
644         if (!InRange(value)) {
645             return false;
646         }
647     }
648 
649     return true;
650 }
651 
InRange(const set<string> & stringSet) const652 bool AttrData::InRange(const set<string> &stringSet) const
653 {
654     if (stringSet.empty()) {
655         HiLog::Debug(LABEL, "InRange: empty set of parameter.");
656         return false;
657     }
658 
659     for (const string &value : stringSet) {
660         if (!InRange(value)) {
661             return false;
662         }
663     }
664 
665     return true;
666 }
667 
InRange(const uint32_t (& uint32Rang)[RANGE_ARRAY_SIZE]) const668 bool AttrData::InRange(const uint32_t (&uint32Rang)[RANGE_ARRAY_SIZE]) const
669 {
670     if (uint32Rang[LOWER_BOUND_INDEX] > uint32Rang[UPPER_BOUND_INDEX]) {
671         return false;
672     }
673 
674     switch (type_) {
675         case AttrDataType::ATTR_DATA_UINT32: {
676             return uint32Rang[LOWER_BOUND_INDEX] == uint32Rang[UPPER_BOUND_INDEX] &&
677                    uint32Rang[UPPER_BOUND_INDEX] == value_.uint32Value;
678         }
679         case AttrDataType::ATTR_DATA_UINT32_SET: {
680             auto lowerIter = value_.uint32Set->find(uint32Rang[LOWER_BOUND_INDEX]);
681             if (lowerIter == value_.uint32Set->end()) {
682                 return false;
683             }
684 
685             auto upperIter = value_.uint32Set->find(uint32Rang[UPPER_BOUND_INDEX]);
686             if (upperIter == value_.uint32Set->end()) {
687                 return false;
688             }
689 
690             uint32_t count = 0;
691             for (auto tmpIter = lowerIter; tmpIter != upperIter; ++tmpIter) {
692                 count++;
693             }
694 
695             if (count != (uint32Rang[UPPER_BOUND_INDEX] - uint32Rang[LOWER_BOUND_INDEX])) {
696                 return false;
697             }
698 
699             return true;
700         }
701         case AttrDataType::ATTR_DATA_UINT32_RANGE: {
702             return (uint32Rang[LOWER_BOUND_INDEX] >= value_.uint32Rang[LOWER_BOUND_INDEX]) &&
703                    (uint32Rang[UPPER_BOUND_INDEX] <= value_.uint32Rang[UPPER_BOUND_INDEX]);
704         }
705         default: {
706             return false;
707         }
708     }
709 }
710 } // namespace MultimediaPlugin
711 } // namespace OHOS
712