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

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

本文关键字:list 人口 std 我可以      更新时间:2023-10-16

我可以简化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>;

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

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

您可能希望emplace_back元素,这可以加快和"简化"(主观术语(您的代码。例:

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

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

如果你不想使用文件,只需使用初始化列表(参见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")
             },
             {
                 ....
             }
             ....
        });
}

您可以通过使用参数构造来缩短它。

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

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

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

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