Book Image

D Cookbook

By : Adam Ruppe
Book Image

D Cookbook

By: Adam Ruppe

Overview of this book

Table of Contents (21 chapters)
D Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Writing platform-specific code (versions) and per-client configuration modules


Different platforms often need specialized code. D provides the version statement for this case, which is similar to but cleaner than C's #ifdef directives. In addition to platforms, special code may be needed when building customized versions of an application for a specific client or other special case. While version can do that job, it isn't ideal. So, we'll use another technique: configuration modules.

How to do it…

Writing platform-specific code and writing client-specific code is best done with two different techniques.

Platform-specific code

Perform the following steps:

  1. Write version statements for all supported platforms.

  2. Write else static assert(0, "Unsupported platform"); at the end of the version list.

  3. Don't try to mix partial declarations for different platforms together, it will be more trouble than it is worth.

The code is as follows:

version(Windows)
void writeToFile(HANDLE file, in void[] data) {
  if(...