Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data encapsulation in C

I am currently working on an embedded system and I have a component on a board which appears two times. I would like to have one .c and one .h file for the component.

I have the following code:

typedef struct {
    uint32_t pin_reset;
    uint32_t pin_drdy;
    uint32_t pin_start;
    volatile avr32_spi_t *spi_module;
    uint8_t cs_id;  
} ads1248_options_t;

Those are all hardware settings. I create two instances of this struct (one for each part).

Now I need to keep an array of values in the background. E.g. I can read values from that device every second and I want to keep the last 100 values. I would like this data to be non-accessible from the "outside" of my component (only through special functions in my component).

I am unsure on how to proceed here. Do I really need to make the array part of my struct? What I thought of would be to do the following:

int32_t *adc_values; // <-- Add this to struct

int32_t *adc_value_buffer = malloc(sizeof(int32_t) * 100); // <-- Call in initialize function, this will never be freed on purpose

Yet, I will then be able to access my int32_t pointer from everywhere in my code (also from outside my component) which I do not like.

Is this the only way to do it? Do you know of a better way?

Thanks.

like image 590
Tom L. Avatar asked Mar 18 '15 10:03

Tom L.


People also ask

Is there encapsulation in C?

It is one of the core features of Object-oriented languages. However, it is not only limited to OOP languages only. In C, encapsulation has been despite the absence of private and public keywords. Encapsulation is being used by various other programming languages like C#, C++, PHP, JAVA as well.

What is the data encapsulation?

Data encapsulation, also known as data hiding, is the mechanism whereby the implementation details of a class are kept hidden from the user.

What is data encapsulation and example?

Containers are just one example of encapsulation in coding where data and methods are bundled together into a single package. A key benefits to hiding information about attributes and methods using encapsulation in programming is that it prevents other developers from writing scripts or APIs that use your code.

What is data abstraction and encapsulation in C?

Abstraction is the method of hiding the unwanted information. Whereas encapsulation is a method to hide the data in a single entity or unit along with a method to protect information from outside. 4. We can implement abstraction using abstract class and interfaces.


1 Answers

For the specific case of writing hardware drivers for a microcontroller, which this appears to be, please consider doing like this.

Otherwise, use opaque/incomplete type. You'd be surprised to learn how shockingly few C programmers there are who know how to actually implement 100% private encapsulation of custom types. This is why there's some persistent myth about C lacking the OO feature known as private encapsulation. This myth originates from lack of C knowledge and nothing else.

This is how it goes:

ads1248.h

typedef struct ads1248_options_t ads1248_options_t; // incomplete/opaque type

ads1248_options_t* ads1248_init (parameters); // a "constructor"
void ads1248_destroy (ads1248_options_t* ads); // a "destructor"

ads1248.c

#include "ads1248.h"

struct ads1248_options_t {
    uint32_t pin_reset;
    uint32_t pin_drdy;
    uint32_t pin_start;
    volatile avr32_spi_t *spi_module;
    uint8_t cs_id;  
};

ads1248_options_t* ads1248_init (parameters)
{
  ads1248_options_t* ads = malloc(sizeof(ads1248_options_t));
  // do things with ads based on parameters
  return ads;
}

void ads1248_destroy (ads1248_options_t* ads)
{
  free(ads);
}

main.c

#include "ads1248.h"

int main()
{
  ads1248_options_t* ads = ads1248_init(parameters);
  ...
  ads1248_destroy(ads);
}

Now the code in main cannot access any of the struct members, all members are 100% private. It can only create a pointer to a struct object, not an instance of it. Works exactly like abstract base classes in C++, if you are familiar with that. The only difference is that you'll have to call the init/destroy functions manually, rather than using true constructors/destructors.

like image 82
Lundin Avatar answered Oct 07 '22 07:10

Lundin