Blame src/enforce.hpp

Packit 01d647
// ********************************************************* -*- C++ -*-
Packit 01d647
/*
Packit 01d647
 * Copyright (C) 2004-2018 Exiv2 authors
Packit 01d647
 * This program is part of the Exiv2 distribution.
Packit 01d647
 *
Packit 01d647
 * This program is free software; you can redistribute it and/or
Packit 01d647
 * modify it under the terms of the GNU General Public License
Packit 01d647
 * as published by the Free Software Foundation; either version 2
Packit 01d647
 * of the License, or (at your option) any later version.
Packit 01d647
 *
Packit 01d647
 * This program is distributed in the hope that it will be useful,
Packit 01d647
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 01d647
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 01d647
 * GNU General Public License for more details.
Packit 01d647
 *
Packit 01d647
 * You should have received a copy of the GNU General Public License
Packit 01d647
 * along with this program; if not, write to the Free Software
Packit 01d647
 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
Packit 01d647
 */
Packit 01d647
/*!
Packit 01d647
  @file    enforce.hpp
Packit 01d647
  @brief   Port of D's enforce() to C++ & Exiv2
Packit 01d647
  @author  Dan Čermák (D4N)
Packit 01d647
           dan.cermak@cgc-instruments.com
Packit 01d647
  @date    11-March-18, D4N: created
Packit 01d647
 */
Packit 01d647
Packit 01d647
#include <string>
Packit 01d647
Packit 01d647
#include "error.hpp"
Packit 01d647
Packit 01d647
/*!
Packit 01d647
 * @brief Ensure that condition is true, otherwise throw an exception of the
Packit 01d647
 * type exception_t
Packit 01d647
 *
Packit 01d647
 * @tparam exception_t  Exception type that is thrown, must provide a
Packit 01d647
 * constructor that accepts a single argument to which arg1 is forwarded.
Packit 01d647
 *
Packit 01d647
 * @todo once we have C++>=11 use variadic templates and std::forward to remove
Packit 01d647
 * all overloads of enforce
Packit 01d647
 */
Packit 01d647
template <typename exception_t, typename T>
Packit 01d647
inline void enforce(bool condition, const T& arg1)
Packit 01d647
{
Packit 01d647
    if (!condition) {
Packit 01d647
        throw exception_t(arg1);
Packit 01d647
    }
Packit 01d647
}
Packit 01d647
Packit 01d647
/*!
Packit 01d647
 * @brief Ensure that condition is true, otherwise throw an Exiv2::Error with
Packit 01d647
 * the given error_code.
Packit 01d647
 */
Packit 01d647
inline void enforce(bool condition, Exiv2::ErrorCode err_code)
Packit 01d647
{
Packit 01d647
    if (!condition) {
Packit 01d647
        throw Exiv2::Error(err_code);
Packit 01d647
    }
Packit 01d647
}
Packit 01d647
Packit 01d647
/*!
Packit 01d647
 * @brief Ensure that condition is true, otherwise throw an Exiv2::Error with
Packit 01d647
 * the given error_code & arg1.
Packit 01d647
 */
Packit 01d647
template <typename T>
Packit 01d647
inline void enforce(bool condition, Exiv2::ErrorCode err_code, const T& arg1)
Packit 01d647
{
Packit 01d647
    if (!condition) {
Packit 01d647
        throw Exiv2::Error(err_code, arg1);
Packit 01d647
    }
Packit 01d647
}
Packit 01d647
Packit 01d647
/*!
Packit 01d647
 * @brief Ensure that condition is true, otherwise throw an Exiv2::Error with
Packit 01d647
 * the given error_code, arg1 & arg2.
Packit 01d647
 */
Packit 01d647
template <typename T, typename U>
Packit 01d647
inline void enforce(bool condition, Exiv2::ErrorCode err_code, const T& arg1, const U& arg2)
Packit 01d647
{
Packit 01d647
    if (!condition) {
Packit 01d647
        throw Exiv2::Error(err_code, arg1, arg2);
Packit 01d647
    }
Packit 01d647
}
Packit 01d647
Packit 01d647
/*!
Packit 01d647
 * @brief Ensure that condition is true, otherwise throw an Exiv2::Error with
Packit 01d647
 * the given error_code, arg1, arg2 & arg3.
Packit 01d647
 */
Packit 01d647
template <typename T, typename U, typename V>
Packit 01d647
inline void enforce(bool condition, Exiv2::ErrorCode err_code, const T& arg1, const U& arg2, const V& arg3)
Packit 01d647
{
Packit 01d647
    if (!condition) {
Packit 01d647
        throw Exiv2::Error(err_code, arg1, arg2, arg3);
Packit 01d647
    }
Packit 01d647
}