mod-playerbots/src/Util/LazyCalculatedValue.h
bashermens 6cf7f1aaef
[CHORE] Util classes format and simple cleanup with generated resources (#2028)
- Did some basic formatting
- Some generated docs
- Cleaned header/impl Helper.css
- Moved PerfMonitor from util to bot/handler/command

Still a freaking mess though, but its a start i guess. Cant ask ppl to
add more or make use of those when its so messy.
2026-01-18 00:43:44 +01:00

75 lines
2.1 KiB
C++

/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license, you may redistribute it
* and/or modify it under version 3 of the License, or (at your option), any later version.
*/
#ifndef _PLAYERBOT_LAZYCALCULATEDVALUE_H
#define _PLAYERBOT_LAZYCALCULATEDVALUE_H
/**
* @brief Lazy calculation helper.
*
* Stores a function pointer (calculator) and its owner instance, and
* calculates the value only when it is requested for the first time.
* The result is cached until Reset() is called.
*
* @tparam TValue Type of the calculated value.
* @tparam TOwner Type of the owner class containing the calculator function.
*/
template <class TValue, class TOwner>
class LazyCalculatedValue
{
public:
/**
* @brief Type of the calculator function.
*
* This is a pointer to a member function of TOwner returning TValue.
*/
typedef TValue (TOwner::*Calculator)();
public:
/**
* @brief Constructor.
*
* @param owner Pointer to the owner object.
* @param calculator Pointer to the member function used to calculate the value.
*/
LazyCalculatedValue(TOwner* owner, Calculator calculator) : calculator(calculator), owner(owner) { Reset(); }
public:
/**
* @brief Get the cached value or calculate it if needed.
*
* If the value has not been calculated yet, it calls the calculator
* on the owner and caches the result.
*
* @return TValue The calculated or cached value.
*/
TValue GetValue()
{
if (!calculated)
{
value = (owner->*calculator)();
calculated = true;
}
return value;
}
/**
* @brief Reset the cached state.
*
* After calling Reset(), the next call to GetValue() will recalculate
* the value again.
*/
void Reset() { calculated = false; }
protected:
Calculator calculator; ///< Pointer to calculator member function
TOwner* owner; ///< Owner instance
bool calculated; ///< Whether value has already been calculated
TValue value; ///< Cached value
};
#endif