aboutsummaryrefslogtreecommitdiffstats
path: root/ctrl/facemgr/src/common.h
blob: a73964b6d11af9dbdbd0aa28ffba9ac73ce72469 (plain)
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
87
88
89
90
91
92
93
94
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * \file common.h
 * \file Common definitions used throughout the code
 */
#ifndef FACEMGR_COMMON_H
#define FACEMGR_COMMON_H

#include <stdbool.h>
#include <stdint.h>

#include "util/types.h"
#include "util/ip_address.h"
#include "util/token.h" // XXX debug

//#define DEBUG

/* Return value conventions */
#define FACEMGR_SUCCESS 0
#define FACEMGR_FAILURE -1
#define FACEMGR_IS_ERROR(rc) (rc < 0)

/* Useful types and macros for comparisons */
typedef int(*cmp_t)(const void * x, const void * y);

#define INT_CMP(x, y) x < y ? -1 : (x == y ? 0 : 1)

/* Dump with indent */
#define INDENT(n, fmt) "%*s" fmt, n, ""
#define printfi(n, fmt, ...) printf(INDENT(n*4, fmt), ##__VA_ARGS__)

/* Boilerplate code */

#define NO_INITIALIZE(NAME)                                             \
int                                                                     \
NAME ## _initialize(NAME ## _t * obj) {                                 \
    return FACEMGR_SUCCESS;                                             \
}

#define NO_FINALIZE(NAME)                                               \
int                                                                     \
NAME ## _finalize(NAME ## _t * obj) {                                   \
    return FACEMGR_SUCCESS;                                             \
}

#define AUTOGENERATE_CREATE_FREE(NAME)                                  \
                                                                        \
NAME ## _t *                                                            \
NAME ## _create()                                                       \
{                                                                       \
    NAME ## _t * obj  = malloc(sizeof(NAME ## _t));                     \
    if (!obj)                                                           \
        goto ERR_MALLOC;                                                \
                                                                        \
    if (FACEMGR_IS_ERROR(NAME ## _initialize(obj)))                     \
        goto ERR_INIT;                                                  \
                                                                        \
    return obj;                                                         \
                                                                        \
ERR_INIT:                                                               \
    free(obj);                                                          \
ERR_MALLOC:                                                             \
    return NULL;                                                        \
}                                                                       \
                                                                        \
void                                                                    \
NAME ## _free(NAME ## _t * obj)                                         \
{                                                                       \
    if (FACEMGR_IS_ERROR(NAME ## _finalize(obj)))                       \
        (void)0; /* XXX */                                              \
    free(obj);                                                          \
}                                                                       \

#define AUTOGENERATE_DEFS(NAME)                                         \
int NAME ## _initialize(NAME ## _t *);                                  \
int NAME ## _finalize(NAME ## _t *);                                    \
NAME ## _t * NAME ## _create();                                         \
void NAME ## _free(NAME ## _t *);                                       \

#endif /* FACEMGR_COMMON_H */