Skip to content
/ ctl Public

The C-template library: A simple library to enable generic programming via a pseudo-template style interface for the C language.

License

Notifications You must be signed in to change notification settings

nmakes/ctl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The C-Template Library

Version: 0.0.1

Overview

C++ uses templates to produce type definitions at compile time. This enables generic programming while still maintaining the robustness of a strongly typed language.

But, C doesn't support templates. How does one solve it for C?

Enter: ctemplate.

The idea is to use the power of void* pointers to develop an interface which acts like a generic data type. Standard data structure and algorithm implementations will exploit the ctemplate interface, while the user will provide elementary functions such as a comparator and an assignment function.

Demo

Let's demonstrate by example. Suppose we have an integer array as follows:

#include <ctl.c>

int arr[] = {9, 3, 7, 5, 6, 4, 8, 2}; // the user wants to sort an integer array
int length = 8; // defines the length of the array

We would like to sort the array above without writing a custom sort function. We now use ctemplates to leave the sorting task to the library. First, we must define the compare and assign functions knowing that we perform sorting on an int array.

int compare(ctemplate left, ctemplate right)
{
	// we will compare integer values to sort an int array
	return ( *(int*) left.ptr - *(int*) right.ptr );
}

void assign(ctemplate left, ctemplate right)
{
	// we know that the data is of type int
	*(int*) left.ptr = *(int*) right.ptr;
}

The sort function call is simply called by passing a ctemplate wrapper for the integer array. We specify both the base address and the size of each element in the array.

// Calling the following lines, sorts the array.

ctemplate wrapper = {arr, sizeof(int)};
ctl_merge_sort( wrapper, length, compare, assign );

Getting Started

  1. Download the repository and copy the library your local project folder (say, ./myproject).
git clone https://github.com/nmakes/ctl
cp -r ctl/ctlib ./myproject/ctlib
  1. Import ctl and get started
#include "ctlib/ctl.c"

Run sample code

Sample demonstrations are available in files named demo_*. To execute them,

cd ctl
gcc demo_sort.c
./a.out

About

The C-template library: A simple library to enable generic programming via a pseudo-template style interface for the C language.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages