我可以简化这个 std::list 的人口吗?

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

我可以简化 std::list 代码的填充吗:

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);
    }
}

更新

RegPathlist的定义:

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

} S_REGISTRY_PATH;
using RegistryPathList = list<S_REGISTRY_PATH>;

您可能想要 emplace_back 您的元素,这可以加速您的代码并 "simplify" (主观术语)。示例:

rListRegPaths.emplace_back(
      HKEY_LOCAL_MACHINE, 
      _T("SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe"),
      _T("Firefox"));

您可以通过带参数的构造使其更短。

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

} S_REGISTRY_PATH;

rListRegPaths.push_back(S_REGISTRY_PATH {
    HKEY_LOCAL_MACHINE,
    _T("SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe"),
    _T("Firefox")
});

您可以使用初始化列表构造函数和 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")});
        // ...
    }
}

请注意,我将 CString 替换为 TCHAR const* 以避免内存分配和复制字符串。

您可以使用不同的浏览器创建数组,如下所示:

S_REGISTRY_PATH native[] = { 
    {HKEY_LOCAL_MACHINE, _T("SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe"), _T("Firefox")},
    //...
}

S_REGISTRY_PATH wow64[] = {
   {HKEY_LOCAL_MACHINE, _T("SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe"), _T("Firefox")},
    //...
}

这甚至可以是一个单独的文件,甚至是自动生成的,您只需将其包含在实现该方法的文件中即可。 然后在方法内部,您要做的就是:

void CMeetingScheduleAssistantApp::InitBrowserRegistryLookupList(RegistryPathList& rListRegPaths)
{
    rListRegPaths.clear();

    for (auto && it : native) {
        rListRegPaths.push_back(*it);
    }

    if (IsOS(OS_WOW6432)) {
         for (auto && it : wow64) {
             rListRegPaths.push_back(*it);
         }
    }
}

这会将基本上只是数据的内容与代码本身分开,从而使其更易于阅读、更改和一般管理。

是的,你可以简化它。最好的方法是提供包含此数据的配置文件。

如果您不想使用文件,只需使用初始化列表(参见 std::list::insert 的第 5 版):

void CMeetingScheduleAssistantApp::InitBrowserRegistryLookupList(RegistryPathList& rListRegPaths)
{
    rListRegPaths.insert(rListRegPaths.end(),
         {
             {
                 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")
             },
             {
                 ....
             }
             ....
        });
}