////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
// All Rights Reserved.
// Confidential and Proprietary - Qualcomm Technologies, Inc.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// THIS IS AUTO-GENERATED CODE.  PLEASE DO NOT EDIT (File bug reports against tools).
///
/// Auto-generated by: Parameter Parser V5.5.1 (2411131018)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @file  bitmlsetmanager.cpp
/// @brief Auto-generated Chromatix parameter file
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


#include "bitmlsetmanager.h"

bitmlSetManager::bitmlSetManager()
{
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /// MinimumNodes
    ///
    /// @brief  Set the minimum number of scenario modes to keep in memory before unloading scenario modes.
    ///
    /// The minimum number of scenario modes to keep active before unloading modes. Used when loading binaries from
    /// a file to enable the scenario mode delay load feature (default is loaded at startup, and new scenario modes
    /// are loaded as they are entered). Switching between Snapshot and Preview mode takes up to 10 scenarios
    /// modes (depending on if the scenario modes have scenes and effects), with no links, and higher with links.
    /// to allow seamless picture taking, set the minimum to at least 10. The default value is 17 to account for
    /// linked modes. Smaller values will increase memory usage efficiency at the cost of scenario mode switching
    /// latency. To use a different value, set the ParameterParser --MinNodes <number> command line option when
    /// generating source (the s command). Setting this value to 0 disables auto scenario mode unloading.
    /// Call the UnloadMode function to manually unload a scenario mode. Calling UnloadMode directly will ignore
    /// Module reference counts, unload all modules and set their reference counts to 0.  Do not call UnloadMode
    /// unless you are certain no modules are referenced within the mode.
    ///
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    MinimumModes   = 17;
    DelayLoad      = true;
    LogError       = true;
    LogWarning     = true;
    LogDebug       = true;
    SimulatorMode  = false;

    AddModule(PARAMETER_NEW camxbitmlconfig::BITMLConfigTypeClass("BITMLConfig"));

    PARAMETER_LOG_DEBUG(this, "bitmlSetManager constructed: %s\n", VERSION);
};

bitmlSetManager::~bitmlSetManager()
{
    PARAMETER_LOG_DEBUG(this, "bitmlSetManager deleting\n");

    ParameterMutexLock rlock {ReloadMutex, false, true}; // Unique LOCK

    std::unordered_map<std::string, DefaultModule*>::iterator iter = DefaultModuleMap.begin();
    while (iter != DefaultModuleMap.end())
    {
        PARAMETER_DELETE iter->second;
        iter++;
    }

    DefaultModuleMap.clear();
}

void bitmlSetManager::AddModule(ParameterModule* module)
{
    if (module != nullptr && module->Type != nullptr)
    {
        ParameterMutexLock rlock {ReloadMutex, false, true}; // Unique LOCK

        DefaultModuleMap[module->Type] = PARAMETER_NEW DefaultModule(module);
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// UpdateBinaryParameters
///
/// @brief Replaces exising parameters with the ones in a buffer. LoadBinaryParameters must be called first
///
/// @param buffer   Binary parameter buffer
/// @param length   Length of the binary parameter buffer
///
/// @return Node
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool bitmlSetManager::UpdateBinaryParameters(
    UINT8* const buffer,
    const SIZE_T length)
{
    bool result = Valid;

    if (result)
    {
        ParameterMutexLock rlock {ReloadMutex, false, true}; // Unique LOCK
        if (IsFileMode())
        {
            result = ReloadBinaryParameters(true);
        }

        if (result)
        {
            bitmlSetManager* update = PARAMETER_NEW bitmlSetManager();

            if (update->LoadBinaryParameters(buffer, length))
            {
                (void)UpdateParameters(update);
                result = true;
            }
            else
            {
                SetError("UpdateBinaryParameters failed to load parameters");
            }
            m_updateSM.push_back((ParameterSetManager*)update);
        }
        else
        {
            SetError("UpdateBinaryParameters failed to reload parameters in buffer mode");
        }
    }
    else
    {
        SetError("UpdateBinaryParameters failed, original parameter load is invalid");
    }

    return result;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// SaveModules
///
/// @brief  Saves all current modules to the m_replaced list to prevent accessing deleted memory
///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void bitmlSetManager::SaveAndClearModules()
{
    std::unordered_map<std::string, DefaultModule*>::iterator iter = DefaultModuleMap.begin();
    while (iter != DefaultModuleMap.end())
    {
        DefaultModule* const module = iter->second;
        ModuleLink*    link   = module->FirstModule;
        while (link != nullptr)
        {
            ModuleLink* const next = link->Next;
            m_replaced.push_back(link->Module);
            link->Module = nullptr;
            link->Next   = nullptr;
            PARAMETER_DELETE link;
            link = next;

        }
        module->FirstModule = nullptr;
        module->LastModule  = nullptr;
        iter++;
    }
}

camxbitmlconfig::BITMLConfigType* bitmlSetManager::GetModule_BITMLConfig(
    const TuningMode* const modeBranch,
    UINT32 const modeCount,
    camxbitmlconfig::BITMLConfigType* const release)
{
    ParameterModule* const pModule = GetModule((const CHAR *)"BITMLConfig",
        modeBranch, modeCount, (ParameterModule*)(camxbitmlconfig::BITMLConfigTypeClass*)release);

    return (camxbitmlconfig::BITMLConfigType*)(camxbitmlconfig::BITMLConfigTypeClass*)pModule;
}

