Commit dc60eb97 authored by Joseph Mirabel's avatar Joseph Mirabel Committed by Joseph Mirabel
Browse files

Add macro HPP_THROW(TYPE,MSG) to throw exceptions using string stream

parent f900f376
......@@ -43,6 +43,7 @@ SET(${PROJECT_NAME}_HEADERS
include/hpp/util/debug.hh
include/hpp/util/doc.hh
include/hpp/util/exception.hh
include/hpp/util/exception-factory.hh
include/hpp/util/indent.hh
include/hpp/util/pointer.hh
include/hpp/util/portability.hh
......
//
// Copyright (C) 2016 by Joseph Mirabel, CNRS.
//
// This file is part of the hpp-util.
//
// This software is provided "as is" without warranty of any kind,
// either expressed or implied, including but not limited to the
// implied warranties of fitness for a particular purpose.
//
// See the COPYING file for more information.
#ifndef HPP_UTIL_EXCEPTION_FACTORY_HH
# define HPP_UTIL_EXCEPTION_FACTORY_HH
# include <sstream>
# include <hpp/util/config.hh>
namespace hpp
{
/// \brief Class to ease exception creation.
struct ThrowException {};
template <typename exception> struct ExceptionFactory;
namespace internal {
template <typename exception, typename In>
struct conditional_insertion_operator {
typedef ExceptionFactory<exception>& type;
static inline type run(ExceptionFactory<exception>& be, const In& t) { be.ss << t; return be; }
};
}
template <typename exception>
struct HPP_UTIL_DLLAPI ExceptionFactory
{
std::stringstream ss;
template <typename T> inline
typename internal::conditional_insertion_operator<exception, T>::type
operator<< (const T& t) {
return internal::conditional_insertion_operator<exception, T>::run (*this, t);
}
};
// ----------------------------------------
// ExceptionFactory - template specialization
// ----------------------------------------
namespace internal {
template <typename exception>
struct conditional_insertion_operator<exception, ThrowException> {
typedef exception type;
static inline type run(ExceptionFactory<exception>& be, const ThrowException&) { return exception(be.ss.str()); }
};
}
} // end of namespace hpp.
/// \brief Throw a HPP exception.
# define HPP_THROW(TYPE, MSG) \
throw ::hpp::ExceptionFactory<TYPE>() << MSG << ::hpp::ThrowException()
/// \brief Throw a HPP exception.
# define HPP_THROW_WITH_LINEINFO(TYPE, MSG) \
HPP_THROW(TYPE,MSG << " at " << __FILE__ << ":" << __LINE__)
#endif //! HPP_UTIL_EXCEPTION_FACTORY_HH
......@@ -37,4 +37,5 @@ ENDMACRO(DEFINE_TEST)
DEFINE_TEST(simple-test hpp-util)
DEFINE_TEST(assertion hpp-util)
DEFINE_TEST(exception hpp-util)
DEFINE_TEST(exception-factory hpp-util)
DEFINE_TEST(debug hpp-util)
// Copyright (C) 2016 by Joseph Mirabel.
//
// This file is part of the hpp-util.
//
// hpp-util is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// hpp-util 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with hpp-util. If not, see <http://www.gnu.org/licenses/>.
#include <hpp/util/exception-factory.hh>
#include "config.h"
#include <cassert>
#include <stdexcept>
#include <iostream>
#include "common.hh"
int run_test ();
int run_test ()
{
try
{
throw ::hpp::ExceptionFactory<std::runtime_error>() << "manually built std::runtime_error" << ::hpp::ThrowException();
assert (0);
}
catch (const std::exception& exception)
{
std::cout << "Caught " << exception.what() << std::endl;
}
try
{
HPP_THROW(std::runtime_error,"std::runtime_error");
assert (0);
}
catch (const std::exception& exception)
{
std::cout << "Caught " << exception.what() << std::endl;
}
try
{
double d = 12; std::string foo = "foo";
HPP_THROW_WITH_LINEINFO (std::runtime_error,
"std::runtime_error using operator<< " << foo << " " << d);
assert (0);
}
catch (const std::exception& exception)
{
std::cout << "Caught " << exception.what() << std::endl;
}
return 0;
}
GENERATE_TEST ()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment