From: SubaruG Date: Fri, 22 Jan 2010 16:57:33 +0000 (+0900) Subject: call_traits 関連で微更新 + cast のテストコードを追加 X-Git-Url: http://git.osdn.net/view?p=gintenlib%2Fgintenlib.git;a=commitdiff_plain;h=a761f26a3571c302d1203da6c09c12bb995fe038 call_traits 関連で微更新 + cast のテストコードを追加 --- diff --git a/gintenlib/cast.hpp b/gintenlib/cast.hpp index 80860ac..fba5e46 100644 --- a/gintenlib/cast.hpp +++ b/gintenlib/cast.hpp @@ -10,7 +10,7 @@ 宣言&定義: // 引数をそのまま返す template - inline T cast( typename boost::call_traits::param_type x ) { return x; } + inline T cast( typename call_traits::param_type x ) { return x; } 機能: 暗黙変換を明示的に行うためのキャストです。 cast( 0 ) のように使います。 @@ -55,13 +55,13 @@ */ -#include +#include "call_traits.hpp" namespace gintenlib { // 暗黙キャスト関数 template - inline T cast( typename boost::call_traits::param_type x ) + inline T cast( typename call_traits::param_type x ) { return x; } diff --git a/gintenlib/list_format.hpp b/gintenlib/list_format.hpp index e9d56bd..e7bc347 100644 --- a/gintenlib/list_format.hpp +++ b/gintenlib/list_format.hpp @@ -41,8 +41,10 @@ */ +// タイプ数削減のための using 宣言 +#include "call_traits.hpp" + #include -#include namespace gintenlib { @@ -51,20 +53,20 @@ namespace gintenlib typename Delim = const char*, typename Post = const char* > struct list_format_impl_ { - typedef typename boost::call_traits::param_type inite_param_t; - typedef typename boost::call_traits
::param_type pre_param_t;
-    typedef typename boost::call_traits::param_type delim_param_t;
-    typedef typename boost::call_traits::param_type post_param_t;
+    typedef typename call_traits::param_type inite_param_t;
+    typedef typename call_traits
::param_type pre_param_t;
+    typedef typename call_traits::param_type delim_param_t;
+    typedef typename call_traits::param_type post_param_t;
     
     // constructor
     list_format_impl_( inite_param_t begin, inite_param_t end,
       pre_param_t pre, delim_param_t delim, post_param_t post )
      : begin_(begin), end_(end), pre_(pre), delim_(delim), post_(post) {}
 
-    typename boost::call_traits::value_type begin_, end_;
-    typename boost::call_traits
::value_type pre_;
-    typename boost::call_traits::value_type delim_;
-    typename boost::call_traits::value_type post_;
+    typename call_traits::value_type begin_, end_;
+    typename call_traits
::value_type pre_;
+    typename call_traits::value_type delim_;
+    typename call_traits::value_type post_;
 
     // 出力の実装
     // テンプレートにより std::ostream に限らず出力できます
diff --git a/gintenlib/list_formatter.hpp b/gintenlib/list_formatter.hpp
index b09dbbb..b22f968 100644
--- a/gintenlib/list_formatter.hpp
+++ b/gintenlib/list_formatter.hpp
@@ -106,24 +106,27 @@
 
 */
 
+// operator() で使う
+#include "list_format.hpp"
+// タイプ数削減のための using 宣言
+#include "call_traits.hpp"
+
 #include 
 #include 
 #include   // for swap()
 
 #include 
-#include 
 
-#include "list_format.hpp"
 
 namespace gintenlib
 {
   template< typename Elem >
   class basic_list_formatter
   {
-    typedef typename boost::call_traits::param_type param_type;
+    typedef typename call_traits::param_type param_type;
 
    public:
-    typedef typename boost::call_traits::value_type element_type;
+    typedef typename call_traits::value_type element_type;
 
     basic_list_formatter()
       : pre("( "), delim(", "), post(" )"), iterator_count_(0) {}
diff --git a/tests/cast.cc b/tests/cast.cc
new file mode 100644
index 0000000..ed03638
--- /dev/null
+++ b/tests/cast.cc
@@ -0,0 +1,67 @@
+#include "../gintenlib/cast.hpp"
+
+// boost の単体テストフレームワーク
+#include 
+
+using gintenlib::cast;
+
+void test1()
+{
+  // オーバーロードのチェック
+  struct
+  {
+    int operator()(int)    { return 1; }
+    int operator()(double) { return 2; }
+    int operator()(void*)  { return 3; }
+  } f;
+  
+  BOOST_CHECK( f(0) == 1 );
+  BOOST_CHECK( f( cast(0) ) == 2 );
+  BOOST_CHECK( f( cast(0) ) == 3 );
+}
+
+// ユーザ定義型
+struct base {};
+struct derived : base {};
+
+void test2()
+{
+  struct
+  {
+    int operator()( const base&  )   { return 1; }
+    int operator()( const derived& ) { return 2; }
+    
+  } f;
+  
+  base b; derived d;
+  
+  // 特に意味のないチェック
+  BOOST_CHECK( f( b ) == 1 );
+  BOOST_CHECK( f( d ) == 2 );
+  
+  // derived -> base へのキャスト
+  // 値
+  BOOST_CHECK( f(  cast( d) ) == 1 );
+  // ポインタ
+  BOOST_CHECK( f( *cast(&d) ) == 1 );
+  // 参照
+  BOOST_CHECK( f(  cast( d) ) == 1 );
+  
+  // base -> derived へのキャスト(ダメ)
+  // 値
+  // BOOST_CHECK( f(  cast( b) ) == 2 );
+  // ポインタ
+  // BOOST_CHECK( f( *cast(&b) ) == 2 );
+  // 参照
+  // BOOST_CHECK( f(  cast( b) ) == 2 );
+}
+
+
+int test_main( int argc, char* argv[] )
+{
+  // コンパイルが通った時点で殆どテスト成功
+  test1();
+  test2();
+  
+  return 0;
+}