首页 > 解决方案 > 如何在两个不同的单例类或普通类中派生具有不同文件路径的相同函数?

问题描述

我有两个具有相同函数 uploadToFile() 的不同类,它们采用不同的文件路径。

我现在已经使用了单例类并且有不同的对象来访问该函数。

代码如下。

库存清单.cpp

InventoryList* InventoryList::pInventoryList =NULL;
std::once_flag InventoryList::InventoryListInstanceFlag;

InventoryList* InventoryList::getInstance()
{
    std::call_once(InventoryListInstanceFlag, []{
        pInventoryList = new InventoryList();
        pInventoryList->runInternalThread=true;
        pthread_create(&pInventoryList->writer, NULL, (void* (*)(void*))&InventoryList::updateToFile, pInventoryList);
        }
    );
    return pInventoryList;
}

NodeList.cpp

NodeList* NodeList::pNodeList =NULL;
std::once_flag NodeList::NodeListInstanceFlag;

NodeList* NodeList::getInstance()
{
    std::call_once(NodeListInstanceFlag, []{
            pNodeList = new NodeList();
            pNodeList->runInternalThread=true;
            pthread_create(&pNodeList->writer, NULL, (void* (*)(void*))&NodeList::updateToFile, pNodeList);
            }
      );
    return pNodeList;
}

InventoryList::UpdateTofile()

void InventoryList::updateToFile()
{
    while(true)
    {
        if((pInventoryList->writeTImer.GetEnabled() && pInventoryList->writeTImer.IsDone()) ||
            pInventoryList->bailoutTimer.IsDone())
        {
            std::unique_lock<std::recursive_mutex> invlock(pInventoryList->mutex_NodeInvConf);
//#if _DEBUG_LOG
            FILE * pFile;
            std::string conff =  pInventoryList->NodeInvConfiguration.toStyledString();
//           LOG_G("inventory file data %s",conff.c_str());
            pFile = fopen (KV_FOLDER_LOCATION DIR_SEP KV_READABLE_INVENTORYLIST_FILE, "wb");
            fwrite (conff.c_str() , sizeof(char), conff.length(), pFile);
            fclose (pFile);
            sync();
//#endif
            FILE * pFile2;
            pFile2 = fopen (/*KV_FOLDER_LOCATION DIR_SEP KV_INVENTORYLIST_FILE*/SYS_INV_PATH , "wb");
            PrintJSONTree(pInventoryList->NodeInvConfiguration,0,pFile2);
            fclose (pFile2);
            sync();
            pInventoryList->writeTImer.SetEnabled(false);
            LOG_G("Inventory file updated");
            LOGS_INFO("Inventory file updated");
            MessageQueInterface::getInstance()->sendInventory(true);
            pInventoryList->bailoutTimer.Reset();
        }
        sleep(1);
    }
}

NodeList::updateToFile()

void NodeList::updateToFile()
{
    while(true)
    {
        if(pNodeList->writeTImer.GetEnabled() && pNodeList->writeTImer.IsDone())
        {
            std::unique_lock<std::recursive_mutex> panellock(pNodeList->mutex_NodeInvConf);
//#if _DEBUG_LOG
            FILE * pFile;
            std::string conff =  pNodeList->NodeInvConfiguration.toStyledString();
            pFile = fopen (KV_FOLDER_LOCATION DIR_SEP KV_READABLE_NODELIST_FILE , "wb");
            fwrite (conff.c_str() , sizeof(char), conff.length(), pFile);
            fclose (pFile);
            sync();
//#endif
            FILE * pFile2;
            pFile2 = fopen (/*KV_FOLDER_LOCATION DIR_SEP KV_NODELIST_FILE*/SYS_PNL_PATH, "wb");
            PrintJSONTree(pNodeList->NodeInvConfiguration,0,pFile2);
            fclose (pFile2);
            sync();
            pNodeList->writeTImer.SetEnabled(false);
            LOG_G("Nodelist file updated");
            LOGS_INFO("Nodelist file updated");
            MessageQueInterface::getInstance()->sendInventory(false);

        }
        sleep(2);
    }
}

我想在一个基类中编写这两个不同的 updateTofile 并通过传递文件路径派生到两个不同的类。

谁能建议我如何做到这一点?

谢谢

标签: c++

解决方案


C++ 作为自由函数的概念。所以在大多数情况下,你不想使用继承而是创建自由函数。

updateToFile绝对是一个免费功能的候选人:

void write_to_file(NodeList* pNodeList)
{
    while(true)
    {
        if(pNodeList->writeTImer.GetEnabled() && pNodeList->writeTImer.IsDone())
        {
            std::unique_lock<std::recursive_mutex> panellock(pNodeList->mutex_NodeInvConf);
//#if _DEBUG_LOG
            FILE * pFile;
            std::string conff =  pNodeList->NodeInvConfiguration.toStyledString();
            pFile = fopen (KV_FOLDER_LOCATION DIR_SEP KV_READABLE_NODELIST_FILE , "wb");
            fwrite (conff.c_str() , sizeof(char), conff.length(), pFile);
            fclose (pFile);
            sync();
//#endif
            FILE * pFile2;
            pFile2 = fopen (/*KV_FOLDER_LOCATION DIR_SEP KV_NODELIST_FILE*/SYS_PNL_PATH, "wb");
            PrintJSONTree(pNodeList->NodeInvConfiguration,0,pFile2);
            fclose (pFile2);
            sync();
            pNodeList->writeTImer.SetEnabled(false);
            LOG_G("Nodelist file updated");
            LOGS_INFO("Nodelist file updated");
            MessageQueInterface::getInstance()->sendInventory(false);

        }
        sleep(2);
    }
}

然后在您的成员函数中使用它:

void NodeList::updateToFile()
{
   write_to_file(pNodeList)
}

或者根本不使用成员函数。

如果您查看 STD 中的类,您会发现它们将继承和成员函数限制在绝对最小值,并尽可能使用自由函数。


推荐阅读