Defines | Typedefs | Enumerations | Functions
common.h File Reference
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>

Go to the source code of this file.

Defines

#define CT_ASSERT(e)   extern char (*CT_ASSERT(void)) [sizeof(char[1 - 2*!(e)])]
#define NELEMS(x)   (sizeof(x) / sizeof(x[0]))
#define LOG_ERR(fmt,...)
#define VALIDATE_VTABLE_FN(obj_h, private_h, vtable, fn, rc)
#define INHERIT_VTABLE_FN(parent_vtable, child_vtable, fn, do_null_check, rc)

Typedefs

typedef enum my_rc_e_ my_rc_e

Enumerations

enum  my_rc_e_ {
  MY_RC_E_INVALID, MY_RC_E_SUCCESS, MY_RC_E_EINVAL, MY_RC_E_ENOMEM,
  MY_RC_E_MAX
}

Functions

bool my_rc_e_is_notok (my_rc_e rc)
bool my_rc_e_is_ok (my_rc_e rc)
bool my_rc_e_is_valid (my_rc_e rc)
const char * my_rc_e_get_string (my_rc_e rc)

Detailed Description

Author:
Matt Miller <matt@matthewjmiller.net>

LICENSE

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

DESCRIPTION

This is an interface for some common declarations.

Definition in file common.h.


Define Documentation

#define CT_ASSERT (   e)    extern char (*CT_ASSERT(void)) [sizeof(char[1 - 2*!(e)])]

Compile time assert macro from: http://www.pixelbeat.org/programming/gcc/static_assert.html

Definition at line 38 of file common.h.

#define INHERIT_VTABLE_FN (   parent_vtable,
  child_vtable,
  fn,
  do_null_check,
  rc 
)
Value:
do { \
    if (NULL == child_vtable->fn) { \
        child_vtable->fn = parent_vtable->fn; \
        if (do_null_check && (NULL == child_vtable->fn)) { \
            LOG_ERR("Invalid input, " #fn "(%p)", child_vtable->fn); \
            rc = MY_RC_E_EINVAL; \
            goto err_exit; \
        } \
    } \
} while (0)

If the function in the child's table is NULL, then inherit the function from the parent table. In do_null_check is true, then it is considered an error condition if the function is NULL after inheritance (e.g., when the base class is setting the vtable, all functions should be non-NULL). If the error condition is reached, we set the rc and goto an err_exit label.

Definition at line 97 of file common.h.

#define LOG_ERR (   fmt,
  ... 
)
Value:
do { \
    printf("(%s:%u) ERROR: " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
} while (0)

Display an error message.

Definition at line 48 of file common.h.

#define NELEMS (   x)    (sizeof(x) / sizeof(x[0]))

Determine the number of elements in an array.

Definition at line 43 of file common.h.

#define VALIDATE_VTABLE_FN (   obj_h,
  private_h,
  vtable,
  fn,
  rc 
)
Value:
do { \
    if (NULL == obj_h) { \
        LOG_ERR("Invalid input, " #obj_h "(%p)", obj_h); \
        rc = MY_RC_E_EINVAL;  \
        break; \
    } \
\
    if (NULL == obj_h->private_h) { \
        LOG_ERR("Invalid input, " #obj_h "(%p) " #private_h "(%p)", obj_h, \
                obj_h->private_h); \
        rc = MY_RC_E_EINVAL;  \
        break; \
    } \
\
    if (NULL == obj_h->private_h->vtable) { \
        LOG_ERR("Invalid input, " #obj_h "(%p) " #private_h "(%p) " #vtable \
                "(%p)", obj_h, obj_h->private_h, obj_h->private_h->vtable); \
        rc = MY_RC_E_EINVAL;  \
        break; \
    } \
\
    if (NULL == obj_h->private_h->vtable->fn) { \
        LOG_ERR("Invalid input, " #obj_h "(%p) " #private_h "(%p) " #vtable \
                "(%p) " #fn "(%p)",  obj_h, obj_h->private_h, \
                obj_h->private_h->vtable, obj_h->private_h->vtable->fn); \
        rc = MY_RC_E_EINVAL;  \
        break; \
    } \
} while (0)

Validate that a function in an object's virtual table exists. If not, set the return code. Callers should set the return code to MY_RC_E_SUCCESS prior to calling the macro and check it after the macro is executed to make sure it is not an error return code.

Definition at line 59 of file common.h.


Typedef Documentation

typedef enum my_rc_e_ my_rc_e

Return codes used to indicate whether a function call was successful.


Enumeration Type Documentation

enum my_rc_e_

Return codes used to indicate whether a function call was successful.

Enumerator:
MY_RC_E_INVALID 

Invalid return code, should never be used

MY_RC_E_SUCCESS 

Successful return

MY_RC_E_EINVAL 

Function received an invalid input

MY_RC_E_ENOMEM 

Function failed to allocate memory

MY_RC_E_MAX 

Max return code for bounds testing

Definition at line 112 of file common.h.


Function Documentation

const char* my_rc_e_get_string ( my_rc_e  rc)

Get a string representation of the return code.

Parameters:
rcThe return code
Returns:
A string representation of the return code or "__Invalid__" if an invalid return code is input.

Definition at line 88 of file common.c.

bool my_rc_e_is_notok ( my_rc_e  rc)

Indicates whether the return code is not in error.

Parameters:
rcThe return code to check
Returns:
true if the return code is not in error.

Definition at line 51 of file common.c.

bool my_rc_e_is_ok ( my_rc_e  rc)

Indicates whether the return code is in error.

Parameters:
rcThe return code to check
Returns:
true if the return code is not in error.

Definition at line 63 of file common.c.

bool my_rc_e_is_valid ( my_rc_e  rc)

Indicates whether the return code is valid.

Parameters:
rcThe return code to check
Returns:
true if the return code is valid.

Definition at line 75 of file common.c.

 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines