aesop-map/map.cpp

Go to the documentation of this file.
00001 /*
00002  * map.cpp
00003  *
00004  * Copyright (C) 2008  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * Basic map support.
00032  */
00033 
00034 // includes --------------------------------------------------------------------
00035 #include "map.h"                        // always include our own header first!
00036 
00037 #include "common/wave_ex.h"
00038 #include "perf/perf.h"
00039 
00040 
00041 namespace aesop {
00042 
00043 
00044 // interface destructor implementation
00045 MapFormatReader::~MapFormatReader(void) throw() { }
00046 
00047 typedef std::vector<smart_ptr<MapFormatReader> > vec_reader_t;
00048 
00049 static vec_reader_t s_readers;  // static (process-wide) list of readers
00050 
00051 
00053 //
00054 //      static helper methods
00055 //
00057 
00058 
00059 
00061 //
00062 //      public API
00063 //
00065 
00066 void
00067 registerMapFormatReader
00068 (
00069 IN smart_ptr<MapFormatReader>& reader
00070 )
00071 {
00072         ASSERT(reader, "registering a null reader?");
00073 
00074         s_readers.push_back(reader);
00075 }
00076 
00077 
00078 
00079 smart_ptr<Map>
00080 loadMap
00081 (
00082 IN const char * id,
00083 IN const char * path
00084 )
00085 {
00086         perf::Timer timer("loadMap");
00087         ASSERT(id, "null");
00088         ASSERT(path, "null");
00089 
00090         smart_ptr<Map> map;
00091         for (vec_reader_t::iterator i = s_readers.begin(); i != s_readers.end();
00092              ++i) {
00093                 MapFormatReader * reader = *i;
00094                 ASSERT(reader, "null map reader in vector?");
00095 
00096                 bool isFormat = false;
00097                 {
00098                         perf::Timer timer("loadMap::isMyFormat");
00099                         isFormat = reader->isMyFormat(id, path);
00100                 }
00101 
00102                 if (!isFormat)
00103                         continue;       // skip this reader
00104 
00105                 // for now we dump information about the path and the
00106                 //   general reader description
00107                 DPRINTF("Path '%s' is claimed by reader.", path);
00108                 const char * desc = reader->getDescription();
00109                 ASSERT(desc, "reader returned a null description?");
00110                 DPRINTF("  Reader: '%s'", desc);
00111 
00112                 std::string timer_name = "loadMap::";
00113                 timer_name += id;
00114                 try {
00115                         perf::Timer timer(timer_name.c_str());
00116                         map = reader->loadMap(id, path);
00117                 } catch (std::exception& e) {
00118                         WAVE_EX(wex);
00119                         wex << "Failed to load map " << id << " from " << path;
00120                         wex << "\n" << e.what();
00121                 }
00122                 break;
00123         }
00124 
00125         if (!map) {
00126                 WAVE_EX(wex);
00127                 wex << "Failed to load map at '" << path << "'.  No reader found!";
00128                 return NULL;
00129         }
00130 
00131         // all done!
00132         return map;
00133 }
00134 
00135 
00136 
00137 };      // aesop namespace
00138