メインページ | ネームスペース一覧 | クラス階層 | 構成 | Directories | ファイル一覧 | ネームスペースメンバ | 構成メンバ | ファイルメンバ | 関連ページ

BaseUtility.h

説明を見る。
00001 #ifndef Spr_BASE_UTILITIES_H
00002 #define Spr_BASE_UTILITIES_H
00003 #include "BaseDebug.h"
00004 #include <algorithm>
00005 #include <iosfwd>
00006 #include <vector>
00007 #include <typeinfo>
00008 
00009 /** @file   BaseUtility.h   その他のユーティリティークラス・関数.*/
00010 
00011 namespace Spr {
00012 
00013 typedef std::string UTString;
00014 inline bool operator < (const UTString& u1, const UTString& u2){
00015     return u1.compare(u2) < 0;
00016 }
00017 struct UTStringLess{
00018     bool operator ()(const UTString& t1, const UTString& t2) const{
00019         return t1.compare(t2) < 0;
00020     }
00021 };
00022 
00023 template <class T>
00024 bool UTContentsEqual(const T& t1, const T& t2){
00025     return *t1 == *t2;
00026 }
00027 
00028 template <class T>
00029 struct UTContentsLess{
00030     bool operator ()(const T& t1, const T& t2) const{
00031         return *t1 < *t2;
00032     }
00033 };
00034 
00035 template <class T, class M, M T::* m>
00036 struct UTMemberLess{
00037     bool operator ()(const T& t1, const T& t2) const{
00038         return *t1.*m < *t2.*m;
00039     }
00040 };
00041 
00042 class SPR_DLL UTPadding{
00043 public:
00044     int len;
00045     UTPadding(int i){len = i;}
00046 };
00047 std::ostream& operator << (std::ostream& os, UTPadding p);
00048 
00049 struct UTEatWhite{
00050     UTEatWhite(){}
00051 };
00052 std::istream& operator >> (std::istream& is, const UTEatWhite& e);
00053 
00054 /// 参照カウンタ.カウントが0になっても勝手に消えはしない.
00055 class SPR_DLL UTRefCount{
00056     mutable int refCount;
00057 public:
00058 #ifndef _DEBUG
00059 //  UTRefCount(){refCount = 0;}
00060 //  UTRefCount(const UTRefCount&){refCount = 0;}
00061 //  ~UTRefCount(){ assert(refCount==0); }
00062     UTRefCount();
00063     UTRefCount(const UTRefCount& r);
00064     ~UTRefCount();
00065 #else
00066     static int nObject;
00067     UTRefCount();
00068     UTRefCount(const UTRefCount& r);
00069     ~UTRefCount();
00070 #endif
00071     UTRefCount& operator = (const UTRefCount& r){ return *this; }
00072 
00073     int AddRef() const { return ++ refCount; }
00074     int DelRef() const {
00075         assert(refCount > 0);
00076         return -- refCount;
00077     }
00078     int RefCount() const { return refCount; }
00079 };
00080 
00081 /** 参照カウンタ用のポインタ.自動的に参照カウンタを増減,
00082     オブジェクトのdeleteをする.
00083 */
00084 template <class T>
00085 class UTRef{
00086     T* obj;
00087     T*& Obj() const {return (T*&) obj;}
00088 public:
00089     UTRef(T* t = NULL){
00090         Obj() = t;
00091         if (Obj()) Obj()->AddRef();
00092     }
00093     template <class E>
00094     UTRef(const UTRef<E>& r){
00095         Obj() = (T*)(E*)r;
00096         if (Obj()) Obj()->AddRef();
00097     }
00098     UTRef(const UTRef<T>& r){
00099         Obj() = r.Obj();
00100         if (Obj()) Obj()->AddRef();
00101     }
00102     ~UTRef(){ if (Obj() && Obj()->DelRef() == 0) delete Obj(); }
00103     UTRef& operator =(T* t){
00104         if (Obj() != t){
00105             if (Obj() && Obj()->DelRef() == 0) delete Obj();
00106             Obj() = t;
00107             if (Obj()) Obj()->AddRef();
00108         }
00109         return *this;
00110     }
00111     template <class E>
00112     UTRef& operator =(const UTRef<E>& r){
00113         if (Obj() != r){
00114             if (Obj() && Obj()->DelRef() == 0) delete Obj();
00115             Obj() = r;
00116             if (Obj()) Obj()->AddRef();
00117         }
00118         return *this;
00119     }
00120     UTRef& operator =(const UTRef<T>& r){
00121         if (Obj() != r.Obj()){
00122             if (Obj() && Obj()->DelRef() == 0) delete Obj();
00123             Obj() = r;
00124             if (Obj()) Obj()->AddRef();
00125         }
00126         return *this;
00127     }
00128     operator T*() const {return Obj();}
00129     T* operator->() const {return Obj();}
00130     bool operator <(const UTRef& r) const { return Obj() < r.Obj(); }
00131 };
00132 
00133 /// UTRefの配列
00134 template<class T, class ARRAY = std::vector< UTRef<T> > >
00135 class UTRefArray : public ARRAY{
00136 public:
00137     UTRef<T> Erase(const UTRef<T>& ref){
00138         iterator it = std::find(begin(), end(), ref);
00139         if (it == end()) return NULL;
00140         UTRef<T> rv = *it;
00141         erase(it);
00142         return rv;
00143     }
00144     UTRef<T>* Find(const UTRef<T>& ref){
00145         iterator it = std::find(begin(), end(), s);
00146         if (it == end()) return NULL;
00147         else return &*it;
00148     }
00149     UTRef<T>* Find(const UTRef<T>& ref) const {
00150         return ((UTRefArray<T, ARRAY>*)this)->Find(ref);
00151     }
00152 };
00153 
00154 
00155 /// シングルトンクラス
00156 template <class T>
00157 T& Singleton(){
00158     static T t;
00159     return t;
00160 }
00161 
00162 /// スタックつき vector 
00163 template <class T, class Cont=std::vector<T> >
00164 class UTStack: public Cont{
00165 public:
00166     T Pop(){
00167         assert(size());
00168         T t=back(); pop_back(); return t;
00169     }
00170     void Push(const T& t=T()){ push_back(t); }
00171     T& Top(){
00172         assert(size());
00173         return back();
00174     }
00175 };
00176 
00177 /// ツリーのノード(親子両方向参照)
00178 template <class T, class ARRAY=std DOUBLECOLON vector< UTRef<T> > >
00179 class UTTreeNode{
00180 private:
00181     void clear();
00182 protected:
00183     T* parent;
00184     ARRAY children;
00185 public:
00186     typedef ARRAY container_t;
00187     ///
00188     UTTreeNode():parent(NULL){}
00189     virtual ~UTTreeNode(){
00190 /*      for(ARRAY::iterator it = children.begin(); it != children.end(); ++it){
00191             (*it)->parent = NULL;
00192         }
00193         children.clear();
00194 */
00195         while(children.size()){
00196              children.back()->parent = NULL;
00197              children.pop_back();
00198         }
00199     }
00200     ///@name ツリーの操作
00201     //@{
00202     /// 親ノードを取得する.
00203     T* GetParent(){ return parent; }
00204     const T* GetParent() const { return parent; }
00205     /// 親ノードを設定する.
00206     void SetParent(T* n){
00207         if (parent == n) return;                        //  変更がなければ何もしない.
00208         //  途中でRefCountが0になって消えないように,先に新しいノードの子にする.
00209         if (n) n->children.push_back((T*)this);
00210         if (parent) {                                   //  古い親ノードの子リストから削除
00211             TYPENAME ARRAY::iterator it = std::find(parent->children.begin(), parent->children.end(), UTRef<T>((T*)this));
00212             if (it != parent->children.end()) parent->children.erase(it);
00213         }
00214         parent = n;                                     //  parent を新しいノードに切り替える.
00215     }
00216     /// 子ノード.
00217     ARRAY& Children(){ return children; }
00218     /// 子ノード.
00219     const ARRAY& Children() const { return children; }
00220     /// 子ノードを追加する
00221     void AddChild(UTRef<T> c){ c->SetParent((T*)this); }
00222     /// 子ノードを削除する
00223     void DelChild(UTRef<T> c){ if(c->GetParent() == this) c->SetParent(NULL); }
00224     /// 子ノードをすべて削除する.
00225     void ClearChildren(){
00226         while(children.size()){
00227              children.back()->parent = NULL;
00228              children.pop_back();
00229         }
00230     }
00231     ///
00232     template <class M>
00233     void ForEachChild(M m){
00234         for(TYPENAME ARRAY::iterator it = children.begin(); it !=children.end(); ++it){
00235             T* t = *it;
00236             (t->*m)();
00237         }
00238     }
00239     ///
00240     template <class M>
00241     void Traverse(M m){
00242           m(this);
00243         for(TYPENAME ARRAY::iterator it = children.begin(); it !=children.end(); ++it){
00244             (*it)->Traverse(m);
00245         }
00246     }
00247     template <class M, class A>
00248     void Traverse(M m, A a){
00249         m((T*)this, a);
00250         for(TYPENAME ARRAY::iterator it = children.begin(); it !=children.end(); ++it){
00251             (*it)->Traverse(m, a);
00252         }
00253     }
00254     template <class T2, class M>
00255     void MemberTraverse(T2 t, M m){
00256         (t->*m)(this);
00257         for(TYPENAME ARRAY::iterator it = children.begin(); it !=children.end(); ++it){
00258             (*it)->MemberTraverse(t, m);
00259         }
00260     }
00261     template <class E, class M, class A>
00262     void MemberTraverse(E e, M m, A& a){
00263         (e->*m)((T*)this, a);
00264         for(TYPENAME ARRAY::iterator it = children.begin(); it !=children.end(); ++it){
00265             (*it)->MemberTraverse(e, m, a);
00266         }
00267     }
00268 };
00269 
00270     
00271 /* assert_cast
00272          SPR_DEBUG定義時はdynamic_cast、それ以外ではstatic_castとして働く。
00273          dynamic_castに失敗するとstd::bad_cast例外を発生する。
00274 */
00275 template <class T, class U>
00276 inline T assert_cast(U u){
00277 #ifdef SPR_DEBUG
00278     T t= dynamic_cast<T>(u);
00279     if (u && !t) throw std::bad_cast();
00280     return t;
00281 #else
00282     return static_cast<T>(u);
00283 #endif
00284 }
00285 
00286 
00287 }   //  namespace Spr
00288 
00289 #endif

Springheadに対してSun Apr 16 01:57:49 2006に生成されました。  doxygen 1.4.1