SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IDSupplier.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A class that generates enumerated and prefixed string-ids
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
34 #include <sstream>
35 #include "StdDefs.h"
36 #include "IDSupplier.h"
37 
38 #ifdef CHECK_MEMORY_LEAKS
39 #include <foreign/nvwa/debug_new.h>
40 #endif // CHECK_MEMORY_LEAKS
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 IDSupplier::IDSupplier(const std::string& prefix, long begin)
47  : myCurrent(begin), myPrefix(prefix) {}
48 
49 
50 
51 IDSupplier::IDSupplier(const std::string& prefix, const std::vector<std::string>& knownIDs)
52  : myCurrent(0), myPrefix(prefix) {
53  for (std::vector<std::string>::const_iterator id_it = knownIDs.begin(); id_it != knownIDs.end(); ++id_it) {
54  avoid(*id_it);
55  }
56 }
57 
58 
60 
61 
62 std::string
64  std::ostringstream strm;
65  strm << myPrefix << myCurrent++;
66  return strm.str();
67 }
68 
69 
70 void
71 IDSupplier::avoid(const std::string& id) {
72  // does it start with prefix?
73  if (id.find(myPrefix) == 0) {
74  long number;
75  std::istringstream buf(id.substr(myPrefix.size(), std::string::npos));
76  buf >> number;
77  // does it continue with a number?
78  if (!buf.fail()) {
79  myCurrent = MAX2(myCurrent, number + 1);
80  }
81  }
82 }
83 
84 
85 /****************************************************************************/
86