33 #ifndef OTBR_COMMON_CODE_UTILS_HPP_
34 #define OTBR_COMMON_CODE_UTILS_HPP_
37 #define OTBR_LOG_TAG "UTILS"
55 #define OTBR_ALIGNED(aMem, aAlignType) \
56 reinterpret_cast<aAlignType>( \
57 ((reinterpret_cast<unsigned long>(aMem) + sizeof(aAlignType) - 1) / sizeof(aAlignType)) * sizeof(aAlignType))
59 #ifndef CONTAINING_RECORD
61 #define myoffsetof(s, m) (((size_t) & (((s *)BASE)->m)) - BASE)
62 #define CONTAINING_RECORD(address, type, field) ((type *)((uint8_t *)(address)-myoffsetof(type, field)))
73 #define SuccessOrExit(aStatus) \
90 #define SuccessOrDie(aStatus, aMessage) \
95 otbrLogEmerg("FAILED %s:%d - %s", __FILE__, __LINE__, aMessage); \
110 #define VerifyOrExit(aCondition, ...) \
128 #define VerifyOrDie(aCondition, aMessage) \
133 otbrLogEmerg("FAILED %s:%d - %s", __FILE__, __LINE__, aMessage); \
150 #define ExitNow(...) \
158 #define OTBR_UNUSED_VARIABLE(variable) ((void)(variable))
160 template <
typename T,
typename... Args> std::unique_ptr<T> MakeUnique(Args &&... args)
162 return std::unique_ptr<T>(
new T(std::forward<Args>(args)...));
184 Optional(T aValue) { SetValue(aValue); }
192 constexpr
const T *operator->(
void)
const {
return &GetValue(); }
194 constexpr
const T &operator*(
void)
const {
return GetValue(); }
196 constexpr
bool HasValue(
void)
const {
return mHasValue; }
199 T &GetValue(
void)
const
202 return *
const_cast<T *
>(
reinterpret_cast<const T *
>(&mStorage));
205 void ClearValue(
void)
214 void SetValue(
const T &aValue)
217 new (&mStorage) T(aValue);
221 void AssignFrom(
const Optional &aOther)
224 if (aOther.mHasValue)
226 SetValue(aOther.GetValue());
230 alignas(T)
unsigned char mStorage[
sizeof(T)];
231 bool mHasValue =
false;
234 #endif // OTBR_COMMON_CODE_UTILS_HPP_