[Spice-devel] [PATCH spice-streaming-agent 2/6] Separate ERROR macro in a different utility header

Lukáš Hrázký lhrazky at redhat.com
Mon Feb 19 16:12:30 UTC 2018


On Mon, 2018-02-19 at 15:52 +0000, Frediano Ziglio wrote:
> Allows to reuse it.
> 
> Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
> ---
>  src/Makefile.am        |  1 +
>  src/mjpeg-fallback.cpp |  7 +------
>  src/utils.hpp          | 18 ++++++++++++++++++
>  3 files changed, 20 insertions(+), 6 deletions(-)
>  create mode 100644 src/utils.hpp
> 
> diff --git a/src/Makefile.am b/src/Makefile.am
> index 3717b5c..ba3b1bf 100644
> --- a/src/Makefile.am
> +++ b/src/Makefile.am
> @@ -55,4 +55,5 @@ spice_streaming_agent_SOURCES = \
>  	mjpeg-fallback.hpp \
>  	jpeg.cpp \
>  	jpeg.hpp \
> +	utils.hpp \
>  	$(NULL)
> diff --git a/src/mjpeg-fallback.cpp b/src/mjpeg-fallback.cpp
> index cf704c6..0f31834 100644
> --- a/src/mjpeg-fallback.cpp
> +++ b/src/mjpeg-fallback.cpp
> @@ -6,6 +6,7 @@
>  
>  #include <config.h>
>  #include "mjpeg-fallback.hpp"
> +#include "utils.hpp"
>  
>  #include <cstring>
>  #include <exception>
> @@ -19,12 +20,6 @@
>  
>  using namespace spice::streaming_agent;
>  
> -#define ERROR(args) do { \
> -    std::ostringstream _s; \
> -    _s << args; \
> -    throw std::runtime_error(_s.str()); \
> -} while(0)
> -
>  static inline uint64_t get_time()
>  {
>      timespec now;
> diff --git a/src/utils.hpp b/src/utils.hpp
> new file mode 100644
> index 0000000..1e43eff
> --- /dev/null
> +++ b/src/utils.hpp
> @@ -0,0 +1,18 @@
> +/* Miscellaneous utilities
> + *
> + * \copyright
> + * Copyright 2018 Red Hat Inc. All rights reserved.
> + */
> +#ifndef SPICE_STREAMING_AGENT_UTILS_HPP
> +#define SPICE_STREAMING_AGENT_UTILS_HPP
> +
> +#include <stdexcept>
> +#include <sstream>
> +
> +#define ERROR(args) do { \
> +    std::ostringstream _s; \
> +    _s << args; \
> +    throw std::runtime_error(_s.str()); \
> +} while(0)
> +
> +#endif // SPICE_STREAMING_AGENT_UTILS_HPP

Can we please not do this :) It isn't such a chore to throw the
exceptions directly. This adds a level of indirection (code-wise) and
macros are (personal opinion alert) best avoided in C++ unless you
absolutely have to...

Lukas


More information about the Spice-devel mailing list