openthread-br  0.3.0-72c0388
Classes | Macros | Functions
code_utils.hpp File Reference
#include <assert.h>
#include <memory>
#include <stdlib.h>
#include "common/logging.hpp"

Go to the source code of this file.

Classes

class  NonCopyable
 
class  Optional< T >
 

Macros

#define OTBR_LOG_TAG   "UTILS"
 
#define OTBR_ALIGNED(aMem, aAlignType)
 
#define BASE   0x1
 
#define myoffsetof(s, m)   (((size_t) & (((s *)BASE)->m)) - BASE)
 
#define CONTAINING_RECORD(address, type, field)   ((type *)((uint8_t *)(address)-myoffsetof(type, field)))
 
#define SuccessOrExit(aStatus)
 
#define SuccessOrDie(aStatus, aMessage)
 
#define VerifyOrExit(aCondition, ...)
 
#define VerifyOrDie(aCondition, aMessage)
 
#define ExitNow(...)
 
#define OTBR_NOOP
 
#define OTBR_UNUSED_VARIABLE(variable)   ((void)(variable))
 

Functions

template<typename T , typename... Args>
std::unique_ptr< T > MakeUnique (Args &&... args)
 

Detailed Description

This file includes utility macros for coding.

Macro Definition Documentation

◆ ExitNow

#define ExitNow (   ...)
Value:
do \
{ \
__VA_ARGS__; \
goto exit; \
} while (false)

This unconditionally executes ... and branches to the local label 'exit'.

Note
The use of this interface implies neither success nor failure for the overall exit status of the enclosing function body.
Parameters
[in]...An optional expression or block to execute when the assertion fails.

◆ OTBR_ALIGNED

#define OTBR_ALIGNED (   aMem,
  aAlignType 
)
Value:
reinterpret_cast<aAlignType>( \
((reinterpret_cast<unsigned long>(aMem) + sizeof(aAlignType) - 1) / sizeof(aAlignType)) * sizeof(aAlignType))

This aligns the pointer to aAlignType.

Parameters
[in]aMemA pointer to arbitrary memory.
[in]aAlignTypeThe type to align with and convert the pointer to this type.
Returns
A pointer to aligned memory.

◆ SuccessOrDie

#define SuccessOrDie (   aStatus,
  aMessage 
)
Value:
do \
{ \
if ((aStatus) != 0) \
{ \
otbrLogEmerg("FAILED %s:%d - %s", __FILE__, __LINE__, aMessage); \
exit(-1); \
} \
} while (false)

This macro verifies a given error status to be successful (compared against value zero (0)), otherwise, it emits a given error messages and exits the program.

Parameters
[in]aStatusA scalar error status to be evaluated against zero (0).
[in]aMessageA message (text string) to print on failure.

◆ SuccessOrExit

#define SuccessOrExit (   aStatus)
Value:
do \
{ \
if ((aStatus) != 0) \
{ \
goto exit; \
} \
} while (false)

This checks for the specified status, which is expected to commonly be successful, and branches to the local label 'exit' if the status is unsuccessful.

Parameters
[in]aStatusA scalar status to be evaluated against zero (0).

◆ VerifyOrDie

#define VerifyOrDie (   aCondition,
  aMessage 
)
Value:
do \
{ \
if (!(aCondition)) \
{ \
otbrLogEmerg("FAILED %s:%d - %s", __FILE__, __LINE__, aMessage); \
exit(-1); \
} \
} while (false)

This macro checks for the specified condition, which is expected to commonly be true, and both prints the message and terminates the program if the condition is false.

Parameters
[in]aConditionThe condition to verify
[in]aMessageA message (text string) to print on failure.

◆ VerifyOrExit

#define VerifyOrExit (   aCondition,
  ... 
)
Value:
do \
{ \
if (!(aCondition)) \
{ \
__VA_ARGS__; \
goto exit; \
} \
} while (false)

This checks for the specified condition, which is expected to commonly be true, and both executes ... and branches to the local label 'exit' if the condition is false.

Parameters
[in]aConditionA Boolean expression to be evaluated.
[in]...An expression or block to execute when the assertion fails.