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.2 kB

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