From e4e676136036c7ce8262f0cab028c2704a5fee55 Mon Sep 17 00:00:00 2001 From: Thomas Walker Lynch Date: Tue, 26 Nov 2024 09:44:20 +0000 Subject: [PATCH] another common doc --- document/RT_C_control_structure.html | 153 +++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 document/RT_C_control_structure.html diff --git a/document/RT_C_control_structure.html b/document/RT_C_control_structure.html new file mode 100644 index 0000000..f7c23d5 --- /dev/null +++ b/document/RT_C_control_structure.html @@ -0,0 +1,153 @@ + + + + + + + RT C coding conventions + + + + +
+
+

Reasoning Technology (RT) C file control structure

+

© 2024 Thomas Walker Lynch - All Rights Reserved.

+
+ +

Introduction

+ +

This document summarizes some of the coding conventions used in RT C projects. Discussed here are conventions for integrated header designs, ad hoc namespaces, and a structured approach to source file extensions. The document also outlines the associated build process using a standardized makefile.

+ +

Header file integration

+ +

RT C projects adopt an innovative approach by integrating headers directly into source files. This ensures consistency between interfaces and implementations, eliminating mismatches. Each file contains both an interface and an implementation section, gated by preprocessor directives.

+ +

Each RT C source file integrates its header directly into the source file. This locality makes header content easier to maintain as everything is found in a single file. It also eliminates the need to maintain two files for each module.

+ +

Each file has two sections

+ + +

Each section is turned on and off with the CPP macro IFACE.

+ +

Example

+

+// If not an IFACE, then an IMPLEMENTATION
+#ifndef IFACE
+  #define MyModule·IMPLEMENTATION
+  // Ensures included files are processed for their interfaces.
+  #define IFACE
+#endif
+
+// Define the interface exactly once.
+#ifndef MyModule·IFACE
+#define MyModule·IFACE
+  // Interface-only includes go here.
+  void MyModule·function();
+#endif
+
+#ifdef MyModule·IMPLEMENTATION
+  // Additional includes for implementation go here.
+  #include 
+  void MyModule·function() {
+    printf("Hello, World!\n");
+  }
+#endif
+  
+ +

Explanation

+

The example above demonstrates the structure and purpose of each block:

+

First block: Ensures that the file operates correctly based on the value of IFACE. If IFACE is undefined, it defines MyModule·IMPLEMENTATION to enable the implementation section and sets IFACE to ensure subsequent includes process interface sections.

+

Second block: Defines the interface, including declarations and interface-specific includes. The #ifndef MyModule·IFACE macro ensures the interface is defined exactly once, regardless of how many times the file is included.

+

Third block: Contains implementation-specific includes and function definitions. Guarded by MyModule·IMPLEMENTATION, it is only included when compiling the implementation.

+

Interface includes are placed in the interface block, ensuring they are available wherever the interface is used. Implementation includes are isolated in the implementation block, minimizing unnecessary dependencies in other files.

+ +

Namespace conventions

+

RT projects use ad hoc namespaces to maintain clarity and prevent naming conflicts. This is achieved by prefixing exported identifiers with a module-specific name followed by the · (cdot) character.

+ +

Conventions

+ + +

Example

+

+void Server·run();
+  
+ +

Source file extensions

+

RT projects use standardized extensions to distinguish between library and command-line interface (CLI) source files:

+ + +

The .lib.c files compile into libraries, while .cli.c files compile into standalone executables. The makefile processes these files automatically, ensuring a clear separation of functionality.

+ +

Build process

+

The build process follows these steps:

+
    +
  1. Dependency generation: Run make dependency to create dependencies. This step is only required when the dependency structure changes.
  2. +
  3. Compilation: Run make cli to compile CLI sources and link them against the library. The makefile automatically manages targets and dependencies.
  4. +
+ +

Benefits

+ + +

Conclusion

+

This document outlines the conventions and practices for writing and building RT C projects. By integrating headers, adopting namespaces, and standardizing extensions, RT ensures its projects are robust, modular, and easy to maintain.

+
+ + -- 2.20.1