Skip to content

Commit 028b977

Browse files
sharwellEsther2013
authored andcommitted
Implement SafeResourceVariableHandle as a wrapper for TFE_ResourceVariable
1 parent 076b3fc commit 028b977

2 files changed

Lines changed: 44 additions & 5 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*****************************************************************************
2+
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
******************************************************************************/
16+
17+
using System;
18+
using Tensorflow.Util;
19+
20+
namespace Tensorflow.Variables
21+
{
22+
public sealed class SafeResourceVariableHandle : SafeTensorflowHandle
23+
{
24+
private SafeResourceVariableHandle()
25+
{
26+
}
27+
28+
public SafeResourceVariableHandle(IntPtr handle)
29+
: base(handle)
30+
{
31+
}
32+
33+
protected override bool ReleaseHandle()
34+
{
35+
c_api.TFE_DeleteResourceVariable(handle);
36+
SetHandle(IntPtr.Zero);
37+
return true;
38+
}
39+
}
40+
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Runtime.InteropServices;
4-
using System.Text;
3+
using Tensorflow.Variables;
54

65
namespace Tensorflow
76
{
87
public partial class c_api
98
{
109
[DllImport(TensorFlowLibName)]
11-
public static extern IntPtr TFE_NewResourceVariable();
10+
public static extern SafeResourceVariableHandle TFE_NewResourceVariable();
1211

1312
[DllImport(TensorFlowLibName)]
1413
public static extern void TFE_DeleteResourceVariable(IntPtr variable);
1514

1615
[DllImport(TensorFlowLibName)]
17-
public static extern void TFE_SetResourceVariableHandle(IntPtr variable, IntPtr tensor);
16+
public static extern void TFE_SetResourceVariableHandle(SafeResourceVariableHandle variable, IntPtr tensor);
1817

1918
[DllImport(TensorFlowLibName)]
20-
public static extern void TFE_SetResourceVariableName(IntPtr variable, string name);
19+
public static extern void TFE_SetResourceVariableName(SafeResourceVariableHandle variable, string name);
2120
}
2221
}

0 commit comments

Comments
 (0)