Skip to content

Commit

Permalink
Merge pull request #16 from eclipse-ecal/expmap-iterator-fix-cpp17
Browse files Browse the repository at this point in the history
Expmap iterator fix cpp17
  • Loading branch information
rex-schilasky authored Feb 9, 2024
2 parents 6595a8e + 6cae2b3 commit 9964636
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
49 changes: 31 additions & 18 deletions src/core/src/util/ecal_expmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -42,31 +42,38 @@ namespace eCAL
template<class Key,
class T,
class Compare = std::less<Key>,
class Alloc = std::allocator<std::pair<const Key, T> > >
class CExpMap
class Alloc = std::allocator<std::pair<const Key, T> > >
class CExpMap
{
public:
using clock_type = std::chrono::steady_clock;

// Key access history, most recent at back
using key_tracker_type = std::list<std::pair<clock_type::time_point, Key>>;

// Key to value and key history iterator
using key_to_value_type = std::map<Key, std::pair<T, typename key_tracker_type::iterator>>;

using allocator_type = Alloc;
using value_type = std::pair<const Key, T>;
using reference = typename Alloc::reference;
using allocator_type = Alloc;
using value_type = std::pair<const Key, T>;
using reference = typename Alloc::reference;
using const_reference = typename Alloc::const_reference;
using difference_type = typename Alloc::difference_type;
using size_type = typename Alloc::size_type;
using key_type = Key;
using mapped_type = T;
using size_type = typename Alloc::size_type;
using key_type = Key;
using mapped_type = T;

class iterator : public std::iterator<std::bidirectional_iterator_tag, std::pair<Key, T>>
class iterator
{
friend class const_iterator;

public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = std::pair<Key, T>;
using difference_type = std::ptrdiff_t;
using pointer = std::pair<Key, T>*;
using reference = std::pair<Key, T>&;

iterator(const typename key_to_value_type::iterator _it)
: it(_it)
{}
Expand All @@ -87,44 +94,50 @@ namespace eCAL
{
return std::make_pair(it->first, it->second.first);
}

//friend void swap(iterator& lhs, iterator& rhs); //C++11 I think
bool operator==(const iterator& rhs) const { return it == rhs.it; }
bool operator!=(const iterator& rhs) const { return it != rhs.it; }

private:
typename key_to_value_type::iterator it;
};
};

class const_iterator : public std::iterator<std::bidirectional_iterator_tag, const std::pair<Key, T>>
class const_iterator
{
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = std::pair<Key, T>;
using difference_type = std::ptrdiff_t;
using pointer = std::pair<Key, T>*;
using reference = std::pair<Key, T>&;

const_iterator(const iterator& other)
: it(other.it)
{}

const_iterator(const typename key_to_value_type::const_iterator _it)
: it(_it)
{}

const_iterator& operator++()
{
it++;
return *this;
} //prefix increment

const_iterator& operator--()
{
it--;
return *this;
} //prefix decrement
//reference operator*() const

std::pair<Key, T> operator*() const
{
return std::make_pair(it->first, it->second.first);
}

//friend void swap(iterator& lhs, iterator& rhs); //C++11 I think
bool operator==(const const_iterator& rhs) const { return it == rhs.it; }
bool operator!=(const const_iterator& rhs) const { return it != rhs.it; }
Expand Down
2 changes: 0 additions & 2 deletions tests/expmap_test/src/expmap_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

#include <gtest/gtest.h>

#include <iostream>

TEST(ExpMap, ExpMapSetGet)
{
// create the map with 2500 ms expiration
Expand Down

0 comments on commit 9964636

Please sign in to comment.