none
How to write a generic function to insert elements into the map container using C++? RRS feed

  • Question

  • Hello All,

    I am inserting elements into the map container. For this I am using the same statements multiple times in the below function. Is there any way I can write a generic function for this? So that even at the later point of time, when required, I can insert new elements.

    bool EMRMgr::GetParams()
    {
    EmIOStruct emIOstructObj;
    WCHAR szValue[MAX_PATH] = { 0 };
    DWORD dwDataSize = sizeof(szValue) / sizeof(WCHAR);
    long lRes = 0;
    McStorageType mcStorageTypeObj = McStorageType::eRegistry;
    std::wstring value;

    // First time I am using the below statements to insert elements into the map container
    mcIOstructObj.lpszValueName = (LPWSTR)ER_ID;
    memset(szValue, 0, MAX_PATH);
    mcIOstructObj.lpData = (LPBYTE)&szValue[0];
    lRes = m_cIOManager.ReadValue(mcStorageTypeObj, mcIOstructObj);
    value.clear();
    if ((LPWSTR)mcIOstructObj.lpData == nullptr)
    {
    value.assign(L"");
    }
    else
    {
    value.assign((LPWSTR)mcIOstructObj.lpData);
    }

    m_fileParams.insert({ (std::wstring) ER_ID, value });

    // Second time I am using the below statements to insert elements into the map container


    mcIOstructObj.lpszValueName = (LPWSTR)CPS;
    memset(szValue, 0, MAX_PATH);
    mcIOstructObj.lpData = (LPBYTE)&szValue[0];
    lRes = m_cIOManager.ReadValue(mcStorageTypeObj, mcIOstructObj);
    value.clear();
    if ((LPWSTR)mcIOstructObj.lpData == nullptr)
    {
    value.assign(L"");
    }
    else
    {
    value.assign((LPWSTR)mcIOstructObj.lpData);
    }
    m_fileParams.insert({ (std::wstring) CPS, value });

    return true;
    }

    could anyone please help me on this?

    Thanks in advance.

    Sunday, August 18, 2019 4:54 PM

All replies

  • Maybe something like this:

     

    void LoadValue( LPCWSTR name )

    {

       mcIOstructObj.lpszValueName = name;

       WCHAR value[MAX_PATH + 1] = "";

       mcIOstructObj.lpData = (LPBYTE)&value[0];

       m_cIOManager.ReadValue( McStorageType::eRegistry, mcIOstructObj);

       m_fileParams[ name ] = mcIOstructObj.lpData == nullptr ? L"" : value;

    }

     


    • Edited by Viorel_MVP Sunday, August 18, 2019 7:28 PM
    Sunday, August 18, 2019 7:20 PM