Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DBC.cs 9.6KB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. using System;
  2. using System.Diagnostics;
  3. namespace DBC.ECS
  4. {
  5. /// <summary>
  6. /// Design By Contract Checks.
  7. ///
  8. /// Each method generates an exception or
  9. /// a trace assertion statement if the contract is broken.
  10. /// </summary>
  11. /// <remarks>
  12. /// This example shows how to call the Require method.
  13. /// Assume DBC_CHECK_PRECONDITION is defined.
  14. /// <code>
  15. /// public void Test(int x)
  16. /// {
  17. /// try
  18. /// {
  19. /// Check.Require(x > 1, "x must be > 1");
  20. /// }
  21. /// catch (System.Exception ex)
  22. /// {
  23. /// Console.WriteLine(ex.ToString());
  24. /// }
  25. /// }
  26. /// </code>
  27. /// If you wish to use trace assertion statements, intended for Debug scenarios,
  28. /// rather than exception handling then set
  29. ///
  30. /// <code>Check.UseAssertions = true</code>
  31. ///
  32. /// You can specify this in your application entry point and maybe make it
  33. /// dependent on conditional compilation flags or configuration file settings, e.g.,
  34. /// <code>
  35. /// #if DBC_USE_ASSERTIONS
  36. /// Check.UseAssertions = true;
  37. /// #endif
  38. /// </code>
  39. /// You can direct output to a Trace listener. For example, you could insert
  40. /// <code>
  41. /// Trace.Listeners.Clear();
  42. /// Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
  43. /// </code>
  44. ///
  45. /// or direct output to a file or the Event Log.
  46. ///
  47. /// (Note: For ASP.NET clients use the Listeners collection
  48. /// of the Debug, not the Trace, object and, for a Release build, only exception-handling
  49. /// is possible.)
  50. /// </remarks>
  51. ///
  52. static class Check
  53. {
  54. #region Interface
  55. /// <summary>
  56. /// Precondition check.
  57. /// </summary>
  58. #if PROFILER || !DEBUG
  59. [Conditional("__NEVER_DEFINED__")]
  60. #endif
  61. public static void Require(bool assertion, string message)
  62. {
  63. if (UseExceptions)
  64. {
  65. if (!assertion)
  66. throw new PreconditionException(message);
  67. }
  68. else
  69. {
  70. Trace.Assert(assertion, "Precondition: " + message);
  71. }
  72. }
  73. /// <summary>
  74. /// Precondition check.
  75. /// </summary>
  76. ///
  77. #if PROFILER || !DEBUG
  78. [Conditional("__NEVER_DEFINED__")]
  79. #endif
  80. public static void Require(bool assertion, string message, Exception inner)
  81. {
  82. if (UseExceptions)
  83. {
  84. if (!assertion)
  85. throw new PreconditionException(message, inner);
  86. }
  87. else
  88. {
  89. Trace.Assert(assertion, "Precondition: " + message);
  90. }
  91. }
  92. /// <summary>
  93. /// Precondition check.
  94. /// </summary>
  95. ///
  96. #if PROFILER || !DEBUG
  97. [Conditional("__NEVER_DEFINED__")]
  98. #endif
  99. public static void Require(bool assertion)
  100. {
  101. if (UseExceptions)
  102. {
  103. if (!assertion)
  104. throw new PreconditionException("Precondition failed.");
  105. }
  106. else
  107. {
  108. Trace.Assert(assertion, "Precondition failed.");
  109. }
  110. }
  111. /// <summary>
  112. /// Postcondition check.
  113. /// </summary>
  114. ///
  115. #if PROFILER || !DEBUG
  116. [Conditional("__NEVER_DEFINED__")]
  117. #endif
  118. public static void Ensure(bool assertion, string message)
  119. {
  120. if (UseExceptions)
  121. {
  122. if (!assertion)
  123. throw new PostconditionException(message);
  124. }
  125. else
  126. {
  127. Trace.Assert(assertion, "Postcondition: " + message);
  128. }
  129. }
  130. /// <summary>
  131. /// Postcondition check.
  132. /// </summary>
  133. ///
  134. #if PROFILER || !DEBUG
  135. [Conditional("__NEVER_DEFINED__")]
  136. #endif
  137. public static void Ensure(bool assertion, string message, Exception inner)
  138. {
  139. if (UseExceptions)
  140. {
  141. if (!assertion)
  142. throw new PostconditionException(message, inner);
  143. }
  144. else
  145. {
  146. Trace.Assert(assertion, "Postcondition: " + message);
  147. }
  148. }
  149. /// <summary>
  150. /// Postcondition check.
  151. /// </summary>
  152. ///
  153. #if PROFILER || !DEBUG
  154. [Conditional("__NEVER_DEFINED__")]
  155. #endif
  156. public static void Ensure(bool assertion)
  157. {
  158. if (UseExceptions)
  159. {
  160. if (!assertion)
  161. throw new PostconditionException("Postcondition failed.");
  162. }
  163. else
  164. {
  165. Trace.Assert(assertion, "Postcondition failed.");
  166. }
  167. }
  168. /// <summary>
  169. /// Invariant check.
  170. /// </summary>
  171. ///
  172. #if PROFILER || !DEBUG
  173. [Conditional("__NEVER_DEFINED__")]
  174. #endif
  175. public static void Invariant(bool assertion, string message)
  176. {
  177. if (UseExceptions)
  178. {
  179. if (!assertion)
  180. throw new InvariantException(message);
  181. }
  182. else
  183. {
  184. Trace.Assert(assertion, "Invariant: " + message);
  185. }
  186. }
  187. /// <summary>
  188. /// Invariant check.
  189. /// </summary>
  190. ///
  191. #if PROFILER || !DEBUG
  192. [Conditional("__NEVER_DEFINED__")]
  193. #endif
  194. public static void Invariant(bool assertion, string message, Exception inner)
  195. {
  196. if (UseExceptions)
  197. {
  198. if (!assertion)
  199. throw new InvariantException(message, inner);
  200. }
  201. else
  202. {
  203. Trace.Assert(assertion, "Invariant: " + message);
  204. }
  205. }
  206. /// <summary>
  207. /// Invariant check.
  208. /// </summary>
  209. ///
  210. #if PROFILER || !DEBUG
  211. [Conditional("__NEVER_DEFINED__")]
  212. #endif
  213. public static void Invariant(bool assertion)
  214. {
  215. if (UseExceptions)
  216. {
  217. if (!assertion)
  218. throw new InvariantException("Invariant failed.");
  219. }
  220. else
  221. {
  222. Trace.Assert(assertion, "Invariant failed.");
  223. }
  224. }
  225. /// <summary>
  226. /// Assertion check.
  227. /// </summary>
  228. #if PROFILER || !DEBUG
  229. [Conditional("__NEVER_DEFINED__")]
  230. #endif
  231. public static void Assert(bool assertion, string message)
  232. {
  233. if (UseExceptions)
  234. {
  235. if (!assertion)
  236. throw new AssertionException(message);
  237. }
  238. else
  239. {
  240. Trace.Assert(assertion, "Assertion: " + message);
  241. }
  242. }
  243. /// <summary>
  244. /// Assertion check.
  245. /// </summary>
  246. ///
  247. #if PROFILER || !DEBUG
  248. [Conditional("__NEVER_DEFINED__")]
  249. #endif
  250. public static void Assert(bool assertion, string message, Exception inner)
  251. {
  252. if (UseExceptions)
  253. {
  254. if (!assertion)
  255. throw new AssertionException(message, inner);
  256. }
  257. else
  258. {
  259. Trace.Assert(assertion, "Assertion: " + message);
  260. }
  261. }
  262. /// <summary>
  263. /// Assertion check.
  264. /// </summary>
  265. ///
  266. #if PROFILER || !DEBUG
  267. [Conditional("__NEVER_DEFINED__")]
  268. #endif
  269. public static void Assert(bool assertion)
  270. {
  271. if (UseExceptions)
  272. {
  273. if (!assertion)
  274. throw new AssertionException("Assertion failed.");
  275. }
  276. else
  277. {
  278. Trace.Assert(assertion, "Assertion failed.");
  279. }
  280. }
  281. /// <summary>
  282. /// Set this if you wish to use Trace Assert statements
  283. /// instead of exception handling.
  284. /// (The Check class uses exception handling by default.)
  285. /// </summary>
  286. public static bool UseAssertions
  287. {
  288. get
  289. {
  290. return useAssertions;
  291. }
  292. set
  293. {
  294. useAssertions = value;
  295. }
  296. }
  297. #endregion // Interface
  298. #region Implementation
  299. // No creation
  300. /// <summary>
  301. /// Is exception handling being used?
  302. /// </summary>
  303. private static bool UseExceptions
  304. {
  305. get
  306. {
  307. return !useAssertions;
  308. }
  309. }
  310. // Are trace assertion statements being used?
  311. // Default is to use exception handling.
  312. private static bool useAssertions = false;
  313. #endregion // Implementation
  314. } // End Check
  315. class Trace
  316. {
  317. internal static void Assert(bool assertion, string v)
  318. {
  319. #if NETFX_CORE
  320. System.Diagnostics.Contracts.Contract.Assert(assertion, v);
  321. #else
  322. System.Diagnostics.Trace.Assert(assertion, v);
  323. #endif
  324. }
  325. }
  326. #region Exceptions
  327. /// <summary>
  328. /// Exception raised when a contract is broken.
  329. /// Catch this exception type if you wish to differentiate between
  330. /// any DesignByContract exception and other runtime exceptions.
  331. ///
  332. /// </summary>
  333. public class DesignByContractException : Exception
  334. {
  335. protected DesignByContractException() {}
  336. protected DesignByContractException(string message) : base(message) {}
  337. protected DesignByContractException(string message, Exception inner) : base(message, inner) {}
  338. }
  339. /// <summary>
  340. /// Exception raised when a precondition fails.
  341. /// </summary>
  342. public class PreconditionException : DesignByContractException
  343. {
  344. /// <summary>
  345. /// Precondition Exception.
  346. /// </summary>
  347. public PreconditionException() {}
  348. /// <summary>
  349. /// Precondition Exception.
  350. /// </summary>
  351. public PreconditionException(string message) : base(message) {}
  352. /// <summary>
  353. /// Precondition Exception.
  354. /// </summary>
  355. public PreconditionException(string message, Exception inner) : base(message, inner) {}
  356. }
  357. /// <summary>
  358. /// Exception raised when a postcondition fails.
  359. /// </summary>
  360. public class PostconditionException : DesignByContractException
  361. {
  362. /// <summary>
  363. /// Postcondition Exception.
  364. /// </summary>
  365. public PostconditionException() {}
  366. /// <summary>
  367. /// Postcondition Exception.
  368. /// </summary>
  369. public PostconditionException(string message) : base(message) {}
  370. /// <summary>
  371. /// Postcondition Exception.
  372. /// </summary>
  373. public PostconditionException(string message, Exception inner) : base(message, inner) {}
  374. }
  375. /// <summary>
  376. /// Exception raised when an invariant fails.
  377. /// </summary>
  378. public class InvariantException : DesignByContractException
  379. {
  380. /// <summary>
  381. /// Invariant Exception.
  382. /// </summary>
  383. public InvariantException() {}
  384. /// <summary>
  385. /// Invariant Exception.
  386. /// </summary>
  387. public InvariantException(string message) : base(message) {}
  388. /// <summary>
  389. /// Invariant Exception.
  390. /// </summary>
  391. public InvariantException(string message, Exception inner) : base(message, inner) {}
  392. }
  393. /// <summary>
  394. /// Exception raised when an assertion fails.
  395. /// </summary>
  396. public class AssertionException : DesignByContractException
  397. {
  398. /// <summary>
  399. /// Assertion Exception.
  400. /// </summary>
  401. public AssertionException() {}
  402. /// <summary>
  403. /// Assertion Exception.
  404. /// </summary>
  405. public AssertionException(string message) : base(message) {}
  406. /// <summary>
  407. /// Assertion Exception.
  408. /// </summary>
  409. public AssertionException(string message, Exception inner) : base(message, inner) {}
  410. }
  411. #endregion // Exception classes
  412. } // End Design By Contract