libbluray | branch: master | hpi1 <[email protected]> | Wed Sep 12 11:40:57 2012 +0300| [b176a16cdf0475aaf902f278ffc83a66a96b5446] | committer: hpi1
automatic cast from void* (malloc/calloc/realloc) for C++ compilers > http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=b176a16cdf0475aaf902f278ffc83a66a96b5446 --- src/util/macro.h | 34 ++++++++++++++++++++++++++++++++++ src/util/strutl.c | 2 ++ 2 files changed, 36 insertions(+) diff --git a/src/util/macro.h b/src/util/macro.h index 81a08e3..7df96e8 100644 --- a/src/util/macro.h +++ b/src/util/macro.h @@ -30,4 +30,38 @@ #define MKINT_BE32(X) ( (X)[0] << 24 | (X)[1] << 16 | (X)[2] << 8 | (X)[3] ) #define X_FREE(X) ( free(X), X = NULL ) +/* + * automatic cast from void* (malloc/calloc/realloc) + */ + +#ifdef __cplusplus + +template <typename T> +class auto_cast_wrapper +{ +public: + template <typename R> friend auto_cast_wrapper<R> auto_cast(const R& x); + template <typename U> operator U() { return static_cast<U>(p); } + +private: + auto_cast_wrapper(const T& x) : p(x) {} + auto_cast_wrapper(const auto_cast_wrapper& o) : p(o.p) {} + + auto_cast_wrapper& operator=(const auto_cast_wrapper&); + + const T& p; +}; + +template <typename R> +auto_cast_wrapper<R> auto_cast(const R& x) +{ + return auto_cast_wrapper<R>(x); +} + +# define calloc(n,s) auto_cast(calloc(n,s)) +# define malloc(s) auto_cast(malloc(s)) +# define realloc(p,s) auto_cast(realloc(p,s)) +#endif /* __cplusplus */ + + #endif /* MACRO_H_ */ diff --git a/src/util/strutl.c b/src/util/strutl.c index 9831ebc..ea29067 100644 --- a/src/util/strutl.c +++ b/src/util/strutl.c @@ -19,6 +19,8 @@ #include "strutl.h" +#include "macro.h" + #include <stdio.h> #include <stdarg.h> #include <stdlib.h> _______________________________________________ libbluray-devel mailing list [email protected] http://mailman.videolan.org/listinfo/libbluray-devel
