1f0bfeaa8Sopenharmony_ci/*
2f0bfeaa8Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3f0bfeaa8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f0bfeaa8Sopenharmony_ci * you may not use this file except in compliance with the License.
5f0bfeaa8Sopenharmony_ci * You may obtain a copy of the License at
6f0bfeaa8Sopenharmony_ci *
7f0bfeaa8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f0bfeaa8Sopenharmony_ci *
9f0bfeaa8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f0bfeaa8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f0bfeaa8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f0bfeaa8Sopenharmony_ci * See the License for the specific language governing permissions and
13f0bfeaa8Sopenharmony_ci * limitations under the License.
14f0bfeaa8Sopenharmony_ci */
15f0bfeaa8Sopenharmony_ci
16f0bfeaa8Sopenharmony_ci#ifndef OHOS_UTILS_MEMORY_LIBPURGEABLEMEM_CPP_INCLUDE_PURGEABLE_MEM_BUILDER_H
17f0bfeaa8Sopenharmony_ci#define OHOS_UTILS_MEMORY_LIBPURGEABLEMEM_CPP_INCLUDE_PURGEABLE_MEM_BUILDER_H
18f0bfeaa8Sopenharmony_ci
19f0bfeaa8Sopenharmony_ci#include <memory> /* unique_ptr */
20f0bfeaa8Sopenharmony_ci#include <functional>
21f0bfeaa8Sopenharmony_ci
22f0bfeaa8Sopenharmony_cinamespace OHOS {
23f0bfeaa8Sopenharmony_cinamespace PurgeableMem {
24f0bfeaa8Sopenharmony_ci/*
25f0bfeaa8Sopenharmony_ci * Class PurgeableMemBuilder is a base class of user's builder.
26f0bfeaa8Sopenharmony_ci * PurgeableMem users can define their builders by inheriting this class.
27f0bfeaa8Sopenharmony_ci * In its member func Build(), user should define how to build the content of a PurgeableMem obj.
28f0bfeaa8Sopenharmony_ci */
29f0bfeaa8Sopenharmony_ciclass PurgeableMemBuilder {
30f0bfeaa8Sopenharmony_cipublic:
31f0bfeaa8Sopenharmony_ci    virtual ~PurgeableMemBuilder();
32f0bfeaa8Sopenharmony_ci
33f0bfeaa8Sopenharmony_ci    /*
34f0bfeaa8Sopenharmony_ci     * User should define how to build the content of a PurgeableMem obj in this func.
35f0bfeaa8Sopenharmony_ci     * Input:   data: data ptr, ponits to start address of a PurgeableMem obj's content.
36f0bfeaa8Sopenharmony_ci     * Input:   size: data size of the content.
37f0bfeaa8Sopenharmony_ci     * Return:  build content result, true means success, while false is fail.
38f0bfeaa8Sopenharmony_ci     */
39f0bfeaa8Sopenharmony_ci    virtual bool Build(void *data, size_t size) = 0;
40f0bfeaa8Sopenharmony_ci
41f0bfeaa8Sopenharmony_ci    void SetRebuildSuccessCallback(std::function<void()> &callback)
42f0bfeaa8Sopenharmony_ci    {
43f0bfeaa8Sopenharmony_ci        rebuildSuccessCallback_ = callback;
44f0bfeaa8Sopenharmony_ci    }
45f0bfeaa8Sopenharmony_ci
46f0bfeaa8Sopenharmony_ci    void DoRebuildSuccessCallback()
47f0bfeaa8Sopenharmony_ci    {
48f0bfeaa8Sopenharmony_ci        if (rebuildSuccessCallback_) {
49f0bfeaa8Sopenharmony_ci            rebuildSuccessCallback_();
50f0bfeaa8Sopenharmony_ci        }
51f0bfeaa8Sopenharmony_ci    }
52f0bfeaa8Sopenharmony_ci
53f0bfeaa8Sopenharmony_ciprivate:
54f0bfeaa8Sopenharmony_ci    std::function<void()> rebuildSuccessCallback_ = nullptr;
55f0bfeaa8Sopenharmony_ci    std::unique_ptr<PurgeableMemBuilder> nextBuilder_ = nullptr;
56f0bfeaa8Sopenharmony_ci
57f0bfeaa8Sopenharmony_ci    /* Only called by its friend */
58f0bfeaa8Sopenharmony_ci    void AppendBuilder(std::unique_ptr<PurgeableMemBuilder> builder);
59f0bfeaa8Sopenharmony_ci    bool BuildAll(void *data, size_t size);
60f0bfeaa8Sopenharmony_ci    friend class PurgeableMemBase;
61f0bfeaa8Sopenharmony_ci};
62f0bfeaa8Sopenharmony_ci} /* namespace PurgeableMem */
63f0bfeaa8Sopenharmony_ci} /* namespace OHOS */
64f0bfeaa8Sopenharmony_ci#endif /* OHOS_UTILS_MEMORY_LIBPURGEABLEMEM_CPP_INCLUDE_PURGEABLE_MEM_BUILDER_H */
65