SUMO - Simulation of Urban MObility
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.dlr.de/
12 // Copyright (C) 2001-2017 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 
39 // ===========================================================================
40 // method definitions
41 // ===========================================================================
42 IDSupplier::IDSupplier(const std::string& prefix, long long int begin)
43  : myCurrent(begin), myPrefix(prefix) {}
44 
45 
46 
47 IDSupplier::IDSupplier(const std::string& prefix, const std::vector<std::string>& knownIDs)
48  : myCurrent(0), myPrefix(prefix) {
49  for (std::vector<std::string>::const_iterator id_it = knownIDs.begin(); id_it != knownIDs.end(); ++id_it) {
50  avoid(*id_it);
51  }
52 }
53 
54 
56 
57 
58 std::string
60  std::ostringstream strm;
61  strm << myPrefix << myCurrent++;
62  return strm.str();
63 }
64 
65 
66 void
67 IDSupplier::avoid(const std::string& id) {
68  // does it start with prefix?
69  if (id.find(myPrefix) == 0) {
70  long long int number;
71  std::istringstream buf(id.substr(myPrefix.size()));
72  buf >> number;
73  // does it continue with a number?
74  if (!buf.fail()) {
75  myCurrent = MAX2(myCurrent, number + 1);
76  }
77  }
78 }
79 
80 
81 /****************************************************************************/
82 
std::string myPrefix
The prefix to use.
Definition: IDSupplier.h:72
IDSupplier(const std::string &prefix="", long long int begin=0)
Constructor.
Definition: IDSupplier.cpp:42
void avoid(const std::string &id)
make sure that the given id is never supplied
Definition: IDSupplier.cpp:67
T MAX2(T a, T b)
Definition: StdDefs.h:70
std::string getNext()
Returns the next id.
Definition: IDSupplier.cpp:59
~IDSupplier()
Destructor.
Definition: IDSupplier.cpp:55
long long int myCurrent
The current index.
Definition: IDSupplier.h:69