1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 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 TENSORFLOW_CORE_FRAMEWORK_SHARED_PTR_VARIANT_H_ 17 #define TENSORFLOW_CORE_FRAMEWORK_SHARED_PTR_VARIANT_H_ 18 19 #include <memory> 20 21 #include "tensorflow/core/platform/logging.h" 22 23 namespace tensorflow { 24 25 template <typename T> 26 struct SharedPtrVariant { 27 std::shared_ptr<T> shared_ptr; 28 SharedPtrVariantSharedPtrVariant29 SharedPtrVariant() : shared_ptr() {} 30 SharedPtrVariantSharedPtrVariant31 explicit SharedPtrVariant(std::shared_ptr<T>&& ptr) 32 : shared_ptr(std::forward<decltype(ptr)>(ptr)) { 33 VLOG(3) << "Creating shared_ptr of " << shared_ptr.get() 34 << " count is: " << shared_ptr.use_count(); 35 } 36 SharedPtrVariantSharedPtrVariant37 SharedPtrVariant(SharedPtrVariant&& rhs) 38 : shared_ptr(std::move(rhs.shared_ptr)) { 39 VLOG(3) << "Moving SharedPtrVariant of " << shared_ptr.get() 40 << " count is: " << shared_ptr.use_count(); 41 } 42 43 SharedPtrVariant& operator=(const SharedPtrVariant& rhs) = delete; 44 45 SharedPtrVariant& operator=(SharedPtrVariant&& rhs) { 46 if (&rhs == this) return *this; 47 std::swap(shared_ptr, rhs.shared_ptr); 48 VLOG(3) << "Move-assign of SharedPtrVariant of " << shared_ptr.get() 49 << " count is: " << shared_ptr.use_count(); 50 return *this; 51 } 52 SharedPtrVariantSharedPtrVariant53 SharedPtrVariant(const SharedPtrVariant& rhs) : shared_ptr(rhs.shared_ptr) { 54 VLOG(3) << "Copying SharedPtrVariant of " << shared_ptr.get() 55 << " count is: " << shared_ptr.use_count(); 56 } 57 ~SharedPtrVariantSharedPtrVariant58 ~SharedPtrVariant() { 59 VLOG(3) << "Destroying SharedPtrVariant of " << shared_ptr.get() 60 << " count is: " << shared_ptr.use_count(); 61 } 62 EncodeSharedPtrVariant63 void Encode(VariantTensorData*) const { 64 // Not supported. 65 } 66 DecodeSharedPtrVariant67 bool Decode(const VariantTensorData&) { 68 return false; // Not supported. 69 } 70 }; 71 72 } // namespace tensorflow 73 74 #endif // TENSORFLOW_CORE_FRAMEWORK_SHARED_PTR_VARIANT_H_ 75