SUMO - Simulation of Urban MObility
IDSupplier.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // A class that generates enumerated and prefixed string-ids
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <string>
33 #include <sstream>
34 #include "StdDefs.h"
35 #include "IDSupplier.h"
36 
37 
38 // ===========================================================================
39 // method definitions
40 // ===========================================================================
41 IDSupplier::IDSupplier(const std::string& prefix, long long int begin)
42  : myCurrent(begin), myPrefix(prefix) {}
43 
44 
45 
46 IDSupplier::IDSupplier(const std::string& prefix, const std::vector<std::string>& knownIDs)
47  : myCurrent(0), myPrefix(prefix) {
48  for (std::vector<std::string>::const_iterator id_it = knownIDs.begin(); id_it != knownIDs.end(); ++id_it) {
49  avoid(*id_it);
50  }
51 }
52 
53 
55 
56 
57 std::string
59  std::ostringstream strm;
60  strm << myPrefix << myCurrent++;
61  return strm.str();
62 }
63 
64 
65 void
66 IDSupplier::avoid(const std::string& id) {
67  // does it start with prefix?
68  if (id.find(myPrefix) == 0) {
69  long long int number;
70  std::istringstream buf(id.substr(myPrefix.size()));
71  buf >> number;
72  // does it continue with a number?
73  if (!buf.fail()) {
74  myCurrent = MAX2(myCurrent, number + 1);
75  }
76  }
77 }
78 
79 
80 /****************************************************************************/
81 
std::string myPrefix
The prefix to use.
Definition: IDSupplier.h:71
IDSupplier(const std::string &prefix="", long long int begin=0)
Constructor.
Definition: IDSupplier.cpp:41
void avoid(const std::string &id)
make sure that the given id is never supplied
Definition: IDSupplier.cpp:66
T MAX2(T a, T b)
Definition: StdDefs.h:73
std::string getNext()
Returns the next id.
Definition: IDSupplier.cpp:58
~IDSupplier()
Destructor.
Definition: IDSupplier.cpp:54
long long int myCurrent
The current index.
Definition: IDSupplier.h:68