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#ifndef ATTR_DATA_H
17#define ATTR_DATA_H
18
19#include <set>
20#include <string>
21#include "plugin_errors.h"
22
23namespace OHOS {
24namespace MultimediaPlugin {
25enum class AttrDataType : int32_t {
26    ATTR_DATA_NULL = 0,
27    ATTR_DATA_BOOL,
28    ATTR_DATA_UINT32,
29    ATTR_DATA_STRING,
30    ATTR_DATA_UINT32_SET,
31    ATTR_DATA_STRING_SET,
32    ATTR_DATA_UINT32_RANGE,
33    ATTR_DATA_TYPE_INVALID
34};
35
36class AttrData final {
37public:
38    AttrData();
39    explicit AttrData(bool value);
40    explicit AttrData(uint32_t value);
41    explicit AttrData(const std::string &value);
42    explicit AttrData(std::string &&value);
43    AttrData(uint32_t lowerBound, uint32_t upperBound);
44    AttrData(const AttrData &data);
45    AttrData(AttrData &&data) noexcept;
46    ~AttrData();
47
48    AttrData &operator=(const AttrData &data);
49    AttrData &operator=(AttrData &&data) noexcept;
50
51    void SetData(bool value);
52    void SetData(uint32_t value);
53    uint32_t SetData(const std::string &value);
54    uint32_t SetData(std::string &&value);
55    uint32_t SetData(uint32_t lowerBound, uint32_t upperBound);
56    void ClearData();
57
58    uint32_t InsertSet(uint32_t value);
59    uint32_t InsertSet(const std::string &value);
60    uint32_t InsertSet(std::string &&value);
61
62    bool InRange(bool value) const;
63    bool InRange(uint32_t value) const;
64    bool InRange(const std::string &value) const;
65    bool InRange(const AttrData &data) const;
66
67    AttrDataType GetType() const;
68    uint32_t GetMinValue(uint32_t &value) const;
69    uint32_t GetMaxValue(uint32_t &value) const;
70    uint32_t GetMinValue(const std::string *&value) const;
71    uint32_t GetMaxValue(const std::string *&value) const;
72
73    uint32_t GetValue(bool &value) const;
74    uint32_t GetValue(uint32_t &value) const;
75    uint32_t GetValue(std::string &value) const;
76    uint32_t GetValue(const std::string *&value) const;
77
78    static constexpr uint8_t RANGE_ARRAY_SIZE = 2;
79    static constexpr uint8_t LOWER_BOUND_INDEX = 0;
80    static constexpr uint8_t UPPER_BOUND_INDEX = 1;
81
82private:
83    uint32_t InitStringAttrData(const AttrData &data);
84    uint32_t InitUint32SetAttrData(const AttrData &data);
85    uint32_t InitStringSetAttrData(const AttrData &data);
86    bool InRangeUint32Range(uint32_t value) const;
87    bool InRange(const std::set<uint32_t> &uint32Set) const;
88    bool InRange(const std::set<std::string> &stringSet) const;
89    bool InRange(const uint32_t (&uint32Rang)[RANGE_ARRAY_SIZE]) const;
90
91    AttrDataType type_;
92    union AttrDataUnion {
93        bool boolValue;
94        uint32_t uint32Value;
95        uint32_t uint32Rang[RANGE_ARRAY_SIZE];
96        std::set<uint32_t> *uint32Set;
97        std::string *stringValue;
98        std::set<std::string> *stringSet;
99    };
100    AttrDataUnion value_;
101};
102} // namespace MultimediaPlugin
103} // namespace OHOS
104
105#endif // ATTR_DATA_H
106