| 303 | |
| 304 | template <typename T> |
| 305 | struct Value : ValueInterface { |
| 306 | template <class... Args> |
| 307 | explicit Value(in_place_t /*tag*/, Args&&... args) |
| 308 | : value(std::forward<Args>(args)...) {} |
| 309 | |
| 310 | // NOTE(ebrevdo): Destructor must be explicitly defined for CUDA to happily |
| 311 | // build `alignof(Variant<void*>)`. |
| 312 | ~Value() final = default; |
| 313 | |
| 314 | TypeIndex TypeId() const override { |
| 315 | const TypeIndex value_type_index = |
| 316 | MakeTypeIndex<typename std::decay<T>::type>(); |
| 317 | return value_type_index; |
| 318 | } |
| 319 | |
| 320 | void* RawPtr() override { return &value; } |
| 321 | |
| 322 | const void* RawPtr() const override { return &value; } |
| 323 | |
| 324 | ValueInterface* Clone() const override { |
| 325 | // NOTE: Use placement new here because we override `operator delete`, |
| 326 | // and need to match the call to `port::Free()` with a call to |
| 327 | // `port::Malloc()`. |
| 328 | auto* clone = static_cast<Value*>(port::Malloc(sizeof(Value))); |
| 329 | new (clone) Value(kInPlace, value); |
| 330 | return clone; |
| 331 | } |
| 332 | |
| 333 | void MoveAssign(ValueInterface* memory) override { |
| 334 | CHECK(TypeId() == memory->TypeId()) |
| 335 | << TypeId().name() << " vs. " << memory->TypeId().name(); |
| 336 | static_cast<Value*>(memory)->value = std::move(value); |
| 337 | } |
| 338 | |
| 339 | void CloneInto(ValueInterface* memory) const override { |
| 340 | new (memory) Value(kInPlace, value); |
| 341 | } |
| 342 | |
| 343 | void MoveInto(ValueInterface* memory) override { |
| 344 | new (memory) Value(kInPlace, std::move(value)); |
| 345 | } |
| 346 | |
| 347 | void Swap(ValueInterface* memory) override { |
| 348 | CHECK(TypeId() == memory->TypeId()) |
| 349 | << TypeId().name() << " vs. " << memory->TypeId().name(); |
| 350 | std::swap(value, static_cast<Value*>(memory)->value); |
| 351 | } |
| 352 | |
| 353 | string TypeName() const override { return TypeNameVariant(value); } |
| 354 | |
| 355 | string DebugString() const override { return DebugStringVariant(value); } |
| 356 | |
| 357 | void Encode(VariantTensorData* data) const override { |
| 358 | EncodeVariant(value, data); |
| 359 | } |
| 360 | |
| 361 | bool Decode(VariantTensorData data) override { |
| 362 | return DecodeVariant(&data, &value); |
no outgoing calls
no test coverage detected