1 /* Copyright 2020 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_C_TENSOR_INTERFACE_H_
17 #define TENSORFLOW_C_TENSOR_INTERFACE_H_
18 
19 #include "tensorflow/core/framework/types.pb.h"
20 #include "tensorflow/core/platform/status.h"
21 
22 namespace tensorflow {
23 
24 // Abstract interface to a Tensor.
25 //
26 // This allows us to hide concrete implementations of Tensor from header
27 // files. The interface lists the common functionality that must be provided by
28 // any concrete implementation. However, in cases where the true concrete class
29 // is needed a static_cast can be applied.
30 class AbstractTensorInterface {
31  public:
32   // Release any underlying resources, including the interface object.
33   virtual void Release() = 0;
34 
35   // Returns tensor dtype.
36   virtual DataType Type() const = 0;
37   // Returns number of dimensions.
38   virtual int NumDims() const = 0;
39   // Returns size of specified dimension
40   virtual int64_t Dim(int dim_index) const = 0;
41   // Returns number of elements across all dimensions.
42   virtual int64_t NumElements() const = 0;
43   // Return size in bytes of the Tensor
44   virtual size_t ByteSize() const = 0;
45   // Returns a pointer to tensor data
46   virtual void* Data() const = 0;
47 
48   // Returns if the tensor is aligned
49   virtual bool IsAligned() const = 0;
50   // Returns if their is sole ownership of this Tensor and thus it can be moved.
51   virtual bool CanMove() const = 0;
52 
53  protected:
~AbstractTensorInterface()54   virtual ~AbstractTensorInterface() {}
55 };
56 
57 namespace internal {
58 struct AbstractTensorInterfaceDeleter {
operatorAbstractTensorInterfaceDeleter59   void operator()(AbstractTensorInterface* p) const {
60     if (p != nullptr) {
61       p->Release();
62     }
63   }
64 };
65 }  // namespace internal
66 
67 using AbstractTensorPtr =
68     std::unique_ptr<AbstractTensorInterface,
69                     internal::AbstractTensorInterfaceDeleter>;
70 
71 }  // namespace tensorflow
72 
73 #endif  // TENSORFLOW_C_TENSOR_INTERFACE_H_
74