cfengine  3.15.4
About: CFEngine is a configuration management system for configuring and maintaining Unix-like computers (using an own high level policy language). Community version.
  Fossies Dox: cfengine-3.15.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

condition_macros.h
Go to the documentation of this file.
1 #ifndef __CONDITION_MACROS_H__
2 #define __CONDITION_MACROS_H__
3 
4 #include <assert.h>
5 #include <stdbool.h>
6 
7 // This file contains macros to use for error checking, assertions,
8 // abort, return, etc. Each macro should have a comment about when to use it.
9 // The normal assert() macro should only be used to catch programmer mistakes,
10 // things which should never happen, even for weird file/network inputs.
11 // For example, a function which doesn't accept NULL pointer arguments,
12 // should assert that the parameter is not NULL.
13 
14 // Used when you want to assert a precondtion (catch programmer mistake)
15 // but also want to handle the error if it ever happens in a release build.
16 // Useful if you are unsure if you need to handle the error, for example
17 // if you are adding assertions to older code. Also useful in cases where
18 // you know you want both, for example when network or file input can trigger
19 // the condition
20 #ifndef assert_or_return
21 #define assert_or_return(expr, val) { \
22  assert(expr); \
23  if (!(expr)) \
24  { \
25  return val; \
26  } \
27 }
28 #endif
29 
30 // Similar to assert_or_return, except you put it inside the if
31 // body which handles the error in release builds
32 #ifndef debug_abort_if_reached
33 #define debug_abort_if_reached() { \
34  assert(false); \
35 }
36 #endif
37 
38 // libntech static assert (compile time check):
39 // has nt_ prefix to not be confused with static_assert
40 // Why not just static_assert?
41 // - It is already defined on some platforms (recent RHEL and Ubuntu),
42 // but in incompatible ways. On RHEL second arg (message) is required,
43 // on Ubuntu it is not.
44 // Why not redefine static_assert (undef + define)?
45 // - Hard to know which static_assert you are using. Include ordering becomes
46 // very important. Error messages are confusing if you end up using the wrong
47 // static_assert. Error message would only appear on some platforms, not others.
48 
49 #ifdef _Static_assert
50 // Note: The message here is not really necessary,
51 // most compilers will include this information anyway.
52 #define nt_static_assert(x) _Static_assert(x, __FILE__ ":" TO_STRING(__LINE__) ": (" #x ") -> false")
53 #else
54 #define nt_static_assert(x) { \
55  switch (0) { \
56  case 0: /* Cause duplicate case if next is 0 as well */ \
57  break; \
58  case x: /* Error if 0 or if non-const expression */ \
59  break; \
60  } \
61 }
62 #endif
63 
64 #endif