/* Copyright 2017 - 2022 R. Thomas * Copyright 2017 - 2022 Quarkslab * * 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. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef LIEF_ASSOCIATIVE_ITERATORS_H_ #define LIEF_ASSOCIATIVE_ITERATORS_H_ #include #include #include #include #include #include #include #include "LIEF/iterators.hpp" #include "LIEF/exception.hpp" namespace LIEF { template using decay_t = typename std::decay::type; template using add_const_t = typename std::add_const::type; template using remove_const_t = typename std::remove_const::type; template using add_pointer_t = typename std::add_pointer::type; template using remove_pointer_t = typename std::remove_pointer::type; template using add_ref_t = typename add_lvalue_reference::type; template struct dict_iterator_pair { public: using value_ref_t = remove_pointer_t>; using key_ref_t = remove_pointer_t>; static_assert(std::is_pointer, "Require pointer!"); static_assert(std::is_pointer, "Require pointer!"); dict_iterator_pair(KEY key, VALUE value) : key_{key}, value_{value} {} add_const_t key() const { return const_cast>(key_); } typename std::enable_if::value, remove_const_t>::type key() const { return const_cast>(static_cast(this)->key()); } add_const_t value() const { return const_cast>(*value_); } typename std::enable_if::value, remove_const_t>::type value() const { return const_cast>(static_cast(this)->value()); } private: KEY key_; VALUE value_; }; // Iterator which return ref on container's values // =============================================== template::iterator> class dict_iterator : public std::iterator< std::bidirectional_iterator_tag, dict_iterator_pair< add_pointer_t>>, add_pointer_t>> >, ssize_t, dict_iterator_pair< add_pointer_t>>, add_pointer_t>> >*, const dict_iterator_pair< add_pointer_t>>, add_pointer_t>> >&> { public: using DT = decay_t; using value_ref_t = add_ref_t>>; using value_pointer_t = add_pointer_t>>; using key_pointer_t = add_pointer_t>>; using keys_iterator_t = ref_iterator>; using values_iterator_t = ref_iterator>; using result_t = dict_iterator_pair< add_pointer_t>>, add_pointer_t>> >; dict_iterator(T container) : container_{std::forward(container)}, distance_{0} { it_ = std::begin(container_); } dict_iterator(const dict_iterator& copy) : container_{copy.container_}, it_{std::begin(container_)}, distance_{copy.distance_} { std::advance(it_, distance_); } dict_iterator operator=(dict_iterator other) { swap(other); return *this; } void swap(dict_iterator& other) { std::swap(container_, other.container_); std::swap(it_, other.it_); std::swap(distance_, other.distance_); } dict_iterator& operator++() { it_ = std::next(it_); distance_++; return *this; } dict_iterator operator++(int) { dict_iterator retval = *this; ++(*this); return retval; } dict_iterator& operator--() { if (it_ != std::begin(container_)) { it_ = std::prev(it_); distance_--; } return *this; } dict_iterator operator--(int) { dict_iterator retval = *this; --(*this); return retval; } dict_iterator& operator+=(const typename dict_iterator::difference_type& movement) { std::advance(it_, movement); distance_ += movement; return *this; } dict_iterator& operator-=(const typename dict_iterator::difference_type& movement) { return (*this) += -movement; } //typename std::enable_if::value, remove_const_t>::type //operator[](size_t n) { // return const_cast>(static_cast(this)->operator[](n)); //} //add_const_t operator[](size_t n) const { // assert(n < size() && "integrity_error: out of bound") // auto it = begin(); // std::advance(it, n); // return const_cast>(*it); //} dict_iterator operator+(typename dict_iterator::difference_type n) const { dict_iterator tmp = *this; return tmp += n; } dict_iterator operator-(typename dict_iterator::difference_type n) const { dict_iterator tmp = *this; return tmp -= n; } typename dict_iterator::difference_type operator-(const dict_iterator& rhs) const { return distance_ - rhs.distance_; } bool operator<(const dict_iterator& rhs) const { return (rhs - *this) > 0; } bool operator>(const dict_iterator& rhs) const { return rhs < *this; } bool operator>=(const dict_iterator& rhs) const { return !(*this < rhs); } bool operator<=(const dict_iterator& rhs) const { return !(*this > rhs); } dict_iterator begin() const { return container_; } dict_iterator cbegin() const { return begin(); } dict_iterator end() const { dict_iterator it = dict_iterator{container_}; it.it_ = std::end(it.container_); it.distance_ = it.size(); return it; } dict_iterator cend() const { return end(); } bool operator==(const dict_iterator& other) const { return (size() == other.size() && distance_ == other.distance_); } bool operator!=(const dict_iterator& other) const { return !(*this == other); } size_t size() const { return container_.size(); } typename std::enable_if::value, remove_const_t>::type operator*() { return const_cast>(static_cast(this)->operator*()); } template typename std::enable_if::value, add_const_t>::type operator*() const { assert(*it_ && "integrity error: nupptr"); return const_cast>(**it_); } template typename std::enable_if::value, add_const_t>::type operator*() const { return const_cast>(*(it_)); } typename std::enable_if::value, pointer_t>::type operator->() { return const_cast>(static_cast(this)->operator->()); } add_const_t operator->() const { return const_cast>(&(operator*())); } protected: T container_; ITERATOR_T it_; typename ref_iterator::difference_type distance_; }; // Iterator which return const ref on container's values // ===================================================== //template::type> //using const_ref_iterator = ref_iterator::const_iterator>; } #endif