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