/* This file is part of KDBindings. SPDX-FileCopyrightText: 2021 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> Author: Sean Harmer <sean.harmer@kdab.com> SPDX-License-Identifier: MIT Contact KDAB at <info@kdab.com> for commercial licensing options.*/#pragma once#include<functional>#include<map>#include<memory>namespaceKDBindings{classBindingEvaluator{// We use pimpl here so that we can pass evaluators around by value (copies)// yet each copy refers to the same set of datastructPrivate{// TODO: Use std::vector here?std::map<int,std::function<void()>>m_bindingEvalFunctions;intm_currentId;};public:BindingEvaluator()=default;BindingEvaluator(constBindingEvaluator&)noexcept=default;BindingEvaluator&operator=(constBindingEvaluator&)noexcept=default;BindingEvaluator(BindingEvaluator&&other)noexcept=delete;BindingEvaluator&operator=(BindingEvaluator&&other)noexcept=delete;voidevaluateAll()const{// a std::map's ordering is deterministic, so the bindings are evaluated// in the order they were inserted, ensuring correct transitive dependency// evaluation.for(auto&[id,func]:m_d->m_bindingEvalFunctions)func();}private:template<typenameBindingType>intinsert(BindingType*binding){m_d->m_bindingEvalFunctions.insert({++(m_d->m_currentId),[=](){binding->evaluate();}});returnm_d->m_currentId;}voidremove(intid){m_d->m_bindingEvalFunctions.erase(id);}std::shared_ptr<Private>m_d{std::make_shared<Private>()};template<typenameT,typenameUpdaterT>friendclassBinding;};classImmediateBindingEvaluatorfinal:publicBindingEvaluator{public:staticinlineImmediateBindingEvaluatorinstance(){staticImmediateBindingEvaluatorevaluator;returnevaluator;}};}// namespace KDBindings