* @brief Create a map to trace all members of this object. * @param alloc allocator that maintain this node's memory * @retval true successful * @retval false failed, which means that no memory can be allocated by * allocator. */
| 275 | * allocator. |
| 276 | */ |
| 277 | bool CreateMap(Allocator& alloc) { |
| 278 | sonic_assert(this->IsObject()); |
| 279 | sonic_assert(this->Capacity() >= this->Size()); |
| 280 | // if (this->Size() == 0) return false; |
| 281 | if (nullptr == children()) { |
| 282 | this->memberReserveImpl(16, alloc); |
| 283 | } |
| 284 | if (getMapUnsfe()) return true; |
| 285 | map_type* map = static_cast<map_type*>(alloc.Malloc(sizeof(map_type))); |
| 286 | new (map) map_type(MAType(&alloc)); |
| 287 | // SetMap(map); |
| 288 | MemberNode* m = (MemberNode*)getObjChildrenFirstUnsafe(); |
| 289 | for (size_t i = 0; i < this->Size(); ++i) { |
| 290 | map->emplace(std::make_pair((m + i)->name.GetStringView(), i)); |
| 291 | } |
| 292 | setMap(map); |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * @brief Destory the created map. This means that you don't want maintain the |