OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
RegisteredStringMap.cpp
Go to the documentation of this file.
1 
12 // Copyright 2015 Sensics, Inc.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "License");
15 // you may not use this file except in compliance with the License.
16 // You may obtain a copy of the License at
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 // See the License for the specific language governing permissions and
24 // limitations under the License.
25 
26 // Internal Includes
28 
29 // Library/third-party includes
30 #include <boost/algorithm/string.hpp>
31 
32 // Standard includes
33 #include <iostream>
34 
35 namespace osvr {
36 namespace common {
37 
40  auto n = m_regEntries.size();
41  std::cout << "Current map contains " << m_regEntries.size()
42  << " entries: " << std::endl;
43  for (decltype(n) i = 0; i < n; ++i) {
44  std::cout << "ID: " << i << "; "
45  << "Name: " << m_regEntries[i] << std::endl;
46  }
47  }
48 
50  auto entry = std::find(begin(m_regEntries), end(m_regEntries), str);
51  if (end(m_regEntries) != entry) {
52  // we found it.
53  return util::StringID(std::distance(begin(m_regEntries), entry));
54  }
55 
56  // we didn't find an entry in the registry so we'll add a new one
57  auto ret = util::StringID(
58  m_regEntries.size()); // will be the location of the next insert.
59  m_regEntries.push_back(str);
60  m_modified = true;
61  return ret;
62  }
63 
65 
66  // requested non-existent ID (include sanity check)
67  if (id.value() >= m_regEntries.size()) {
68  // returning empty string
70  return std::string();
71  }
72 
73  return m_regEntries[id.value()];
74  };
75 
78  std::vector<std::string> RegisteredStringMap::getEntries() const {
79  return m_regEntries;
80  }
81 
83  return m_local.getStringID(str);
84  }
85 
87  return m_local.getStringFromId(id);
88  }
89 
92  if (peerID.empty()) {
93  return util::StringID();
94  }
95  if (peerID.value() >= m_remoteToLocal.size()) {
96  throw std::out_of_range("Peer ID out of range!");
97  }
98  return util::StringID(m_remoteToLocal[peerID.value()]);
99  }
100 
102  std::vector<std::string> const &peerEntries) {
103  m_remoteToLocal.clear();
104  auto n = peerEntries.size();
105  for (uint32_t i = 0; i < n; ++i) {
106  m_remoteToLocal.push_back(
107  m_local.getStringID(peerEntries[i]).value());
108  }
109  }
110 } // namespace common
111 } // namespace osvr
util::StringID convertPeerToLocalID(util::PeerStringID peerID) const
void clearModifiedFlag()
Clear the modified flag.
void printCurrentMap()
helper function to print size and contents of the map
util::StringID getStringID(std::string const &str)
util::StringID getStringID(std::string const &str)
void setupPeerMappings(std::vector< std::string > const &peerEntries)
This populates the data structure used by the above method.
std::string getStringFromId(util::StringID id) const
bool m_modified
special flag that gets switched whenever new element is inserted;
std::string getStringFromId(util::StringID id) const
bool isModified() const
Has a new entry been added since the flag was last cleared?