/*! \file yof_types.hpp \brief 汎用的な型、テンプレートなどの定義 \author Kenta Yoshimura $Id$ Definition Params: YOF_NO_GLOBAL_VARIABLES YOF_NO_TOUT */ /* Copyright (C) 2003-2005 Kenta Yoshimura. All Rights Reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY AUTHOR PROJECT ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef YOF_TYPES_HPP #define YOF_TYPES_HPP #if !defined(_MSC_VER) | _MSC_VER > 1000 #pragma once #endif #if defined(_MSC_VER) && _MSC_VER <= 1200 # pragma warning( disable : 4786 ) // warning C4786: 識別子が '255' 文字に切り捨てられました #endif #include #include #include #ifdef YOF_NO_GLOBAL_VARIABLES # undef YOF_NO_TOUT # define YOF_NO_TOUT #endif #if defined(_MSC_VER) && _MSC_VER <= 1200 #define yof_typename #else // _MSC_VER > 1200 || other compiler #define yof_typename typename #endif #include "yof_integer.hpp" #ifdef max # undef max #endif #ifdef min # undef min #endif #ifdef maximum # undef maximum #endif #ifdef minimum # undef minimum #endif namespace yoffy { template< typename T > struct bitwise_and : public std::binary_function< T, T, T > { T operator ()( const T & src1, const T & src2 ) const { return src1 & src2; }; }; template< typename T > struct bitwise_or : public std::binary_function< T, T, T > { T operator ()( const T & src1, const T & src2 ) const { return src1 | src2; }; }; template< typename T > struct bitwise_xor : public std::binary_function< T, T, T > { T operator ()( const T & src1, const T & src2 ) const { return src1 ^ src2; }; }; template< typename T > struct power : public std::binary_function< T, T, T > { T operator ()( const T src1, const T src2 ) const { return ::pow( double(src1), double(src2) ); }; }; #ifdef _MSC_VER template<> struct power< int > : public std::binary_function< int, int, double > { double operator ()( const int src1, const int src2 ) const { return ::pow( double(src1), double(src2) ); }; }; template<> struct power< unsigned int > : public std::binary_function< unsigned int, unsigned int, double > { double operator ()( const unsigned int src1, const unsigned int src2 ) const { return ::pow( double(src1), double(src2) ); }; }; template<> struct power< int32_t > : public std::binary_function< int32_t, int32_t, double > { double operator ()( const int32_t src1, const int32_t src2 ) const { return ::pow( double(src1), double(src2) ); }; }; template<> struct power< uint32_t > : public std::binary_function< uint32_t, uint32_t, double > { double operator ()( const uint32_t src1, const uint32_t src2 ) const { return ::pow( double(src1), double(src2) ); }; }; #endif // _MSC_VER template< typename T > struct maximum : public std::binary_function< T, T, T > { yof_forceinline T operator ()( const T & src1, const T & src2 ) const { return src1 >= src2 ? src1 : src2; }; }; template< typename T > struct minimum : public std::binary_function< T, T, T > { T operator ()( const T & src1, const T & src2 ) const { return src1 <= src2 ? src1 : src2; }; }; template< typename T > struct takeleft : public std::binary_function< T, T, T > { T operator ()( const T & src1, const T & src2 ) const { return src1; }; }; template< typename T > struct takeright : public std::binary_function< T, T, T > { T operator ()( const T & src1, const T & src2 ) const { return src2; }; }; template< typename T > struct squared_length : public std::binary_function< T, T, T > { yof_forceinline T operator ()( const T & x, const T & y ) const { return x * x + y * y; }; }; template< typename T > struct length : public std::binary_function< T, T, T > { yof_forceinline T operator ()( const T & x, const T & y ) const { return T(::sqrt( x * x + y * y )); }; }; #ifdef _MSC_VER template<> struct length< char > : public std::binary_function< char, char, float > { yof_forceinline float operator ()( const char & x, const char & y ) const { return ::sqrt( float(x * x + y * y) ); }; }; template<> struct length< int8_t > : public std::binary_function< int8_t, int8_t, float > { yof_forceinline float operator ()( const int8_t & x, const int8_t & y ) const { return ::sqrt( float(x * x + y * y) ); }; }; template<> struct length< uint8_t > : public std::binary_function< uint8_t, uint8_t, float > { yof_forceinline float operator ()( const uint8_t & x, const uint8_t & y ) const { return ::sqrt( float(x * x + y * y) ); }; }; template<> struct length< int16_t > : public std::binary_function< int16_t, int16_t, float > { yof_forceinline float operator ()( const int16_t & x, const int16_t & y ) const { return ::sqrt( float(x * x + y * y) ); }; }; template<> struct length< uint16_t > : public std::binary_function< uint16_t, uint16_t, float > { yof_forceinline float operator ()( const uint16_t & x, const uint16_t & y ) const { return ::sqrt( float(x * x + y * y) ); }; }; template<> struct length< int32_t > : public std::binary_function< int32_t, int32_t, float > { yof_forceinline float operator ()( const int32_t & x, const int32_t & y ) const { return ::sqrt( float(x * x + y * y) ); }; }; template<> struct length< uint32_t > : public std::binary_function< uint32_t, uint32_t, float > { yof_forceinline float operator ()( const uint32_t & x, const uint32_t & y ) const { return ::sqrt( float(x * x + y * y) ); }; }; template<> struct length< int > : public std::binary_function< int, int, float > { yof_forceinline float operator ()( const int & x, const int & y ) const { return ::sqrt( float(x * x + y * y) ); }; }; template<> struct length< uint_t > : public std::binary_function< uint_t, uint_t, float > { yof_forceinline float operator ()( const uint_t & x, const uint_t & y ) const { return ::sqrt( float(x * x + y * y) ); }; }; #endif // _MSC_VER template struct clamp { T m_min, m_max; clamp( const T & min, const T & max ) : m_min( min ), m_max( max ) {} yof_forceinline T operator ()( const T & value ) const { return minimum()( m_max, maximum()( value, m_min ) ); } }; template struct clamp_operator { T m_min, m_max; OPERATION m_op; clamp_operator( OPERATION & op, const T & min, const T & max ) : m_op( op ), m_min( min ), m_max( max ) {} T operator ()( const T & src1, const T & src2 ) const { T value = m_op( src1, src2 ); return minimum()( m_max, maximum()( value, m_min ) ); } }; template struct clamp_value { typedef clamp_value self; typedef yof_typename int_t::signed_t>::inclusion incl_t; incl_t m_value, m_min, m_max; clamp_value( const T & value, const T & min, const T & max ) : m_value( value ), m_min( min ), m_max( max ) {} operator T() const { return m_value; } self operator +( const T & value ) const { incl_t v = minimum()( m_max, m_value+incl_t(value) ); return clamp_value( v, m_min, m_max ); } self operator -( const T & value ) const { incl_t v = maximum()( m_value-incl_t(value), m_min ); return clamp_value( v, m_min, m_max ); } self & operator =( const T & value ) { m_value = minimum()( m_max, maximum()( value, m_min ) ); return *this; } self & operator +=( const T & value ) { m_value = minimum()( m_max, m_value+incl_t(value) ); return *this; } self & operator -=( const T & value ) { m_value = maximum()( m_value-incl_t(value), m_min ); return *this; } }; #if defined(_MSC_VER) && _MSC_VER <= 1200 && !defined( _STLPORT_VERSION ) template< typename CATEGORY, typename VALUE, typename DIFF = ptrdiff_t, typename POINTER = VALUE *, typename REFERENCE = VALUE & > struct iterator { typedef CATEGORY iterator_category; typedef VALUE value_type; typedef DIFF difference_type; typedef POINTER pointer; typedef REFERENCE reference; }; #else using ::std::iterator; #endif #if (defined(_MSC_VER) && _MSC_VER <= 1200) || (defined( _STLP_INTERNAL_ITERATOR_OLD_H ) && defined( _STLP_MSVC50_COMPATIBILITY )) template< class ITERATOR > class reverse_iterator : public ::std::reverse_iterator< ITERATOR, typename ITERATOR::value_type > {}; #else using ::std::reverse_iterator; #endif } // namespace yoffy #include #include #ifdef max # undef max #endif #ifdef min # undef min #endif #ifdef maximum # undef maximum #endif #ifdef minimum # undef minimum #endif namespace std { //! コンパイルターゲットに応じた string または wstring typedef std::basic_string< yoffy::_TCHAR > _tstring; /*! \var _tout \brief コンパイルターゲットに応じた cout または wcout */ /*! \var _terr \brief コンパイルターゲットに応じた cerr または wcerr */ #ifndef YOF_NO_TOUT # ifdef _UNICODE extern wistream & _tin; extern wostream & _tout; extern wostream & _terr; extern wostream & _tlog; # else // _MBCS or CBCS extern istream & _tin; extern ostream & _tout; extern ostream & _terr; extern ostream & _tlog; # endif // #ifdef _UNICODE #endif // #ifndef YOF_NO_TOUT } // namespace std #endif // #ifndef YOF_TYPES_HPP