OSDN Git Service

call_traits の仕様を変更、旧 call_traits と同機能のメタ関数 identity を追加
authorSubaruG <subaru_g@users.sourceforge.jp>
Fri, 22 Jan 2010 10:55:13 +0000 (19:55 +0900)
committerSubaruG <subaru_g@users.sourceforge.jp>
Fri, 22 Jan 2010 10:55:13 +0000 (19:55 +0900)
gintenlib/call_traits.hpp
gintenlib/identity.hpp [new file with mode: 0644]
tests/identity.cc [new file with mode: 0644]

index e6b9113..292f95a 100644 (file)
@@ -5,20 +5,13 @@
 
       <gintenlib/call_traits.hpp>
 
-  call_traits : boost::call_traits +α
+  call_traits : boost::call_traits
 
   宣言:
-    template<typename T>
-    struct call_traits
-      : boost::call_traits<T>
-    {
-      typedef T type;
-    };
+    using boost::call_traits;
 
   機能:
-    boost::call_traits に「与えられた型をそのまま返す」メタ関数の機能を加えたものです。
-    reference や param_type の取得もしたいし、メタ関数としても使いたい、なんて局面で使います。
-    単純に boost::call_traits は gintenlib 内での使用が多いので、短縮として使う意味もあります。
+    boost::call_traits への using 宣言です。
 
 */
 
 
 namespace gintenlib
 {
-  // typename call_traits<T>::type は T に等しい
-  template<typename T>
-  struct call_traits
-    : boost::call_traits<T>
-  {
-    typedef T type;
-  };
+  using boost::call_traits;
 
 }   // namespace gintenlib
 
diff --git a/gintenlib/identity.hpp b/gintenlib/identity.hpp
new file mode 100644 (file)
index 0000000..b3a8235
--- /dev/null
@@ -0,0 +1,40 @@
+#ifndef GINTENLIB_INCLUDED_IDENTITY_HPP_
+#define GINTENLIB_INCLUDED_IDENTITY_HPP_
+
+/*
+
+      <gintenlib/identity.hpp>
+
+  identity : boost::call_traits + boost::mpl::identity
+
+  宣言:
+    template<typename T>
+    struct identity
+      : boost::call_traits<T>
+    {
+      typedef T type;
+    };
+
+  機能:
+    boost::call_traits に「与えられた型をそのまま返す」メタ関数の機能を加えたものです。
+    reference や param_type の取得もしたいし、メタ関数としても使いたい、なんて局面で使います。
+    実装補助用です。
+
+*/
+
+#include "call_traits.hpp"
+
+namespace gintenlib
+{
+  // gintenlib::identity = boost::call_traits + boost::mpl::identity
+  template<typename T>
+  struct identity
+    : call_traits<T>
+  {
+    typedef T type;
+  };
+
+}   // namespace gintenlib
+
+
+#endif  // #ifndef GINTENLIB_INCLUDED_IDENTITY_HPP_
diff --git a/tests/identity.cc b/tests/identity.cc
new file mode 100644 (file)
index 0000000..2784aed
--- /dev/null
@@ -0,0 +1,55 @@
+#include "../gintenlib/identity.hpp"
+
+// boost の単体テストフレームワーク
+#include <boost/test/minimal.hpp>
+
+// boost の該当ヘッダをインクルード
+#include <boost/mpl/identity.hpp>
+#include <boost/call_traits.hpp>
+
+#include <boost/mpl/assert.hpp>
+#include <boost/type_traits/is_same.hpp>
+
+// 二つの型が「等しい」ことをコンパイル時&一応実行時にチェックする
+template<typename T1, typename T2>
+bool is_equal()
+{
+  BOOST_MPL_ASSERT(( boost::is_same<T1, T2> ));
+  return boost::is_same<T1, T2>::value;
+}
+
+// 型 T に対して動作をチェックする
+template<typename T>
+void test()
+{
+  // gintenlib::identity<T>::type は T 自身に等しい
+  BOOST_CHECK(( is_equal<T, typename gintenlib::identity<T>::type>() ));
+  
+  // value_type, param_type, reference, const_reference は、boost::call_traits と同じ
+  #define GEN_TEST_( xxx )  \
+    BOOST_CHECK(( is_equal<typename boost::call_traits<T>::xxx, \
+      typename gintenlib::identity<T>::xxx >() ));
+  
+    GEN_TEST_( value_type );
+    GEN_TEST_( param_type );
+    GEN_TEST_( reference );
+    GEN_TEST_( const_reference );
+    
+  #undef GEN_TEST_
+}
+
+#include <iostream>
+
+int test_main( int argc, char* argv[] )
+{
+  // 組み込み型
+  test<int>();
+  // オブジェクト
+  test<std::ostream>();
+  // ポインタ
+  test<int*>();
+  // 参照
+  test<std::ostream&>();
+    
+  return 0;
+}