Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to abstract away an init function?

Tags:

c

I am making a low level library that requires initialization to work properly which I implemented with a init function. I am wondering if there is a way to make the init call be called once the user calls a library function ideally without:

  1. Any overhead
  2. No repeated calls
  3. No exposed global variables. (my current solution does this, which I don't quite like)

my current solution as per comment request:

bool isinit = 0;

void init()
{
  isinit = 1;
  // init code
}

void lib_function()
{
  if(!isinit) init();
  // function code
}

The compiler seems to be smart enough (using -0fast on gcc) to not make that comparison each time a lib_function is called, but this still exposes a global variable which I don't like.

like image 392
james ray Avatar asked Dec 13 '21 06:12

james ray


People also ask

What is the use of abstract method in it?

It allows you to create a set of methods that must be created within any child classes built from the abstract class. A class which contains one or more abstract methods is called an abstract class. An abstract method is a method that has a declaration but does not have an implementation.

Why do we need abstract classes in Python?

This capability is especially useful in situations where a third-party is going to provide implementations, such as with plugins, but can also help you when working in a large team or with a large code-base where keeping all classes in your mind is difficult or not possible. By default, Python does not provide abstract classes.

What is an abstract class in Java?

An abstract class can be considered as a blueprint for other classes. It allows you to create a set of methods that must be created within any child classes built from the abstract class. A class which contains one or more abstract methods is called an abstract class.

How do you initialize an object in Python?

The method is useful to do any initialization you want to do with your object. In the above example, a person name Nikhil is created. While creating a person, “Nikhil” is passed as an argument, this argument will be passed to the __init__ method to initialize the object.


Video Answer


2 Answers

Best way to abstract away an init function?

Surely your library has some state. Typically, a library exposes functions that work on a specific structure. Do not use global variables - do not write spaghetti code. Expose the structure that holds the state of your library, and make all functions of your library take a pointer to the structure as an argument. Use a namespace - prepend all exported symbols with a prefix. An init function is just like int lib_init(struct lib_the_struct *t); - it will be self-understandable that users need to initialize the structure with that function before use. For example: fopen(), pthread_create.


Write an init function in your library. Write clear documentation stating, that the user of your library has to call the function once before calling any other function. For example: https://curl.se/libcurl/c/curl_global_init.html .

like image 101
KamilCuk Avatar answered Sep 28 '22 22:09

KamilCuk


If you're happy with a solution that is a common extension rather than part of the C standard, you can mark your init function with the constructor attribute, which ensures it will be called automatically during program initialization (or during shared library load if you eventually end up using that).

like image 35
janneb Avatar answered Sep 28 '22 22:09

janneb