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 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. using System.Diagnostics;
  15. using System.Runtime.CompilerServices;
  16. using Tensorflow.Exceptions;
  17. using Tensorflow.Util;
  18. using static Tensorflow.c_api;
  19. namespace Tensorflow
  20. {
  21. /// <summary>
  22. /// TF_Status holds error information. It either has an OK code, or
  23. /// else an error code with an associated error message.
  24. /// </summary>
  25. public sealed class Status
  26. {
  27. /// <summary>
  28. /// Error message
  29. /// </summary>
  30. public string Message
  31. {
  32. get
  33. {
  34. using (_handle.Lease())
  35. {
  36. return StringPiece(TF_Message(_handle));
  37. }
  38. }
  39. }
  40. /// <summary>
  41. /// Error code
  42. /// </summary>
  43. public TF_Code Code => TF_GetCode(_handle);
  44. SafeStatusHandle _handle { get; }
  45. public Status()
  46. {
  47. _handle = TF_NewStatus();
  48. }
  49. public Status(SafeStatusHandle handle)
  50. {
  51. _handle = handle ?? throw new ArgumentNullException(nameof(handle));
  52. }
  53. public void SetStatus(TF_Code code, string msg)
  54. {
  55. TF_SetStatus(_handle, code, msg);
  56. }
  57. public bool ok() => Code == TF_Code.TF_OK;
  58. /// <summary>
  59. /// Check status
  60. /// Throw exception with error message if code != TF_OK
  61. /// </summary>
  62. /// <exception cref="TensorflowException">When the returned check is not TF_Code.TF_OK</exception>
  63. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  64. [DebuggerHidden]
  65. public void Check(bool throwException = false)
  66. {
  67. if (Code != TF_Code.TF_OK)
  68. {
  69. var message = Message;
  70. if (throwException)
  71. {
  72. switch (Code)
  73. {
  74. case TF_Code.TF_OUT_OF_RANGE:
  75. throw new OutOfRangeError(message);
  76. case TF_Code.TF_INVALID_ARGUMENT:
  77. throw new InvalidArgumentError(message);
  78. default:
  79. throw new NotOkStatusException(message);
  80. }
  81. }
  82. }
  83. }
  84. public override string ToString()
  85. => $"{Code} 0x{_handle.DangerousGetHandle():x16}";
  86. public static implicit operator SafeStatusHandle(Status status)
  87. {
  88. return status._handle;
  89. }
  90. }
  91. }