1 #ifndef GINTENLIB_INCLUDED_INSTANCE_COUNTER_HPP_
2 #define GINTENLIB_INCLUDED_INSTANCE_COUNTER_HPP_
6 <gintenlib/instance_counter.hpp>
8 instance_counter : オブジェクトの存在個数を数える
11 // 存在する Derived の数を数える
12 template<typename Derived>
13 struct instance_counter
15 static int use_count();
19 instance_counter( const instance_counter& );
20 ~instance_counter() throw ();
22 }; // instance_counter<Derived>
24 template<typename Derived>
25 int count_instances();
28 struct hoge : gintenlib::instance_counter<hoge>
32 のように使うことで、 hoge の有効なインスタンス数を数え上げます。
36 #include "enable_if.hpp"
38 #include <boost/mpl/assert.hpp>
39 #include <boost/type_traits/is_base_of.hpp>
43 namespace instance_counter_ // ADL回避用
45 // 存在する Derived の数を数える
46 template<typename Derived>
47 struct instance_counter
49 static int use_count()
52 BOOST_MPL_ASSERT(( boost::is_base_of<instance_counter, Derived> ));
62 instance_counter( const instance_counter& )
66 ~instance_counter() throw ()
74 }; // instance_counter<Derived>
76 // 実体の確保(テンプレートなのでヘッダファイルに置ける)
77 template<typename Derived>
78 int instance_counter<Derived>::count_ = 0;
81 template<typename Derived>
82 inline typename enable_if< boost::is_base_of< instance_counter<Derived>, Derived >, int >::type
85 return instance_counter<Derived>::use_count();
88 } // namespace instance_counter_
90 // gintenlib 名前空間に引き入れる
91 using namespace instance_counter_;
93 } // namespace gintenlib
95 #endif // #ifndef GINTENLIB_INCLUDED_INSTANCE_COUNTER_HPP_