Platform-specific code should always be abstracted and implemented in platform-specific source files in appropriately named subdirectories, for example:
In general, you should avoid adding any uses of
PLATFORM_XBOXONE) to code outside of a directory named [PLATFORM].
Instead, extend the hardware abstraction layer to add a static function, e.g. in FPlatformMisc:
FORCEINLINE static int32 GetMaxPathLength()
Platforms can then override this function, returning either a platform-specific constant value or even using platform APIs to determine the result.
If you force-inline the function it will have the same performance characteristics as using a define.
In cases where a define is absolutely necessary, create new #defines that describe particular properties that can apply to a platform, for example
PLATFORM_USE_PTHREADS. Set the default value in Platform.h and override for any platforms which require it in the platform-specific Platform.h file.
For example, in Platform.h we have:
#define PLATFORM_USE_PTHREADS 1
#define PLATFORM_USE_PTHREADS 0
Cross-platform code can then use the define directly without needing to know the platform.
Reasoning: centralizing the platform-specific details of the engine allow for such details to be contained entirely within platform-specific source files. Doing so makes it easier to maintain the engine across multiple platforms and also to port the code to new platforms without the need to scour the codebase for platform-specific defines.
Keeping platform code in platform-specific folders is also a requirement for NDA platforms such as PS4, XboxOne and Nintendo Switch.
It is important to ensure the code compiles and runs regardless of whether the
[PLATFORM] subdirectory is present. In other words, cross platform code should never be dependent on platform-specific code.