• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2025 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 COMMON_COMPONENTS_OBJECTS_STRING_TABLE_INTEGER_CACHE_H
17 #define COMMON_COMPONENTS_OBJECTS_STRING_TABLE_INTEGER_CACHE_H
18 
19 #include <array>
20 #include "common_interfaces/objects/base_string.h"
21 
22 namespace common {
23 
24 /*
25  Used only when 0 < string->GetLength() <= 4
26  +-----------------------------+
27  |        String Payload       |    <-- 32 bits
28  +--------------+--------------+
29  | IsInteger    | IntegerValue |    <-- 16 + 16 bits
30  +--------------+--------------+
31  */
32 class IntegerCache final {
33 static constexpr size_t OBJECT_ALIGN = 8;
34 static_assert(LineString::DATA_OFFSET % OBJECT_ALIGN == 0);
35 
36 public:
37     NO_MOVE_SEMANTIC_CC(IntegerCache);
38     NO_COPY_SEMANTIC_CC(IntegerCache);
39     static constexpr size_t MAX_INTEGER_CACHE_SIZE = 4;
40 
41     IntegerCache() = delete;
42 
InitIntegerCache(BaseString * string)43     static void InitIntegerCache(BaseString* string)
44     {
45         if (string->IsUtf8() && string->GetLength() <= MAX_INTEGER_CACHE_SIZE && string->GetLength() > 0) {
46             IntegerCache* cache = Extract(string);
47             cache->isInteger_ = 0;
48         }
49     }
50 
Extract(BaseString * string)51     static IntegerCache* Extract(BaseString* string)
52     {
53         DCHECK_CC(string->IsUtf8() && string->GetLength() <= MAX_INTEGER_CACHE_SIZE
54             && string->GetLength() > 0 && string->IsInternString());
55         IntegerCache* cache = reinterpret_cast<IntegerCache*>(string->GetData());
56         return cache;
57     }
58 
IsInteger()59     bool IsInteger() const
60     {
61         return isInteger_ != 0;
62     }
63 
GetInteger()64     uint16_t GetInteger() const
65     {
66         DCHECK_CC(IsInteger());
67         return integer_;
68     }
69 
SetInteger(uint16_t value)70     void SetInteger(uint16_t value)
71     {
72         integer_ = value;
73         isInteger_ = 1;
74     }
75 
76 private:
77     [[maybe_unused]] std::array<uint8_t, MAX_INTEGER_CACHE_SIZE> payload_;
78     uint16_t isInteger_ = 0;
79     uint16_t integer_ = 0;
80 };
81 }
82 #endif //COMMON_COMPONENTS_OBJECTS_STRING_TABLE_INTEGER_CACHE_H
83