简体   繁体   English

在 linux 和 windows 中使用 #define 宏 Function 的最佳方法是什么?

[英]What's the best way #define macro Function used in linux and windows?

WindowsAPI functions currently used in the following examples for Linux当前在 Linux 的以下示例中使用的 WindowsAPI 函数

I want to use it as one definition depending on the platform.我想根据平台将其用作一种定义。

example.

#ifdef _WIN32
            ::CopyMemory(dest, readBuffer_ + fileOffset - readBufferOffset_, size);
#elif __linux__
            memcpy(dest, readBuffer_ + fileOffset - readBufferOffset_, size);
#endif

If there are a lot of processing like the above in the code, the readability is reduced.如果代码中有很多像上面这样的处理,可读性就会降低。 So I want to use something like this, but I'm not sure what is a good way to do it.所以我想使用这样的东西,但我不确定什么是好的方法。


#ifdef _WIN32
  #define MEM_COPY CopyMemory
  #define SET_FILE_POINTER SetFilePointer
  #define READ_FILE ReadFile
#elif __linux
  #define MEM_COPY memcpy
  #define SET_FILE_POINTER lseek // parameter sequence not equal to SetFilePointer
  #define READ_FILE read
#endif


int main()
{
  // i wanna use one function (READ_FILE) both in linux and Windows
  READ_FILE(dest, readBuffer_ + fileOffset - readBufferOffset_, size);
}

I have an additional question.我还有一个问题。

Windows has many safety functions. Windows 具有多项安全功能。

ex) fopen_s memcpy_s例如)fopen_s memcpy_s

in linux I've seen memcpy used frequently.在 linux 我见过 memcpy 经常使用。 How do you usually judge a standard that guarantees safety?您通常如何判断一个保证安全的标准? (Is it ok to just use it normally? I have the linux manual manpage, I saw the note contents.) (正常使用可以吗?我有linux手册manpage,看到note内容了。)

I would use an abstraction layer for that in a namespace:我会在命名空间中使用抽象层:

namespace OperatingSystemFunctions
{
    static void CopyMemory(arguments)
    {
#ifdef _WIN32
            ::CopyMemory(dest, readBuffer_ + fileOffset - readBufferOffset_, size);
#elif __linux__
            memcpy(dest, readBuffer_ + fileOffset - readBufferOffset_, size);
#endif    
    }
}



int main()
{
    MyMemoryCopy::CopyMemory(...);
}

Of course the arguments needs to match for that approach.当然,arguments 需要匹配该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM