这些将安全数组转换为 std::list 对象的方法是否可以转换为模板函数? [英] Can these methods that convert safe arrays into std::list objects be turned into a template function?

查看:27
本文介绍了这些将安全数组转换为 std::list 对象的方法是否可以转换为模板函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

采用这三个方法,每个方法从安全数组构建不同的 std::list 对象:

Take these three methods that each build distinct std::list objects from safe arrays:

void CMSATools::ConvertSAFEARRAY_DISCUSSIONITEMS(SAFEARRAY* psaDiscussionItems, ListDiscussionItems& rListDiscussionItems)
{
    MSAToolsLibrary::IDiscussionItemPtr* pVals = nullptr;
    HRESULT hr = SafeArrayAccessData(psaDiscussionItems, (void**)&pVals); // direct access to SA memory

    if (SUCCEEDED(hr))
    {
        long lowerBound, upperBound;  // get array bounds
        hr = SafeArrayGetLBound(psaDiscussionItems, 1, &lowerBound);
        if (FAILED(hr))
            throw _com_error(hr);

        hr = SafeArrayGetUBound(psaDiscussionItems, 1, &upperBound);
        if (FAILED(hr))
            throw _com_error(hr);

        rListDiscussionItems.clear();
        long cnt_elements = upperBound - lowerBound + 1;
        for (int i = 0; i < cnt_elements; ++i)  // iterate through returned values
        {
            rListDiscussionItems.push_back(pVals[i]);
        }
        hr = SafeArrayUnaccessData(psaDiscussionItems);
        if (FAILED(hr))
            throw _com_error(hr);
    }
    else
    {
        throw _com_error(hr);
    }

    hr = SafeArrayDestroy(psaDiscussionItems);
    if (FAILED(hr))
        throw _com_error(hr);
}

void CMSATools::ConvertSAFEARRAY_STUDENTITEMS(SAFEARRAY* psaStudentItems, ListStudentItems& rListStudentItems)
{
    MSAToolsLibrary::IStudentItemPtr    *pVals = nullptr;
    HRESULT hr = SafeArrayAccessData(psaStudentItems, (void**)&pVals); // direct access to SA memory

    if (SUCCEEDED(hr))
    {
        long lowerBound, upperBound;  // get array bounds
        hr = SafeArrayGetLBound(psaStudentItems, 1, &lowerBound);
        if (FAILED(hr))
            throw _com_error(hr);

        hr = SafeArrayGetUBound(psaStudentItems, 1, &upperBound);
        if (FAILED(hr))
            throw _com_error(hr);

        rListStudentItems.clear();
        long cnt_elements = upperBound - lowerBound + 1;
        for (int i = 0; i < cnt_elements; ++i)  // iterate through returned values
        {
            rListStudentItems.push_back(pVals[i]);
        }
        hr = SafeArrayUnaccessData(psaStudentItems);
        if (FAILED(hr))
            throw _com_error(hr);
    }
    else
    {
        throw _com_error(hr);
    }

    hr = SafeArrayDestroy(psaStudentItems);
    if (FAILED(hr))
        throw _com_error(hr);
}

void CMSATools::ConvertSAFEARRAY_DUTYHISTORYITEMS(SAFEARRAY* psaHistoryItems, ListDutyHistoryLookupItems& rListHistoryItems)
{
    MSAToolsLibrary::IDutyAssignmentLookupPtr   *pVals = nullptr;
    HRESULT hr = SafeArrayAccessData(psaHistoryItems, (void**)&pVals); // direct access to SA memory

    if (SUCCEEDED(hr))
    {
        long lowerBound, upperBound;  // get array bounds
        hr = SafeArrayGetLBound(psaHistoryItems, 1, &lowerBound);
        if (FAILED(hr))
            throw _com_error(hr);

        hr = SafeArrayGetUBound(psaHistoryItems, 1, &upperBound);
        if (FAILED(hr))
            throw _com_error(hr);

        rListHistoryItems.clear();
        long cnt_elements = upperBound - lowerBound + 1;
        for (int i = 0; i < cnt_elements; ++i)  // iterate through returned values
        {
            rListHistoryItems.push_back(pVals[i]);
        }
        hr = SafeArrayUnaccessData(psaHistoryItems);
        if (FAILED(hr))
            throw _com_error(hr);
    }
    else
    {
        throw _com_error(hr);
    }

    hr = SafeArrayDestroy(psaHistoryItems);
    if (FAILED(hr))
        throw _com_error(hr);
}

它们都可以工作并且功能正常.但它们有很多共同点.可以使用模板"这里?这不是我以前做过的.

They all work and are functional. But they have a lot of common aspects. It is possible to use "templates" here? It is not something I have done before.

我不介意有三种不同的方法,但如果他们可以调用一个通用的模板化方法来转换安全数组,那将使代码维护更容易.

I don't mind having three distinct methods but if they could perhaps call one common templated method that converts the safe array it would make the code maintenance easier.

有意义吗?

推荐答案

试试这个(没编译):

template<typename from, typename to>
void CMSATools::ConvertSAFEARRAY_DUTYHISTORYITEMS(SAFEARRAY* psaItems, to& rItems)
{
  from   *pVals = nullptr;
  HRESULT hr = SafeArrayAccessData(psaItems, (void**)&pVals); // direct access to SA memory

  if (SUCCEEDED(hr))
  {
    long lowerBound, upperBound;  // get array bounds
    hr = SafeArrayGetLBound(psaItems, 1, &lowerBound);
    if (FAILED(hr))
      throw _com_error(hr);

    hr = SafeArrayGetUBound(psaItems, 1, &upperBound);
    if (FAILED(hr))
      throw _com_error(hr);

    rItems.clear();
    long cnt_elements = upperBound - lowerBound + 1;
    for (int i = 0; i < cnt_elements; ++i)  // iterate through returned values
    {
      rItems.push_back(pVals[i]);
    }
    hr = SafeArrayUnaccessData(psaItems);
    if (FAILED(hr))
      throw _com_error(hr);
  }
  else
  {
    throw _com_error(hr);
  }

  hr = SafeArrayDestroy(psaItems);
  if (FAILED(hr))
    throw _com_error(hr);
}

这篇关于这些将安全数组转换为 std::list 对象的方法是否可以转换为模板函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆