• 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 #ifndef OHOS_RESTOOL_ID_WORKER_H
17 #define OHOS_RESTOOL_ID_WORKER_H
18 
19 #include<functional>
20 #include<vector>
21 #include "singleton.h"
22 #include "resource_data.h"
23 #include "resource_util.h"
24 
25 namespace OHOS {
26 namespace Global {
27 namespace Restool {
28 class IdWorker : public Singleton<IdWorker> {
29 public:
30     enum class ResourceIdCluster {
31         RES_ID_APP = 0,
32         RES_ID_SYS,
33         RES_ID_TYPE_MAX,
34     };
35 
36     struct ResourceId {
37         int32_t id;
38         int32_t seq;
39         std::string type;
40         std::string name;
41     };
42 
43     uint32_t Init(ResourceIdCluster type, int32_t start = 0x01000000);
44     int32_t GenerateId(ResType resType, const std::string &name);
45     std::vector<ResourceId> GetHeaderId() const;
46     int32_t GetId(ResType resType, const std::string &name) const;
47     int32_t GetSystemId(ResType resType, const std::string &name) const;
48     bool IsValidName(const std::string &name) const;
49     bool PushCache(ResType resType, const std::string &name, int32_t id);
50     void PushDelId(int32_t id);
51 
52 private:
53     int32_t GenerateAppId(ResType resType, const std::string &name);
54     int32_t GenerateSysId(ResType resType, const std::string &name);
55     uint32_t InitIdDefined();
56     uint32_t InitIdDefined(const std::string &filePath);
57     using ParseFunction = std::function<bool(const Json::Value&, ResourceId&)>;
58     void InitParser();
59     bool ParseType(const Json::Value &type, ResourceId &resourceId);
60     bool ParseName(const Json::Value &name, ResourceId &resourceId);
61     bool ParseOrder(const Json::Value &order, ResourceId &resourceId);
62     bool PushResourceId(const ResourceId &resourceId);
63     bool IsValidSystemName(const std::string &name) const;
64     int32_t GetStartId(const Json::Value &root) const;
65     int32_t GetMaxId(int32_t startId) const;
66     int32_t appId_;
67     int32_t maxId_;
68     ResourceIdCluster type_;
69     std::map<std::pair<ResType, std::string>, int32_t> ids_;
70     std::map<std::pair<ResType, std::string>, ResourceId> definedIds_;
71     std::map<std::string, ParseFunction> handles_;
72     std::vector<int32_t> delIds_;
73     std::map<std::pair<ResType, std::string>, int32_t> cacheIds_;
74     static const int32_t START_SYS_ID;
75 };
76 }
77 }
78 }
79 #endif
80