to_shared : 任意のポインタを shared_ptr に変換
宣言:
- // 任意関数
+ // 任意ポインタ
template<typename P>
shared_ptr<typename P::element_type> to_shared( const P& pt );
機能:
各種スマートポインタを shared_ptr に変換します。
- 汎用版は大部分のスマートポインタを変換してくれるはずですが、今のところ動作保障はできません。
- 動作保障の出来ない汎用版をわざわざ残している理由は、名前空間の自動照会を行えるようにです。
- Boost.Swap ( http://www.boost.org/doc/libs/1_41_0/libs/utility/swap.html ) と同じように、
- gintenlib::to_shared( pt ) と記述された場合、 gintenlib 名前空間だけでなく、
- pt のクラスが定義されている名前空間からも to_shared 関数を探してくれます。
+ 名前空間の自動紹介は行わない事になりました。代わりに using gintenlib::to_shared としてください。
*/
#include <cassert>
#include <memory>
-// friend 関数を使いやすいように
-// 本体は別の名前空間を使う
-namespace gintenlib_to_shared_impl_
+namespace gintenlib
{
- using gintenlib::shared_ptr;
+ // std::auto_ptr version
+ // 実際のところ必要ないけど
+ template<typename T>
+ inline shared_ptr<T> to_shared( std::auto_ptr<T> pt )
+ {
+ return shared_ptr<T>( pt.release() );
+ }
+
+ // trivial version
+ template<typename T>
+ inline shared_ptr<T> to_shared( const shared_ptr<T>& pt )
+ {
+ return pt;
+ }
+
+ // 汎用バージョン
// いわゆる「スマートポインタ」を保持するホルダ
// shared_ptr のコンストラクタの第二引数として渡す削除子として使える
}
private:
- // メンバとして Pointer を保持するので
- // これが存在する限り、
+ // メンバとして Pointer を保持する
Pointer pt;
}; // to_shared_holder<Pointer>
- // 「任意ポインタの変換」本体
+ // 汎用版の本体は単なるラッパ
template<typename P>
inline shared_ptr<typename P::element_type> to_shared( const P& pt )
{
return to_shared_holder<P>::to_shared( pt );
}
-} // namespace gintenlib_to_shared_impl_
-
-
-// 転送
-namespace gintenlib_to_shared_impl
-{
- // これも薄いラッパ
- template<typename P>
- inline gintenlib::shared_ptr<typename P::element_type> to_shared_impl( const P& pt )
- {
- using namespace gintenlib_to_shared_impl_;
- return to_shared( pt );
- }
-
-} // namespace gintenlib_to_shared_impl_
-
-
-namespace gintenlib
-{
- // 本体は単なるラッパ
- template<typename P>
- inline shared_ptr<typename P::element_type> to_shared( const P& pt )
- {
- return ::gintenlib_to_shared_impl::to_shared_impl( pt );
- }
-
- // std::auto_ptr version
- // 実際のところ必要ないけど
- template<typename T>
- inline shared_ptr<T> to_shared( std::auto_ptr<T> pt )
- {
- return shared_ptr<T>( pt.release() );
- }
-
- // trivial version
- template<typename T>
- inline shared_ptr<T> to_shared( const shared_ptr<T>& pt )
- {
- return pt;
- }
-
} // namespace gintenlib
#endif // #ifndef GINTENLIB_INCLUDED_TO_SHARED_HPP_