Files
UnrealEngineUWP/Engine/Plugins/Runtime/MeshModelingToolset/Source/ModelingComponents/Public/Changes/ValueWatcher.h
henrik karlsson b5164ac775 Fixes to make modules compile with IWYU. We've added to IWYU toolchain so it compiles "orphaned" headers which does not have a owning cpp file. This identified lots of headers that couldn't be compiled by themselves (or if they were to included first)
Change consist of only forward declaration and additional includes

#preflight 63789c1de30d438849c48188
#rb none

[CL 23218412 by henrik karlsson in ue5-main branch]
2022-11-21 03:22:23 -05:00

53 lines
1.5 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "Templates/Function.h"
/**
* TValueWatcher is used to implement a common Tool pattern where it is necessary to
* essentially poll a value to see if it has changed, and if it has, call a function.
* For example UObject UProperties are frequently directly modified by (eg) DetailsView/etc,
* this results in PropertyChange events that can be used in the editor to respond to changes.
* However at Runtime no such events are generated and so the Tool has no way of knowing
* when UProperties change except by polling and comparing with a cached value.
* The purpose of this class is simply to encapsulate this common pattern/logic.
*/
template<typename ValueType>
class TValueWatcher
{
ValueType CachedValue;
TFunction<ValueType(void)> GetValueFunc;
TFunction<void(ValueType)> OnValueChangedFunc;
public:
void Initialize(
TFunction<ValueType(void)> GetValueFuncIn,
TFunction<void(ValueType)> OnValueChangedFuncIn,
ValueType InitialValue)
{
GetValueFunc = GetValueFuncIn;
OnValueChangedFunc = OnValueChangedFuncIn;
CachedValue = InitialValue;
}
void CheckAndUpdate()
{
ValueType CurValue = GetValueFunc();
if (CurValue != CachedValue)
{
CachedValue = GetValueFunc();
OnValueChangedFunc(CachedValue);
}
}
/**
* Update known value without calling OnValueChangedFunc. Sometimes necessary during undo/redo.
*/
void SilentUpdate()
{
CachedValue = GetValueFunc();
}
};