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 FRAMEWORKS_SURFACE_INCLUDE_BUFFER_EXTRA_DATA_IMPL_H
17#define FRAMEWORKS_SURFACE_INCLUDE_BUFFER_EXTRA_DATA_IMPL_H
18
19#include <buffer_extra_data.h>
20#include <any>
21#include <mutex>
22
23namespace OHOS {
24class BufferExtraDataImpl : public BufferExtraData {
25public:
26    virtual GSError ReadFromParcel(MessageParcel &parcel) override;
27    virtual GSError WriteToParcel(MessageParcel &parcel) override;
28    virtual GSError ExtraGet(const std::string &key, int32_t &value) const override;
29    virtual GSError ExtraGet(const std::string &key, int64_t &value) const override;
30    virtual GSError ExtraGet(const std::string &key, double &value) const override;
31    virtual GSError ExtraGet(const std::string &key, std::string &value) const override;
32    virtual GSError ExtraSet(const std::string &key, int32_t value) override;
33    virtual GSError ExtraSet(const std::string &key, int64_t value) override;
34    virtual GSError ExtraSet(const std::string &key, double value) override;
35    virtual GSError ExtraSet(const std::string &key, const std::string& value) override;
36
37private:
38    enum class ExtraDataType : int32_t {
39        i32,
40        i64,
41        f64,
42        string,
43    };
44    template<class T>
45    GSError ExtraGet(const std::string &key, ExtraDataType type, T &value) const;
46    GSError ExtraSet(const std::string &key, ExtraDataType type, const std::any& val);
47
48    struct ExtraData {
49        std::any val;
50        ExtraDataType type;
51    };
52    std::map<std::string, struct ExtraData> datas_;
53    mutable std::mutex mtx_;
54};
55} // namespace OHOS
56
57#endif // FRAMEWORKS_SURFACE_INCLUDE_BUFFER_EXTRA_DATA_IMPL_H
58