aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/debug.c
blob: 0b4734098d4e3adce1a59c4198c5ee9d0f4c7caa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef NDEBUG

#include "../macros.h"
#include "ui.internal.h"

#include <stdio.h>

const char *ui__debug_types[] = {
#define WT_DEF(_t) "UI__WINDOW_TYPE_" #_t
  UI__FOREACH_WINDOW_TYPE(WT_DEF)
#undef WT_DEF
};

size_t ui__debug_type_count = sizeof(ui__debug_types) / sizeof(ui__debug_types[0]);

const char *ui__debug_type_to_str(unsigned type)
{
  if (type >= ui__debug_type_count) return "???";
  return ui__debug_types[type];
}

struct ui_window_base *ui__check_cast_to_base(void *obj, const char *file, const char *func, int line)
{
  umps_unused(file);
  umps_unused(func);
  umps_unused(line);
  return obj; /* this cast always succeeds */
}

#define UMPS__DEBUG_DO_ERROR(_o, _fi, _fn, _ln, _t) \
  struct ui_window_base *base = _o;                                             \
  if (base->type != _t) {                                                       \
    fprintf(stderr, "!!!!!\n!!!!!\n!!!!! UMPS UI bad cast at %s:%d %s (expect %s got %s:%u) !!!!!\n!!!!!\n!!!!!\n", \
      _fi, _ln, _fn, #_t, ui__debug_type_to_str(base->type), base->type);       \
    umps_trap;                                                                  \
  }                                                                             \
  return _o;

struct ui_window_leaf *ui__check_cast_to_leaf(void *obj, const char *file, const char *func, int line)
{
  UMPS__DEBUG_DO_ERROR(obj, file, func, line, UI__WINDOW_TYPE_LEAF);
}

struct ui_window_dock *ui__check_cast_to_dock(void *obj, const char *file, const char *func, int line)
{
  UMPS__DEBUG_DO_ERROR(obj, file, func, line, UI__WINDOW_TYPE_DOCK);
}

struct ui_window_root *ui__check_cast_to_root(void *obj, const char *file, const char *func, int line)
{
  UMPS__DEBUG_DO_ERROR(obj, file, func, line, UI__WINDOW_TYPE_ROOT);
}

struct ui_window_menu *ui__check_cast_to_menu(void *obj, const char *file, const char *func, int line)
{
  UMPS__DEBUG_DO_ERROR(obj, file, func, line, UI__WINDOW_TYPE_MENU);
}

int ui__debug_nc_check_int(int in, const char *call, const char *file, const char *func, int line)
{
  if (in == ERR) {
    fprintf(stderr, "!!!!!\n!!!!!\n!!!!! UMPS checked ncurses call \"%s\" failed (returned ERR) at %s:%d %s !!!!!\n!!!!!\n!!!!!\n",
            call, file, line, func);
    umps_trap;
  }

  return in;
}

WINDOW *ui__debug_nc_check_ptr(WINDOW *ptr, const char *call, const char *file, const char *func, int line)
{
  if (ptr == NULL) {
    fprintf(stderr, "!!!!!\n!!!!!\n!!!!! UMPS checked ncurses call \"%s\" failed (returned NULL) at %s:%d %s !!!!!\n!!!!!\n!!!!!\n",
            call, file, line, func);
    umps_trap;
  }

  return ptr;
}

#else

/* the file must have a declaration */
void umps__debug_do_nothing(void);

#endif