SUMO - Simulation of Urban MObility

IDSupplier.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A class that generates enumerated and prefixed string-ids
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
00023 // ===========================================================================
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029 
00030 #include <string>
00031 #include <sstream>
00032 #include "StdDefs.h"
00033 #include "IDSupplier.h"
00034 
00035 #ifdef CHECK_MEMORY_LEAKS
00036 #include <foreign/nvwa/debug_new.h>
00037 #endif // CHECK_MEMORY_LEAKS
00038 
00039 
00040 // ===========================================================================
00041 // method definitions
00042 // ===========================================================================
00043 IDSupplier::IDSupplier(const std::string& prefix, long begin)
00044     : myCurrent(begin), myPrefix(prefix) {}
00045 
00046 
00047 
00048 IDSupplier::IDSupplier(const std::string& prefix, const std::vector<std::string> &knownIDs)
00049     : myCurrent(0), myPrefix(prefix) {
00050     for (std::vector<std::string>::const_iterator id_it = knownIDs.begin(); id_it != knownIDs.end(); ++id_it) {
00051         avoid(*id_it);
00052     }
00053 }
00054 
00055 
00056 IDSupplier::~IDSupplier() {}
00057 
00058 
00059 std::string
00060 IDSupplier::getNext() {
00061     std::ostringstream strm;
00062     strm << myPrefix << myCurrent++;
00063     return strm.str();
00064 }
00065 
00066 
00067 void
00068 IDSupplier::avoid(const std::string& id) {
00069     // does it start with prefix?
00070     if (id.find(myPrefix) == 0) {
00071         long number;
00072         std::istringstream buf(id.substr(myPrefix.size(), std::string::npos));
00073         buf >> number;
00074         // does it continue with a number?
00075         if (!buf.fail()) {
00076             myCurrent = MAX2(myCurrent, number+1);
00077         }
00078     }
00079 }
00080 
00081 
00082 /****************************************************************************/
00083 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines