Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

NamedObjectDatabase.h

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Lamp : Open source game middleware
00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //------------------------------------------------------------------------------
00019 
00020 /** @file
00021  * 名前付きオブジェクトデータベースヘッダ
00022  * @author Junpee
00023  */
00024 
00025 #ifndef NAMED_OBJECT_DATABASE_H_
00026 #define NAMED_OBJECT_DATABASE_H_
00027 
00028 #include <Core/Container/HashMap.h>
00029 #include <Core/Container/Deque.h>
00030 
00031 namespace Lamp{
00032 
00033 //------------------------------------------------------------------------------
00034 /**
00035  * 名前付きオブジェクトデータベース
00036  */
00037 template <typename Type>
00038 class NamedObjectDatabase{
00039 public:
00040     /**
00041      * コンストラクタ
00042      * @param capacity 初期容量
00043      * @param loadFactor 負荷係数
00044      */
00045     NamedObjectDatabase(int capacity = 256, float loadFactor = 0.75f) :
00046         hash_(capacity, loadFactor), array_(capacity){
00047     }
00048 
00049     /**
00050      * デストラクタ
00051      */
00052     virtual ~NamedObjectDatabase(){
00053         Assert(hash_.getCount() == 0);
00054         Assert(array_.getCount() == 0);
00055     }
00056 
00057     //--------------------------------------------------------------------------
00058     // オブジェクトの追加と削除
00059     //--------------------------------------------------------------------------
00060     /**
00061      * オブジェクトの追加
00062      * @param object 追加するオブジェクト
00063      * @return 成功すればtrue
00064      */
00065     virtual bool add(const String& name, Type* object){
00066         Assert(name.getSize() != 0);
00067         Assert(object != NULL);
00068         if(search(name) != NULL){ return false; }
00069         hash_.put(name, object);
00070         array_.pushBack(object);
00071         return true;
00072     }
00073 
00074     /**
00075      * オブジェクトの削除
00076      * @param object 削除するオブジェクト
00077      * @return 成功すればtrue
00078      */
00079     virtual bool remove(const String& name, Type* object){
00080         Assert(name.getSize() != 0);
00081         Assert(object != NULL);
00082         Type* removeObject = search(name);
00083         if(removeObject == NULL){ return false; }
00084         if(removeObject != object){ return false; }
00085         if(array_.removeByValue(object) == -1){ return false; }
00086         if(hash_.remove(name) == NULL){ Assert(false); }
00087         return true;
00088     }
00089 
00090     /**
00091      * クリア
00092      * @return 削除したオブジェクト数
00093      */
00094     virtual int clear(){
00095         int result = getCount();
00096         array_.clear();
00097         hash_.clear();
00098         return result;
00099     }
00100 
00101     //--------------------------------------------------------------------------
00102     // オブジェクトへのアクセス
00103     //--------------------------------------------------------------------------
00104     /**
00105      * オブジェクト数の取得
00106      * @return オブジェクト数
00107      */
00108     virtual int getCount(){ return array_.getCount(); }
00109 
00110     /**
00111      * オブジェクトの取得
00112      * @param index オブジェクトのインデクス
00113      * @return オブジェクト
00114      */
00115     virtual Type* get(int index){ return array_.get(index); }
00116 
00117     /**
00118      * オブジェクトの検索
00119      * @param name 検索するオブジェクト名
00120      * @return オブジェクト
00121      */
00122     virtual Type* search(const String& name){ return hash_.get(name); }
00123 
00124 private:
00125     //--------------------------------------------------------------------------
00126     // コピーコンストラクタの隠蔽
00127     NamedObjectDatabase(const NamedObjectDatabase& copy);
00128 
00129     // 代入コピーの隠蔽
00130     void operator =(const NamedObjectDatabase& copy);
00131 
00132     // ハッシュ
00133     HashMap<String, Type*> hash_;
00134     // 配列
00135     Deque<Type*> array_;
00136 
00137 };
00138 
00139 //------------------------------------------------------------------------------
00140 } // End of namespace Lamp
00141 #endif // End of NAMED_OBJECT_DATABASE_H_
00142 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:33 2005 for Lamp by doxygen 1.3.2