-
-
Save apg360/ad663ef505d46ea0e56d84a561f1a170 to your computer and use it in GitHub Desktop.
Interface in C : An example of using interface types in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef container_of | |
#include <stddef.h> | |
#define container_of(ptr, type, member) ((type *)(((char *)ptr) - offsetof(type, member))) | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "file_logger.h" | |
#include "container_of.h" | |
#include <stdio.h> | |
static int | |
file_logger_log(struct ilogger *ilogger, struct ilogrecord *record) | |
{ | |
struct file_logger *this = container_of(ilogger, struct file_logger, ilogger); | |
if(fprintf(this->file, "%s\n", record->format(record)) < 0) | |
{ | |
return -1; | |
} | |
else | |
{ | |
return 0; | |
} | |
} | |
void | |
file_logger_init(struct file_logger *this, FILE *file) | |
{ | |
this->ilogger.log = file_logger_log; | |
this->file = file; | |
} | |
struct file_logger | |
file_logger_new(FILE *file) | |
{ | |
struct file_logger new; | |
file_logger_init(&new, file); | |
return new; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef FILE_LOGGER_H | |
#define FILE_LOGGER_H | |
#include "ilogger.h" | |
#include <stdio.h> | |
struct file_logger | |
{ | |
struct ilogger ilogger; | |
FILE *file; | |
}; | |
void | |
file_logger_init(struct file_logger *this, FILE *file); | |
struct file_logger | |
file_logger_new(FILE *file); | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef ILOGGER_H | |
#define ILOGGER_H | |
struct ilogrecord | |
{ | |
const char *(*format)(struct ilogrecord *this); | |
}; | |
struct ilogger | |
{ | |
int (*log)(struct ilogger *this, struct ilogrecord *record); | |
}; | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ilogger.h" | |
#include "string_logrecord.h" | |
#include "file_logger.h" | |
int | |
main(void) | |
{ | |
struct file_logger fl = file_logger_new(stdout); | |
struct string_logrecord slr = string_logrecord_new("Hello, World!"); | |
struct ilogger *logger = &fl.ilogger; | |
struct ilogrecord *logrecord = &slr.ilogrecord; | |
logger->log(logger, logrecord); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CFLAGS=-Wall -Wextra -pedantic -ansi -g | |
main: file_logger.o string_logrecord.o | |
main.o: ilogger.h file_logger.h string_logrecord.h | |
file_logger.o: ilogger.h file_logger.h | |
string_logrecord.o: ilogger.h string_logrecord.h | |
#https://stackoverflow.com/questions/6304794/oop-and-interfaces-in-c | |
#You implement interfaces using structs of function pointers. | |
#You can then have the interface struct embedded in your data object struct and pass the interface pointer as first parameter of every interface member function. | |
#In that function you then get the pointer to your container class (which is specific to your implementation) using container_of () macro. | |
#Search for "container_of linux kernel" for an implementation. It is a very useful macro. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "string_logrecord.h" | |
#include "container_of.h" | |
static const char * | |
string_logrecord_format(struct ilogrecord *ilogrecord) | |
{ | |
struct string_logrecord *this = container_of(ilogrecord, struct string_logrecord, ilogrecord); | |
return this->text; | |
} | |
void | |
string_logrecord_init(struct string_logrecord *this, const char *text) | |
{ | |
this->ilogrecord.format = string_logrecord_format; | |
this->text = text; | |
} | |
struct string_logrecord | |
string_logrecord_new(const char *text) | |
{ | |
struct string_logrecord new; | |
string_logrecord_init(&new, text); | |
return new; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef STRING_LOGRECORD_H | |
#define STRING_LOGRECORD_H | |
#include "ilogger.h" | |
struct string_logrecord | |
{ | |
struct ilogrecord ilogrecord; | |
const char *text; | |
}; | |
void | |
string_logrecord_init(struct string_logrecord *this, const char *text); | |
struct string_logrecord | |
string_logrecord_new(const char *text); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment