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