Move RoadInfoIterator to its own header

This commit is contained in:
nsubiron 2019-03-27 14:14:18 +01:00
parent 918109f010
commit 19ce185c93
3 changed files with 107 additions and 92 deletions

View File

@ -6,10 +6,10 @@
#pragma once #pragma once
#include "carla/NonCopyable.h"
#include "carla/road/RoadElementSet.h" #include "carla/road/RoadElementSet.h"
#include "carla/road/element/RoadInfo.h" #include "carla/road/element/RoadInfo.h"
#include "carla/road/element/RoadInfoVisitor.h" #include "carla/road/element/RoadInfoIterator.h"
#include "carla/NonCopyable.h"
#include <vector> #include <vector>
#include <memory> #include <memory>
@ -47,7 +47,6 @@ namespace road {
private: private:
RoadElementSet<std::unique_ptr<element::RoadInfo>> _road_set; RoadElementSet<std::unique_ptr<element::RoadInfo>> _road_set;
}; };
} // road } // road

View File

@ -0,0 +1,105 @@
// Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma
// de Barcelona (UAB).
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
#pragma once
#include "carla/Debug.h"
#include "carla/road/element/RoadInfoVisitor.h"
#include <iterator>
#include <memory>
namespace carla {
namespace road {
namespace element {
template <typename T, typename IT>
class RoadInfoIterator : private RoadInfoVisitor {
public:
static_assert(std::is_same<std::unique_ptr<RoadInfo>, typename IT::value_type>::value, "Not compatible.");
using value_type = T;
using difference_type = typename IT::difference_type;
using pointer = T *;
using reference = T &;
RoadInfoIterator(IT begin, IT end)
: _it(begin),
_end(end) {
_success = false;
for (; !IsAtEnd(); ++_it) {
DEBUG_ASSERT((*_it) != nullptr);
(*_it)->AcceptVisitor(*this);
if (_success) {
break;
}
}
}
RoadInfoIterator &operator++() {
_success = false;
while (!_success) {
++_it;
if (IsAtEnd()) {
break;
}
DEBUG_ASSERT((*_it) != nullptr);
(*_it)->AcceptVisitor(*this);
}
return *this;
}
reference operator*() const {
DEBUG_ASSERT((*_it) != nullptr);
return static_cast<T &>(**_it);
}
pointer operator->() const {
DEBUG_ASSERT((*_it) != nullptr);
return static_cast<T *>(_it->get());
}
bool operator!=(const RoadInfoIterator &rhs) const {
return _it != rhs._it;
}
bool operator==(const RoadInfoIterator &rhs) const {
return !((*this) != rhs);
}
bool IsAtEnd() const {
return _it == _end;
}
private:
void Visit(T &) {
_success = true;
}
IT _it;
IT _end;
bool _success;
};
template <typename T, typename Container>
static auto MakeRoadInfoIterator(const Container &c) {
auto begin = std::begin(c);
auto end = std::end(c);
return RoadInfoIterator<T, decltype(begin)>(begin, end);
}
template <typename T, typename IT>
static auto MakeRoadInfoIterator(IT begin, IT end) {
return RoadInfoIterator<T, decltype(begin)>(begin, end);
}
} // namespace element
} // namespace road
} // namespace carla

View File

@ -6,11 +6,6 @@
#pragma once #pragma once
#include "carla/Debug.h"
#include <iterator>
#include <memory>
namespace carla { namespace carla {
namespace road { namespace road {
namespace element { namespace element {
@ -50,90 +45,6 @@ namespace element {
virtual void Visit(RoadInfoSpeed &) {} virtual void Visit(RoadInfoSpeed &) {}
}; };
template <typename T, typename IT>
class RoadInfoIterator : private RoadInfoVisitor {
public:
static_assert(std::is_same<std::unique_ptr<RoadInfo>, typename IT::value_type>::value, "Not compatible.");
using value_type = T;
using difference_type = typename IT::difference_type;
using pointer = T *;
using reference = T &;
RoadInfoIterator(IT begin, IT end)
: _it(begin),
_end(end) {
_success = false;
for (; !IsAtEnd(); ++_it) {
DEBUG_ASSERT((*_it) != nullptr);
(*_it)->AcceptVisitor(*this);
if (_success) {
break;
}
}
}
RoadInfoIterator &operator++() {
_success = false;
while (!_success) {
++_it;
if (IsAtEnd()) {
break;
}
DEBUG_ASSERT((*_it) != nullptr);
(*_it)->AcceptVisitor(*this);
}
return *this;
}
reference operator*() const {
DEBUG_ASSERT((*_it) != nullptr);
return static_cast<T &>(**_it);
}
pointer operator->() const {
DEBUG_ASSERT((*_it) != nullptr);
return static_cast<T *>(_it->get());
}
bool operator!=(const RoadInfoIterator &rhs) const {
return _it != rhs._it;
}
bool operator==(const RoadInfoIterator &rhs) const {
return !((*this) != rhs);
}
bool IsAtEnd() const {
return _it == _end;
}
private:
void Visit(T &) {
_success = true;
}
IT _it;
IT _end;
bool _success;
};
template <typename T, typename Container>
static auto MakeRoadInfoIterator(const Container &c) {
auto begin = std::begin(c);
auto end = std::end(c);
return RoadInfoIterator<T, decltype(begin)>(begin, end);
}
template <typename T, typename IT>
static auto MakeRoadInfoIterator(IT begin, IT end) {
return RoadInfoIterator<T, decltype(begin)>(begin, end);
}
} // namespace element } // namespace element
} // namespace road } // namespace road
} // namespace carla } // namespace carla