make config iteration usefull by also returning the key (oops)

This commit is contained in:
Green Sky 2023-12-12 16:51:43 +01:00
parent 0b4179c039
commit 2b20c2d2a4
No known key found for this signature in database
2 changed files with 9 additions and 3 deletions

View File

@ -42,13 +42,18 @@ struct ConfigModelI {
// actual range cant be virtual // actual range cant be virtual
template<typename Type> template<typename Type>
struct ConstEntryProxy { struct ConstEntryProxy {
struct Pair {
std::string key;
Type value;
};
struct ConstEntryIteratorI { struct ConstEntryIteratorI {
virtual ~ConstEntryIteratorI(void) {} virtual ~ConstEntryIteratorI(void) {}
virtual std::unique_ptr<ConstEntryIteratorI> clone(void) = 0; virtual std::unique_ptr<ConstEntryIteratorI> clone(void) = 0;
virtual bool equal(const ConstEntryIteratorI& other) const = 0; virtual bool equal(const ConstEntryIteratorI& other) const = 0;
virtual void incrementOne(void) = 0; virtual void incrementOne(void) = 0;
//virtual const Type& getValue(void) const = 0; //virtual const Type& getValue(void) const = 0;
virtual Type getValue(void) const = 0; virtual Pair getValue(void) const = 0;
}; };
// actual iterator cant be virtual // actual iterator cant be virtual
@ -69,7 +74,7 @@ struct ConfigModelI {
bool operator==(const ConstEntryIterator& other) const { return _value->equal(*other._value); } bool operator==(const ConstEntryIterator& other) const { return _value->equal(*other._value); }
bool operator!=(const ConstEntryIterator& other) const { return !operator==(other); } bool operator!=(const ConstEntryIterator& other) const { return !operator==(other); }
ConstEntryIterator& operator++(void) { _value->incrementOne(); return *this; } ConstEntryIterator& operator++(void) { _value->incrementOne(); return *this; }
Type operator*(void) const { return _value->getValue(); } Pair operator*(void) const { return _value->getValue(); }
}; };
ConstEntryIterator _begin; ConstEntryIterator _begin;

View File

@ -42,6 +42,7 @@ struct SimpleConfigModel : public ConfigModelI {
// iteration // iteration
template<typename Type, typename RealType = Type> template<typename Type, typename RealType = Type>
struct SimpleConstEntryIteratorImpl : public ConfigModelI::ConstEntryProxy<Type>::ConstEntryIteratorI { struct SimpleConstEntryIteratorImpl : public ConfigModelI::ConstEntryProxy<Type>::ConstEntryIteratorI {
using BaseType = typename ConfigModelI::ConstEntryProxy<Type>;
using BaseIteratorIType = typename ConfigModelI::ConstEntryProxy<Type>::ConstEntryIteratorI; using BaseIteratorIType = typename ConfigModelI::ConstEntryProxy<Type>::ConstEntryIteratorI;
using MapType = std::map<std::string, RealType>; using MapType = std::map<std::string, RealType>;
using MapTypeIterator = typename MapType::const_iterator; using MapTypeIterator = typename MapType::const_iterator;
@ -55,7 +56,7 @@ struct SimpleConfigModel : public ConfigModelI {
std::unique_ptr<BaseIteratorIType> clone(void) override { return std::make_unique<SimpleConstEntryIteratorImpl>(_self); } std::unique_ptr<BaseIteratorIType> clone(void) override { return std::make_unique<SimpleConstEntryIteratorImpl>(_self); }
bool equal(const BaseIteratorIType& other) const override { return _self == static_cast<const SimpleConstEntryIteratorImpl&>(other)._self; } bool equal(const BaseIteratorIType& other) const override { return _self == static_cast<const SimpleConstEntryIteratorImpl&>(other)._self; }
void incrementOne(void) override { ++_self; } void incrementOne(void) override { ++_self; }
Type getValue(void) const override { return _self->second; } typename BaseType::Pair getValue(void) const override { return {_self->first, _self->second}; }
// helper // helper
static ConfigModelI::ConstEntryProxy<Type> createRange(const MapTypeIterator& begin, const MapTypeIterator& end) { static ConfigModelI::ConstEntryProxy<Type> createRange(const MapTypeIterator& begin, const MapTypeIterator& end) {