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, bool isSystem); 57 uint32_t IdDefinedToResourceIds(const Json::Value &record, bool isSystem, const int32_t strtSysId = 0); 58 using ParseFunction = std::function<bool(const Json::Value&, ResourceId&)>; 59 void InitParser(); 60 bool ParseType(const Json::Value &type, ResourceId &resourceId); 61 bool ParseName(const Json::Value &name, ResourceId &resourceId); 62 bool ParseOrder(const Json::Value &order, ResourceId &resourceId); 63 bool ParseId(const Json::Value &id, ResourceId &resourceId); 64 bool PushResourceId(const ResourceId &resourceId, bool isSystem); 65 bool IsValidSystemName(const std::string &name) const; 66 int32_t GetStartId(const Json::Value &root) const; 67 int32_t GetMaxId(int32_t startId) const; 68 int32_t GetCurId(); 69 int32_t appId_; 70 int32_t maxId_; 71 ResourceIdCluster type_; 72 std::map<std::pair<ResType, std::string>, int32_t> ids_; 73 std::map<std::pair<ResType, std::string>, ResourceId> sysDefinedIds_; 74 std::map<std::pair<ResType, std::string>, ResourceId> appDefinedIds_; 75 std::map<int32_t, ResourceId> idDefineds_; 76 std::map<std::string, ParseFunction> handles_; 77 std::vector<int32_t> delIds_; 78 std::map<std::pair<ResType, std::string>, int32_t> cacheIds_; 79 static const int32_t START_SYS_ID; 80 }; 81 } 82 } 83 } 84 #endif 85