OSDN Git Service

pointer_facade の < を訂正 & clonable_ptr のテストに < 比較を追加
authorSubaruG <subaru_g@users.sourceforge.jp>
Thu, 28 Jan 2010 17:52:30 +0000 (02:52 +0900)
committerSubaruG <subaru_g@users.sourceforge.jp>
Thu, 28 Jan 2010 17:52:30 +0000 (02:52 +0900)
gintenlib/pointer_facade.hpp
html/index.html
tests/clonable_ptr.cc

index d687e9a..4d5af9f 100644 (file)
 #include "bool_comparable.hpp"
 
 #include <cassert>
+#include <functional> // for std::less
 #include <boost/type_traits/add_reference.hpp>
 
 namespace gintenlib
 {
   template< typename Derived, typename T, typename Category = Derived >
-  struct pointer_facade
-    : bool_comparable< pointer_facade< Derived, T, Category > >
+  class pointer_facade
+    : public bool_comparable< pointer_facade< Derived, T, Category > >
   {
+    typedef pointer_facade<Derived, T, Category> this_type;
+    
+   public:
     // スマートポインタとして必要なあれこれ
     typedef T  element_type;
     typedef T  value_type;
@@ -157,12 +161,9 @@ namespace gintenlib
     }
     // operator safe_bool は bool_comparable の管轄
     
-    // 等値比較などは外部のテンプレートで実装
-  
-   private:
-    template<typename D, typename U, typename C>
-    friend class pointer_facade;
+    // 相互比較は外部で定義する
     
+   private:
     // 派生クラスの get() を呼び出す
     pointer get_() const
     {
@@ -175,7 +176,7 @@ namespace gintenlib
   
   };  // pointer_facade<Derived, T, Category>
   
-  // テンプレート版相互比較
+  // 相互比較
   // カテゴリが同じものなら相互比較OK
   template<typename T, typename U, typename D1, typename D2, typename C>
   inline bool operator==( const pointer_facade<D1, T, C>& lhs, const pointer_facade<D2, U, C>& rhs )
@@ -187,11 +188,18 @@ namespace gintenlib
   {
     return static_cast<const D1&>(lhs).get() != static_cast<const D2&>(rhs).get();
   }
+  
+  // < は、ポインタ型が同じ場合とそうでない場合で分ける
+  template<typename T, typename D, typename C>
+  inline bool operator< ( const pointer_facade<D, T, C>& lhs, const pointer_facade<D, T, C>& rhs )
+  {
+    // 同じポインタの比較は std::less で比較する
+    return std::less<T*>()( static_cast<const D&>(lhs).get(), static_cast<const D&>(rhs).get() );
+  }
   template<typename T, typename U, typename D1, typename D2, typename C>
   inline bool operator< ( const pointer_facade<D1, T, C>& lhs, const pointer_facade<D2, U, C>& rhs )
   {
-    // 異なるポインタ同士の比較は、
-    // boost::shared_ptr で < を使ってるので、ここでも < で比較する
+    // 異なるポインタ同士の比較は、仕方ないので < で比較する
     return static_cast<const D1&>(lhs).get() < static_cast<const D2&>(rhs).get();
   }
 
index 33447d0..16a7a22 100644 (file)
@@ -18,7 +18,7 @@
 <div class=verinfo>Version 1.0.0   <span class=weak>( under construction )</span></div>
 
 <div class=menu>
-<a href="getting_started.html">はじめに</a>  
+<a href="getting_started.html">導入方法</a>  
 <a href="libs/index.html">ライブラリ一覧</a>  
 <a href="../LICENSE">ライセンス</a>  
 <a href="faq.html">FAQ</a>  
index cb91e33..7dc386c 100644 (file)
@@ -112,6 +112,9 @@ void test1( const gintenlib::clonable_ptr<T>& p0 )
       BOOST_CHECK( p1.unique() );
       // clone したため、別のオブジェクトが格納されてるはず
       BOOST_CHECK( p0 != p1 );
+      // 不等号比較関連をついでに
+      BOOST_CHECK( p0 < p1 || p1 < p0 );
+      BOOST_CHECK( !( p0 < p1 && p1 < p0 ) );
     }
     else
     {
@@ -188,6 +191,8 @@ void test1( const gintenlib::clonable_ptr<T>& p0 )
     // clone() 後なので当然 unique
     BOOST_CHECK( p2 && p2.unique() );
     BOOST_CHECK( p1 != p2 );
+    BOOST_CHECK( p1 < p2 || p2 < p1 );
+    BOOST_CHECK( !( p1 < p2 && p2 < p1 ) );
   }
   
   // おわり