Search code examples
c++visual-c++stdlist

Can I simplify the population of this std::list?


Can I simplify the populate of my std::list code:

void CMeetingScheduleAssistantApp::InitBrowserRegistryLookupList(RegistryPathList& rListRegPaths)
{
    S_REGISTRY_PATH  sRegPath;

    // Reset the list
    rListRegPaths.clear();

    // These will be "native" 32bit or native 64bit browsers (i.e. the Operating System bitness)
    sRegPath.hRootKey = HKEY_LOCAL_MACHINE;
    sRegPath.strKeyPath = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\firefox.exe");
    sRegPath.strBrowser = _T("Firefox");
    rListRegPaths.push_back(sRegPath);

    sRegPath.hRootKey = HKEY_LOCAL_MACHINE;
    sRegPath.strKeyPath = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\IEXPLORE.EXE");
    sRegPath.strBrowser = _T("Internet Explorer");
    rListRegPaths.push_back(sRegPath);

    sRegPath.hRootKey = HKEY_LOCAL_MACHINE;
    sRegPath.strKeyPath = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe");
    sRegPath.strBrowser = _T("Google Chrome");
    rListRegPaths.push_back(sRegPath);

    sRegPath.hRootKey = HKEY_CURRENT_USER;
    sRegPath.strKeyPath = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\opera.exe");
    sRegPath.strBrowser = _T("Opera Internet Browser");
    rListRegPaths.push_back(sRegPath);

    // These will be 32 bit browsers (on a 64 bit Operating System)
    if (IsOS(OS_WOW6432))
    {
        sRegPath.hRootKey = HKEY_LOCAL_MACHINE;
        sRegPath.strKeyPath = _T("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\App Paths\\firefox.exe");
        sRegPath.strBrowser = _T("Firefox");
        rListRegPaths.push_back(sRegPath);

        sRegPath.hRootKey = HKEY_LOCAL_MACHINE;
        sRegPath.strKeyPath = _T("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\App Paths\\IEXPLORE.EXE");
        sRegPath.strBrowser = _T("Internet Explorer");
        rListRegPaths.push_back(sRegPath);

        sRegPath.hRootKey = HKEY_LOCAL_MACHINE;
        sRegPath.strKeyPath = _T("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe");
        sRegPath.strBrowser = _T("Google Chrome");
        rListRegPaths.push_back(sRegPath);
    }
}

Update

Definition of RegPathlist:

typedef struct tagRegistryPath
{
    HKEY hRootKey;
    CString strBrowser;
    CString strKeyPath;

} S_REGISTRY_PATH;
using RegistryPathList = list<S_REGISTRY_PATH>;

Solution

  • You can use initializer list constructor and push_back:

    struct RegistryPath {
        HKEY hRootKey;
        TCHAR const* strBrowser;
        TCHAR const* strKeyPath;
    };
    
    int main() {
        std::list<RegistryPath> sRegPath = {
            {HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\firefox.exe"), _T("Firefox")},
            {HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\IEXPLORE.EXE"), _T("Internet Explorer")}
            // ...
        };
        if(IsOS(OS_WOW6432)) {
            sRegPath.push_back({HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\firefox.exe"), _T("Firefox")});
            // ...
        }
    }
    

    Note that I replaced CString with TCHAR const* to avoid memory allocations and copying the strings.