You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Status.cs 2.2 kB

6 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using System;
  14. namespace Tensorflow
  15. {
  16. /// <summary>
  17. /// TF_Status holds error information. It either has an OK code, or
  18. /// else an error code with an associated error message.
  19. /// </summary>
  20. public class Status : DisposableObject
  21. {
  22. /// <summary>
  23. /// Error message
  24. /// </summary>
  25. public string Message => c_api.StringPiece(c_api.TF_Message(_handle));
  26. /// <summary>
  27. /// Error code
  28. /// </summary>
  29. public TF_Code Code => c_api.TF_GetCode(_handle);
  30. public Status()
  31. {
  32. _handle = c_api.TF_NewStatus();
  33. }
  34. public void SetStatus(TF_Code code, string msg)
  35. {
  36. c_api.TF_SetStatus(_handle, code, msg);
  37. }
  38. /// <summary>
  39. /// Check status
  40. /// Throw exception with error message if code != TF_OK
  41. /// </summary>
  42. public void Check(bool throwException = false)
  43. {
  44. if(Code != TF_Code.TF_OK)
  45. {
  46. Console.WriteLine(Message);
  47. if (throwException)
  48. {
  49. throw new Exception(Message);
  50. }
  51. }
  52. }
  53. public static implicit operator IntPtr(Status status)
  54. {
  55. return status._handle;
  56. }
  57. protected override void DisposeUnManagedState(IntPtr handle)
  58. => c_api.TF_DeleteStatus(handle);
  59. }
  60. }