ObjectRepository.cpp

Go to the documentation of this file.
00001 /*
00002 SagaEngine library
00003 Copyright (c) 2002-2006 Skalden Studio AS
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to distribute the library under the terms of the 
00010 Q Public License version 1.0. Be sure to read and understand the license
00011 before using the library. It should be included here, or you may read it
00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl
00013 
00014 The original version of this library can be located at:
00015 http://www.sagaengine.com/
00016 
00017 Rune Myrland
00018 rune@skalden.com
00019 */
00020 
00021 
00022 #include "ObjectRepository.hpp"
00023 #include "comp/schema/CompSchema.hpp"
00024 #include "comp/Object.hpp"
00025 #include "util/error/Log.hpp"
00026 
00027 
00028 
00029 namespace se_core {
00030     ObjectRepository
00031     ::ObjectRepository(unsigned int size)
00032             : size_(size), objectCount_(0), objects_(new const Object*[ size ]) {
00033         LogDetail("Creating ObjectRepository");
00034     }
00035 
00036 
00037     ObjectRepository
00038     ::~ObjectRepository() {
00039     }
00040 
00041 
00042     void ObjectRepository
00043     ::add(const Object* go) {
00044         Assert(objectCount_ < size_ - 1);
00045         Assert(go != 0);
00046 
00047         unsigned int index = find(go->id());
00048         // Don't add to the list if it is there already...
00049         if(index == objectCount_ || objects_[index]->id() != go->id()) {
00050             for(unsigned int i = objectCount_; i > index; --i) {
00051                 objects_[ i ] = objects_[ i - 1];
00052             }
00053             objects_[ index ] = go;
00054             ++objectCount_;
00055         }
00056         else {
00057             LogFatal("Hash collision between " << *go << ") and " 
00058                     << *objects_[index] << ". Change the name of one of them.");
00059         }
00060         LogDetail("Registered Object: " << *go);
00061     }
00062 
00063 
00064     void ObjectRepository
00065     ::remove(const Object* go) {
00066         unsigned int index = find(go->id());
00067         if(index == objectCount_ || objects_[ index ]->id() != go->id()) {
00068             return;
00069         }
00070         --objectCount_;
00071         for(unsigned int i = index; i < objectCount_; ++i) {
00072             objects_[ i ] = objects_[ i + 1];
00073         }
00074     }
00075 
00076 
00077     unsigned int ObjectRepository
00078     ::find(int id) {
00079         unsigned int min = 0;
00080         unsigned int max = objectCount_;
00081         unsigned int middle;
00082         while(max > min) {
00083             middle = (max + min) >> 1;
00084             if(objects_[ middle ]->id() < id) {
00085                 min = middle + 1;
00086             }
00087             else {
00088                 max = middle;
00089             }
00090         }
00091         return min;
00092     }
00093 
00094 
00095     const Object* ObjectRepository
00096     ::get(int id) {
00097         unsigned int index = find(id);
00098         Assert(index <= objectCount_);
00099         Assert(objects_[ index ]->id() == id);
00100 
00101         return objects_[ index ];
00102     }
00103 
00104 
00105     bool ObjectRepository
00106     ::has(int id) {
00107         unsigned int index = find(id);
00108         if(index == objectCount_ || objects_[ index ]->id() != id) {
00109             return false;
00110         }
00111         return true;
00112     }
00113 }

Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:10 2007 by Doxygen version 1.3.9.1.

SourceForge.net Logo