• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "id_generator.h"
17 #include <climits>
18 #include "display_common.h"
19 namespace OHOS {
20 namespace HDI {
21 namespace DISPLAY {
IdGenerator(std::string name)22 IdGenerator::IdGenerator(std::string name) : name_(name)
23 {
24     DISPLAY_LOGI("name : %{public}s", name.c_str());
25 }
26 
GetNext(uint32_t & id)27 bool IdGenerator::GetNext(uint32_t &id)
28 {
29     DISPLAY_LOGI();
30     if (usedIds_.size() > UINT_MAX) {
31         DISPLAY_LOGE("all id have been used");
32         return false;
33     }
34 
35     while (usedIds_.find(nextId_) != usedIds_.end()) {
36         nextId_++;
37     }
38 
39     id = nextId_++;
40     usedIds_.emplace(id);
41     return true;
42 }
43 
Release(uint32_t id)44 void IdGenerator::Release(uint32_t id)
45 {
46     DISPLAY_LOGI();
47     usedIds_.erase(id);
48 }
49 }
50 }
51 }