[ create a new paste ] login | about

Project: murilo
Link: http://murilo.codepad.org/cPQ482Qs    [ raw code | fork ]

C++, pasted on Jul 30:
/*
 * By Murilo Adriano Vasconcelos (C) 2010
 * http://murilo.wordpress.com
 */

#ifndef POLICIES_HPP_INCLUDED
#define POLICIES_HPP_INCLUDED

#include <cerrno>    // for errno
#include <stdexcept> // for std::domain_error

namespace policies 
{

// Our policies
enum error_policy_type
{
	ignore_error = 0,
	errno_on_error,
	throw_on_error,
	user_error
};

template <int P>
struct domain_error
{
	const static int policy = P;
};

typedef domain_error<ignore_error> default_domain_policy;

// Only the declaration, must be defined somewhere
template <typename T>
T user_domain_error(const char* function, const char* message, const T& val);

template <typename T>
T raise_domain_error(const char* function, const char* message, const T& val, const policies::domain_error<ignore_error>&)
{
	// Simply ignoring the error
	return val;
}

template <typename T>
T raise_domain_error(const char* function, const char* message, const T& val, const policies::domain_error<errno_on_error>&)
{
	// Sets errno to Error DOMain
	errno = EDOM;
	return val;
}

template <typename T>
T raise_domain_error(const char* function, const char* message, const T& val, const policies::domain_error<throw_on_error>&)
{
	throw std::domain_error(message);
	
	// This will never be returned
	return val;
}

template <typename T>
T raise_domain_error(const char* function, const char* message, const T& val, const policies::domain_error<user_error>&)
{
	return user_domain_error(function, message, val);
}

}

#endif


Create a new paste based on this one


Comments: