Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[new-core-alignment] ecal_expmap not using std::iterator anymore #1357

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 37 additions & 25 deletions ecal/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 All @@ -23,13 +23,14 @@

#pragma once

#include <chrono>
#include <functional>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <utility>
#include <vector>
#include <chrono>

namespace eCAL
{
Expand All @@ -41,32 +42,37 @@ 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 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 allocator_type = Alloc;
using value_type = std::pair<const Key, T>;
using reference = typename Alloc::reference;
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:
iterator(const typename key_to_value_type::iterator _it)
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>&;

explicit iterator(const typename key_to_value_type::iterator _it)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rex-schilasky how come you made the constructor explicit?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what about the iterator tag, is it not necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what about the iterator tag, is it not necessary?

It's not 100 percent clear to me why you need all 5, it also seems to work with 3 and clang reports 2 of 5 as "unused". But I will complete it again.

: it(_it)
{}

Expand All @@ -86,44 +92,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:
const_iterator(const iterator& other)
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>&;

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

const_iterator(const typename key_to_value_type::const_iterator _it)
explicit 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 All @@ -135,7 +147,7 @@ namespace eCAL

// Constructor specifies the timeout of the map
CExpMap() : _timeout(std::chrono::milliseconds(5000)) {};
CExpMap(clock_type::duration t) : _timeout(t) {};
explicit CExpMap(clock_type::duration t) : _timeout(t) {};

/**
* @brief set expiration time
Expand Down
Loading