[LINUX] Realize interface class in C language

Interface class in C

Last time, when I created Centralized resource management library

--The structure can be encapsulated even in C

I wrote.

At that time, I wrote that it would be interesting because there is no instance method, If you think about it, you can ** realize it normally with a function pointer **. So I made a sample.

4/29 postscript When it is actually convenient to use is [here](https://qiita.com/developer-kikikaikai/items/47b55456ae6e9c287718#c%E8%A8%80%E8%AA%9E%E3% 81% A7% E3% 81% AE% E3% 82% A4% E3% 83% B3% E3% 82% BF% E3% 83% BC% E3% 83% 95% E3% 82% A7% E3% 82% A4% E3% 82% B9% E3% 82% AF% E3% 83% A9% E3% 82% B9% E8% A1% A8% E7% 8F% BE% E3% 81% AE% E6% 9C% 89% I wrote my impressions on E7% 94% A8% E6% 80% A7% E3% 81% AB% E3% 81% A4% E3% 81% 84% E3% 81% A6).

sample

I defined a structure that looks like an interface class as follows. Use the interface to ask the implementation class to answer about your culture.

This time, get_any_people is used to get the implementation class information. I made the code to do an interview using the interface in main.

The argument int * is a list of IDs for getting culture_if.

culture.h



//interface definition
struct culture_if {
 char * (* introduce) (); // Self-introduction
 char * (* get_name) (); // name
 char * (* answer) (int id); // Question
};

int get_any_people(int *peoples_id);

And main looks like this. I have a question for people who gathered using culture_if.

main.c


include <stdio.h>
include "culture.h"
include "flyweight.h"

int main() {
        struct {
                int id;
                char * question;
        } questions[] =
        {
                {FOOD, FOOD_QUESTION},
                {HAPPY, HAPPY_QUESTION},
                {CONPUTER, CONPUTER_QUESTION},
        };

        int max_members[10];
        int i=0;
        int member_num = get_any_people(max_members);

 printf ( "... [interviewer] Hello, everyone today is thank you First lightly you introduce yourself? \ n");

        //interface
        struct culture_if *cultureif;

        for( i = 0 ; i < member_num; i ++ ) {
 printf ("[% dth self-introduction] \ n", i + 1);
                cultureif = (struct culture_if *) flyweight_get(max_members[i]);
 //自己紹介してもらう
                printf("\t%s\n", cultureif->introduce() );
        }

        printf("\n");
 printf ("[Interviewer] Thank you. Then I would like to ask you questions one by one. \ N Feel free to answer. \ N \ n");

        int j=0;
        for ( i = 0 ; i < sizeof(questions)/sizeof(questions[0]); i ++ ) {
                printf("Q %d: %s\n", i+1, questions[i].question);
                for( j = 0 ; j < member_num ; j ++ ) {
 //名前と、質問の回答をもらう
                        cultureif = (struct culture_if *) flyweight_get(max_members[j]);
 //名前と、質問の回答をもらう
 printf ("[A:% s] \ n", cultureif-> get_name ());
                        printf("\t%s\n", cultureif->answer(i));
                }
                printf("\n");
        }

 printf ("[Interviewer] That's all for the question. Thank you everyone! \ N");
        flyweight_exit();
        return 0;
}

flyweight_get isLibrary at the beginningAPI to get an instance of. The entity of the interface implementation class is acquired by specifying the ID. It will be easier to use if you can use the method.

In the interface implementation class, it is OK if you prepare the entity of the structure and set the pointer of the static function.

germany.c



#define GNAME "Schweinsteiger"

static char * gm_introduce() {
        return "German"GNAME"is.";
}

static char * gm_name() {
        return GNAME;
}

static char * gm_answer(int id) {
        static char *answer[]={
                "Beer with potatoes and delicious sausages is enough.",
                "I'm happy. I work to the best of my ability and have a nice family. The best",
                "It's okay so far, have you tested the error cases used?",
        };
        return answer[id];
}

//Set a function pointer in the structure
static void gm_constructor(void *src) {
        struct culture_if *culture_if = (struct culture_if *)src;
        culture_if->introduce = gm_introduce;
        culture_if->get_name = gm_name;
        culture_if->answer = gm_answer;
}

int Germany_new() {
        struct flyweight_init_s method={
                gm_constructor,
                NULL,
                NULL
        };

        //In this, struct culture_malloc if, then gm_constructor is called
        int id = flyweight_register_class(sizeof(struct culture_if), 0, &method);
        return id;
}

~~The source ishere~~ ~~Operating environment: Ubuntu 18.04 Desktop (Ubuntu 14.04 Confirmed operation on Desktop,I think it works on most Linux OS.)~~

2018/07/30 Addendum I've erased the source, I'm sorry.

After that, if you want to use member variables in the implementation class, I thought it would be easier to cut define as follows.

python


#define CULTURE_METHOD \
        char *(*introduce)();\
        char *(*get_name)();\
        char *(*answer)(int id);

##By the way

Obviously, there are people who have carefully organized the realization of object-oriented programming in C like this. I will refer to it.

http://www.sage-p.com/process/cool.htm#7

Recommended Posts

Realize interface class in C language
Machine language embedding in C language
Heapsort made in C language
Multi-instance module test in C language
Segfault with 16 characters in C language
Generate C language from S-expressions in Python
How to multi-process exclusive control in C language
Set up a UDP server in C language
Handle signals in C
Try to make a Python module in C language
Write decorator in class
C language ALDS1_3_B Queue
Access MongoDB in C
Next Python in C
[C language algorithm] Endianness
C API in Python 3
Class notation in Python
Go language to see and remember Part 7 C language in GO language
I tried to illustrate the time and time in C language
[C language algorithm] Block movement
C language ALDS1_4_B Binary Search
C / C ++ programmer challenges Python (class)
Use regular expressions in C
Programming language in "Hello World"
Imitated Python's Numpy in C #
Binary search in Python / C ++
Hello World in GO language
[C language] readdir () vs readdir_r ()
C language ALDS1_4_A Linear Search
Minimum spanning tree in C #
Introduction to Socket API Learned in C Language Part 1 Server Edition
Consider an easy-to-use object-oriented "like" representation in C. -Part 2 Interface override