Newer
Older
snipet / libscpp / trunk / include / scpp_assert.hpp
/* =============================================================================
 *  scpp_assert.hpp
 *  Copyright (c)  2003 - 2011  Nomura Kei
 *  LICENSE :
 *  LGPL (GNU Lesser General General Public License - Version 3,29 June 2007)
 *  http://www.gnu.org/copyleft/lesser.html
 * =============================================================================
 *
 * アサーションモジュール.
 * アサーション関数が失敗するとき, エラーメッセージを表示します.
 */
#ifndef SCPP_ASSERT_HPP
#define SCPP_ASSERT_HPP

#include <string>

#include <scpp_compiler.hpp>
#include <scpp_exception.hpp>

namespace scpp
{


/**
 * アサーションに失敗した際に throw される Error です.
 */
class AssertError : public Error
{
	public:
		AssertError() throw();
		AssertError(const AssertError& t) throw();
		AssertError(const std::string& msg) throw();
		AssertError(const std::string& msg, const char* file, int line, const char* func) throw();
		virtual ~AssertError() throw();
		const std::string& getFileName() const throw();
		int                getLineNumber() const throw();
		const std::string& getFunctionName() const throw();
	private:
		std::string fileName;
		int         lineNumber;
		std::string functionName;
};


/**
 * アサーション名前空間.
 * assertEquals, assertFalse, assertTrue, fail 等の
 * アサーションを扱います.
 */
namespace Assertion
{

void assertEquals (bool               expected, bool               actual, const char* file, int line, const char* func) throw(AssertError);
void assertEquals (char               expected, char               actual, const char* file, int line, const char* func) throw(AssertError);
void assertEquals (double             expected, double             actual, const char* file, int line, const char* func) throw(AssertError);
void assertEquals (float              expected, float              actual, const char* file, int line, const char* func) throw(AssertError);
void assertEquals (int                expected, int                actual, const char* file, int line, const char* func) throw(AssertError);
void assertEquals (long               expected, long               actual, const char* file, int line, const char* func) throw(AssertError);
void assertEquals (const std::string& expected, const std::string& actual, const char* file, int line, const char* func) throw(AssertError);
void assertEquals (const char*        expected, const char*        actual, const char* file, int line, const char* func) throw(AssertError);
void assertEquals (const char*        expected, const std::string& actual, const char* file, int line, const char* func) throw(AssertError);
void assertEquals (const std::string& expected, const char*        actual, const char* file, int line, const char* func) throw(AssertError);
void assertTrue   (bool condition                                        , const char* file, int line, const char* func) throw(AssertError);
void assertFalse  (bool condition                                        , const char* file, int line, const char* func) throw(AssertError);
void assertNull   (void* obj                                             , const char* file, int line, const char* func) throw(AssertError);
void assertNotNull(void* obj                                             , const char* file, int line, const char* func) throw(AssertError);
void fail(                                                                 const char* file, int line, const char* func) throw(AssertError);


}	// namespace Assertion
}	// namespace scpp


/**
 * 指定された actual が, expected と同一でない場合, AssertError を throw します.
 *
 * @param expected 期待する値
 * @param actual   比較する値
 */
#define assertEquals(expected, actual)  assertEquals(expected, actual, __FILE__, __LINE__, __FUNCTION__)


/**
 * 指定された condition が true でない場合, AssertError を throw します.
 *
 * @param condition 比較する値
 */
#define assertTrue(condition)           assertTrue(condition         , __FILE__, __LINE__, __FUNCTION__)


/**
 * 指定された condition が false でない場合, AssertError を throw します.
 *
 * @param condition 比較する値
 */
#define assertFalse(condition)          assertFalse(condition        , __FILE__, __LINE__, __FUNCTION__)


/**
 * 指定された obj が 0 (null) でない場合, AssertError を throw します.
 *
 * @param obj null か否か確認する値
 */
#define assertNull(obj)                 assertNull(obj               , __FILE__, __LINE__, __FUNCTION__)


/**
 * 指定された obj が 0 (null) の場合, AssertError を throw します.
 *
 * @param obj null か否か確認する値
 */
#define assertNotNull(obj)              assertNotNull(obj            , __FILE__, __LINE__, __FUNCTION__)


/**
 * 常に AssertError を throw します.
 */
#define fail()                          fail(                          __FILE__, __LINE__, __FUNCTION__)


#endif	// SCPP_ASSERT_HPP