What is the proper way to encapsulate this kind of functionality? - encapsulation

For example I have a function that basically works this way:
function myfunc(data,type_of_analysis){
if type_of_analysis is "Smith-Jones Method"
return smith_jones(data)
else if type_of_analysis is "Pineapple-Mango Method"
return pineapple_mango(data)
}
Names are made up of course, and imagine that there are several more types of analysis than this. What would be the correct way to restructure myfunc()? Is there a better / more standard way for people to pass in arguments to determine what kind of analysis they want to perform? Or is this something the user would have to look up in a documentation?

Here is an example in C++ which uses a map between enum values and function objects to give a type safe and flexible dispatch framework:
//dummy analysis functions
void smithJonesAnalysis (int data){cout << "Smith";}
void pineappleMangoAnalysis (int data){cout << "Pineapple";}
class Analyzer
{
//different analysis methods
enum class AnalysisMethod {SmithJones, PineappleMango};
//a map from analysis method to a function object
std::map<AnalysisMethod, std::function<void(int)>> m_analysis_map;
AnalysisMethod stringToMethod (std::string method)
{
//some basic string canonicalisation
std::transform(method.begin(), method.end(), method.begin(), ::tolower);
if (method == "smith-jones method")
return AnalysisMethod::SmithJones;
if (method == "pineapple-mango method")
return AnalysisMethod::PineappleMango;
throw std::runtime_error("Invalid analysis method");
}
public:
Analyzer()
{
//register all the different functions here
m_analysis_map[AnalysisMethod::SmithJones] = smithJonesAnalysis;
m_analysis_map[AnalysisMethod::PineappleMango] = pineappleMangoAnalysis;
}
//dispatcher function
void operator() (std::string method, int data)
{
AnalysisMethod am = stringToMethod(method);
m_analysis_map[am](data);
}
};
It is used like so:
Analyzer a;
a("Smith-Jones Method", 0);
a("Pineapple-Mango Method", 0);
Demo
This has a bunch of advantages compared to simple switch statements:
It's easier to add/remove analysis methods
It's way easier to change the type of data which the methods accept
You could have different Analyzers for different areas, templated and specialized, and all you'd need to change would be the registration method.
You could enable/disable analysis methods at runtime in a very clear fashion

Related

Ways to mock C functions for unit-testing?

I've tried multiple sources for solutions to this problem. They all either require modifying the source code, and architecture specific exploit such as writing in a jmp instruction to detour the function, or using a macro and including the c file. The first one is extremely annoying to deal with, the second is usually not possible due to page protections, and the third introduces a lot of problems with linking multiple files containing different mocks and unit test for the same source file. Is there any better method of doing this?
You can user function pointer in your nominal code. You assign them at init with nominal implemetation in your application. In your unit test you can then assign the function pointer to the mock implmentation. Function pointer is a common practice used to implement interface in C.
Here is a gist of how that could be done:
typedef struct {
void (*method) ();
} interface;
void run(itf *interface)
{
itf->method();
}
void methodImpl()
{
printf("nominal code");
}
void methodMock()
{
printf("mock code");
}
void do_run()
{
interface itf;
itf.method = methodImpl;
run(&itf);
}
void test_run()
{
interface itf;
itf.method = methodMock;
run(&itf);
}

C++11, wrapper class for handling different versions of C struct versions

EDIT:
I think the version is known at run-time instead of compile-time so I'm not able to add it as a compile option to the gcc cmd. Which is why I have to support both versions based on whatever version the hardware reports back.
So I'm dealing with firmware where I am required to support multiple definitions for versions of the same C struct. We created our own header file as defined by the interface documentation of a memory controller based on the vendor's C struct definition.
// For simplicity lets pretend that this is the struct for version 1
typedef struct __attribute__((packed)) ver1 {
int x;
int y;
} ver1;
I also have an existing API that uses this interface already that needs to be replaced by some sort of class wrapper (I believe), or a wrapper that plays well with the existing API.
void function_call(ver1 v1);
Only one instance (ver 1 or ver 2) of the struct can exist at any time
ver 1 for a certain fw version, and ver 2 after a certain fw version
ver2 is my extended version of ver1, I am naming it as ver2 for the hope of using some sort of factory to select the right C-style struct.
typedef struct __attribute__((packed)) ver2 {
int x;
int y;
int w; // new
int z; // new
} ver2;
Before creating a ver 2 I was looking into options such as the decorator or adaptor design pattern I could try a fancy CRTP template style I found on Hands-On Design Patterns but for simplicity, I'll illustrate with this scheme where I could possibly "add-on" to ver1:
struct ver2 : public ver1 {
int w;
int z;
}
But then I learned that C++ doesn't guarantee the same class layout
C struct Inheritance vs C++ POD struct Inheritance
and potential alignment issues (I'm not too familiar with it) so I don't think it is a real option for me to use.
I found this example on stackoverflow but I don't like the idea of adding include headers in the struct How to handle conflicting struct definitions in a C application.
There is a similar example here using a similar base class
C++ design for multiple versions of same interface (enumerations / structures in header files) which I don't think I can even use due to inheritance impact on the class layout.
Unless there is a valid reason to use the techniques of the links above, I was considering a wrapper class that returns the right version based on a selector. First I'll define a free function to leverage this.
int get_fw_version(int target);
I'm working on C++11 so I'm limited on auto return type deduction and below is just some draft code I'm trying to think up, not complete, doesn't compile, just illustrating my thought process. I haven't considered composition yet since IDK how that will quite work. Looking for ideas.
int main() {
// Roughly how I would like to use it...
const int fw_ver = get_fw_version(target);
auto ver_inst = ver_factory(fw_ver);
function_call( ver_inst.get_data() );
return 0;
}
I am not sure if I can do this without polymorphism where the base class gets ver1 and but the derived class has ver2.
Rough idea where I am at, I tried doing CRTP but I hit the problem that the base class needs to be a template and I can't use a heterogeneous base type (e.g. shared_ptr). Trying the non-CRTP way IDK how to set up the abstract base class with the get_data() method. Without the compiler complains saying that the base doesn't have a get_data method, which, makes sense
// I can't figure out how to add T get_data() here without adding a template param. This base function is really to delegate common member methods and trying to keep a common base for polymorphism.
class base {
virtual ~base() = 0;
// ?? get_data() = 0 or some other method
};
class ver1_derived : public base
{
ver1 data;
public:
ver1_derived() = default;
ver1 get_data() {
return data;
}
};
class ver2_derived : public base
{
ver2 data;
public:
ver2_derived() = default;
ver2 get_data() {
return data;
}
};
// should be using unique_ptr but I can't at work....
shared_ptr<base> ver_factory(const int fw_ver) {
if(fw_ver <= 1)
return make_shared<ver1_derived>();
return make_shared<ver2_derived>();
}
I ended up giving up on an inheritance schemed and ended up taking two different code paths based on the template type.
So
if(fw_ver <= 1)
function_call<ver1>();
} else {
function_call<ver2>();
}

Achieving Object-Oriented Design in the C Language by using proper function names

I am writing a MISRA compliant code that runs on a microcontroller, and this program must be written in the C language.
I would like to design my software according to the object-oriented design. However, the C-language lacks OOP support.
Therefore, I decided to create "classes" and "packages" through C-files and Folders, respectively.
Currently, all legacy function names were having the following names: <Module_Name>_f_<Function_Name>_<Return type>.
This function naming convention works as long as there is only a single Module. However, if I add SubModules or even SubSubModules, then the function name might get confusing.
For example, having Module, Submodule, and a SubSubModule might end up in one the following function names:
<Module_Name><SubModule_Name><SubSubModule_Name>_f_<Function_Name>_<Return type>
<Module_Name>_<SubModule_Name>_<SubSubModule_Name>_f_<Function_Name>_<Return type>
<Module_Name>_f_<SubModule_Name>_<SubSubModule_Name>_<Function_Name>_<Return type>
...
What would be a good name for such functions, and their respective C-files? I would like to have a naming convention that one can read and still understand the "class"/"package" structure?
To make it more clear, we can take more concrete example with the following file structure containing Folders and C-files:
Module (Folder)
- SubModule_1 (Folder)
- SubSubModule_1_1.c
- SubSubModule_1_2.c
- SubSubSubModule_1_2_1.c (Maybe also put in a seperate Sub-Folder?)
- SubSubSubModule_1_2_2.c (Maybe also put in a seperate Sub-Folder?)
...
- SubModule_n (Folder)
- SubSubModule_n_1.c
- SubSubModule_n_2.c
...
The above file structure might look like this in an OOP pseudocode:
class Module:
begin Module;
# Field Declarations
SubModule_1 subModule_1_Instance;
SubModule_2 subModule_2_Instance;
...
# Function declarations
Module_f_<Function_Name>_<return type>;
...
end Module;
class SubModule_1:
begin SubModule_1;
# Field Declarations
SubSubModule_1_1 subSubModule_1_1_Instance;
SubSubModule_1_2 subSubModule_1_2_Instance;
...
# Function declarations
ModuleSubModule1_f_<Function_Name>_<return type>;
OR
Module_SubModule1_f_<Function_Name>_<return type>;
OR
Module_f_SubModule1_f_<Function_Name>_<return type>;
...
end SubModule_1;
class SubSubModule_1_1:
begin SubSubModule_1_1;
# Function declarations
ModuleSubModule1SubModuleSubModule11_f_<Function_Name>_<return type>;
OR
Module_SubModule1_SubModule11_f_<Function_Name>_<return type>;
OR
Module_f_SubModule1_SubModule11__f_<Function_Name>_<return type>;
...
end SubSubModule_1_1;
So for the SubSubModule_1_1, I might end up with:
ModuleSubModule1SubModuleSubModule11_f_<Function_Name>_<return type>;
Module_SubModule1_SubModule11_f_<Function_Name>_<return type>;
Module_f_SubModule1_SubModule11__f_<Function_Name>_<return type>;
Is there maybe a better way to name those functions? I am looking forward to Your replays/alternatives.
Thank you in advance.
Sticking to an OO design is almost always a good idea, but you need to boil down OO to the things that matter. Namely:
Autonomous objects that only know of their designated purpose and know nothing about unrelated things.
For example in an embedded system, your SPI driver shouldn't and needn't know anything about the LCD you are using, even though you are communicating with the LCD through SPI.
Private encapsulation that hides information away to reduce complexity, tight coupling and namespace collisions.
In some cases, inheritance.
For example if you are writing a portable HAL that should function the same no matter the underlying microcontroller hardware. (Like for example a SPI driver.)
All of the above OO can be achieved in C and the language directly or indirectly has language support for it. There's misc other concepts like "RAII", which are handy but not necessary. Unfortunately we can't get automatically called constructors/destructors in C, so we have to live with calling them explicitly.
The main thing to concider when doing OO in C (and other languages) is to do it on a file level. The header file should contain the public interface - everything that the caller needs to know, that you would normally have declared public in a language with keyword support. Each header file contains a corresponding .c file containing the private implementation details.
It's a good idea to have a strict naming policy like in your examples, so that the caller knows where a certain function belongs. The functions belonging to the SPI driver spi.h should be named spi_init, spi_transceive and so on, with the source code prefix first.
Not sure if I like the SubSubModule idea though, seems a bit burdensome. Also, in an embedded system there should be just so many cases where you actually need inheritance, it is a bit of a rare beast rather than the main attraction in most programs. Often it can rather be a sign of poor design and over-engineering with far too many abstraction layers. It's also important to never let your inheritance API be set in stone. Don't hesitate to change it later on, when you discover new requirements that weren't considered during the initial design.
Regarding private encapsulation, C supports that through the static keyword. Functions declared static in the .c file are truly private and can't be accessed from other files. It doesn't work quite as well for variables though. You can use static file scope variables as a "poor man's private", that's in fact how it is done most of the time in embedded systems. static variables have some limitations though: they force the object to become a "singleton pattern" with only one instance possible. Which is fine if you only need one instance of the SPI driver, but what if the MCU comes with 5 different SPI peripherals, all behaving identically?
As a side note, static variables aren't thread-safe in larger, multi-process/multi-thread programs. Could become relevant in case of RTOS.
It is however possible to take OO one step further in C, by using the concept known as opaque type / opaque pointers. Examples. This allows you to create multi-instance classes, fully encapsulated, or optionally with some public parts. It can be used to model inheritance and polymorphism, by letting the first object of the inherited class contain a struct instance of its parent. Function pointers enable "virtual" inherited functions, where calling a function through a base class pointer invokes the corresponding function in the caller.
An object declared as opaque through pointers to incomplete type cannot be allocated by the caller, they can only declare pointers to them. From the caller's perspective they work essentially just the same as abstract base classes in C++. You will have to encapsulate the object allocation inside the init function (constructor). This is a bit of a disadvantage in low-end embedded systems, since sanity demands that we don't use malloc there. Instead memory allocation will have to be done through a fixed maximum size static memory pool. Examples: Static allocation of opaque data types
From a MISRA-C perspective, they actually encourage the use of opaque type since MISRA-C:2012 (Dir 4.8).
Do not over-use opaque type though. It makes perfect sense for things like HAL on top of drivers, portable code, protocol handling etc. But not so much for hiding away non-portable, application-specific logic, which doesn't benefit from abstraction layers since you won't be able to re-use or port it anyway.
Overall, program design is highly qualified work. It takes lots of experience to get it done properly. Add too much abstraction and you end up in over-engineered, meta-programming hell. Add too little and you end up in spaghetti-programming, tight-coupling hell.
The concept missing from this discussion is the "this" pointer to have instance-specific data.
It's implicit in C++, but must be explicit in C.
For example, in a hypothetical module NSMotionController.c:
typedef struct NSMotionControllerStruct {
float speed__m_s;
} NSMotionController_t;
float NSMotionController_SpeedGet__m_s(NSMotionController_t const * const this) {
return this->speed__m_s;
}
bool NSMotionController_Initialize(NSMotionController_t * const this, float const speedCurrent__m_s) {
this->speed__m_s = speedCurrent__m_s;
return true;
}
We can use this like so:
int main(int argc, char ** argv) {
NSMotionController_t motionControllerInstance1;
NSMotionController_Initialize(motionControllerInstance1, 1.0f);
NSMotionController_t motionControllerInstance2;
NSMotionController_Initialize(motionControllerInstance1, 2.0f);
printf("speed1: %.1f\n", NSMotionController_SpeedGet__m_s(&motionControllerInstance1));
printf("speed2: %.1f\n", NSMotionController_SpeedGet__m_s(&motionControllerInstance2));
}
As far as naming, I use a two-letter namespace ("NS" above) since C doesn't support namespaces idiomatically.
I use the module name, then an underscore to start the method name.
I use two underscores to separate a units suffix ("__m_s" above indicates "meters per second").
For polymorphism, you can use function pointers. So, augmenting our example with function pointers:
typedef float (*NSMotionControllerInterface_SpeedGet__m_s_t)(void const * const this);
typedef struct NSMotionControllerStruct {
NSMotionControllerInterface_SpeedGet__m_s_t SpeedGet__m_s;
float speed__m_s;
} NSMotionController_t;
float NSMotionController_SpeedGet__m_s(void const * const this) {
NSMotionController_t const * const motionThis = (NSMotionController_t const *) this;
return motionThis->speed__m_s;
}
bool NSMotionController_Initialize(NSMotionController_t * const this, float const speedCurrent__m_s) {
this->SpeedGet__m_s = NSMotionController_SpeedGet__m_s;
this->speed__m_s = speedCurrent__m_s;
return true;
}
int main(int argc, char ** argv) {
NSMotionController_t motionControllerInstance1;
NSMotionController_Initialize(motionControllerInstance1, 1.0f);
NSMotionController_t motionControllerInstance2;
NSMotionController_Initialize(motionControllerInstance1, 2.0f);
printf("speed1: %.1f\n", motionControllerInstance1.SpeedGet__m_s(&motionControllerInstance1));
printf("speed2: %.1f\n", motionControllerInstance2.SpeedGet__m_s(&motionControllerInstance2));
}
Rather than using polymorphism on a single function, though, you can gather them up in a struct and pass that to other modules.
typedef float (*NSMotionControllerInterface_SpeedGet__m_s_t)(void const * const this);
typedef bool (*NSMotionControllerInterface_SpeedSet__m_s_t)(void const * const this, float const speedNew__m_s);
typedef struct NSMotionControllerInterfaceStruct {
NSMotionControllerInterface_SpeedGet__m_s_t SpeedGet__m_s;
NSMotionControllerInterface_SpeedSet__m_s_t SpeedSet__m_s;
} NSMotionControllerInterface_t;
typedef struct NSMotionControllerStruct {
NSMotionControllerInterface_t interface;
float speed__m_s;
} NSMotionController_t;
float NSMotionController_SpeedGet__m_s(void const * const this) {
NSMotionController_t const * const motionThis = (NSMotionController_t const *) this;
return motionThis->speed__m_s;
}
bool NSMotionController_SpeedSet__m_s(void const * const this, float const speedNew__m_s) {
NSMotionController_t const * const motionThis = (NSMotionController_t const *) this;
motionThis->speed__m_s = speedNew__m_s;
return true;
}
bool NSMotionController_Initialize(NSMotionController_t * const this, float const speedCurrent__m_s) {
this->interface.SpeedGet__m_s = NSMotionController_SpeedGet__m_s;
this->interface.SpeedSet__m_s = NSMotionController_SpeedSet__m_s;
this->speed__m_s = speedCurrent__m_s;
return true;
}
int main(int argc, char ** argv) {
NSMotionController_t motionControllerInstance1;
NSMotionController_Initialize(motionControllerInstance1, 1.0f);
NSMotionController_t motionControllerInstance2;
NSMotionController_Initialize(motionControllerInstance1, 2.0f);
NSMotionControllerInterface_t * const interface1 = motionControllerInstance1.interface;
NSMotionControllerInterface_t * const interface2 = motionControllerInstance2.interface;
printf("speed1: %.1f\n", interface1->SpeedGet__m_s(&interface1));
printf("speed2: %.1f\n", interface2->SpeedGet__m_s(&interface2));
interface1->SpeedSet__m_s(&interface1, 5.0f);
printf("speed1 (faster): %.1f\n", interface1->SpeedGet__m_s(&interface1));
/* Example of passing abstract interface */
NSGroundControl_t groundControl;
NSGroundControl_Initialize(&groundControl, interface1);
}
In short, never use statics when you can avoid it. This will also help unit testing, which I imagine is next (or hopefully first) if you're working in a MISRA environment.

duktape use common c function for multiple bound js functions

I'm trying to bind a common c function to multiple javascript functions using the duktape engine.
My problem is that i need to find out the name of the calling function inside the common c function. I feel like it's possible to implement this using the often referred javascript stack.
However i have no concrete idea on how to implement it. Does anybody have an idea on how to do this?
Below You can find my mwe. The function duk_get_invoking_function is meant to be pseudo code that emphasizes my intent.
#include "duktape.h"
duk_ret_t common_function(duk_context * ctx) {
const char * function_name;
//
// function_name = duk_get_invoking_function(ctx);
// e.g. function_name is "func_01"
//
return 0;
}
int main() {
duk_context * ctx;
ctx = duk_create_heap_default();
if (!ctx) {
return 1;
}
duk_push_c_function(ctx, common_function, DUK_VARARGS);
duk_put_global_string(ctx, "func_01");
duk_push_c_function(ctx, common_function, DUK_VARARGS);
duk_put_global_string(ctx, "func_02");
duk_push_c_function(ctx, common_function, DUK_VARARGS);
duk_put_global_string(ctx, "func_03");
duk_eval_string(ctx, "func_01('abc'); func_02(123); func_03();");
duk_destroy_heap(ctx);
return 0;
}
If I understood correctly you want multiple Ecmascript function objects to bind to the same native function -- and when that native function is called, figure out which Ecmascript function object was used in the call.
If so, you don't need to look at the call stack to do this: each duk_push_c_function() creates a new Ecmascript function object that points to the same native function. When the native function is called, you can use duk_push_current_function() to get access to the Ecmascript function used in the call. You can then distinguish between the Ecmascript function objects based on e.g. their properties which you set when you create them.

Wrapper function in C using structure of function pointers

I have to write code in C where the user has to have flexibility in choosing any existing DB, write to files, or implement their own storage mechanism. I need wrapper functions that redirect to the right functions corresponding to the storage mechanism selected at runtime or compile time. Say my storage options are FLATFILE and SQLDB and my wrapper function is insert(value). So, if I select FLATFILE as my storage, when I call the wrapper function insert(value), it should in turn call the function that writes to a file. If I choose a SQLDB, insert(value) should call the function that insert the values in the data base.
I know I can somehow use a structure of function pointers to do wrapper functions, but I have no idea how.
Does anyone know of any docs, links, examples, etc I could refer to, to understand and implement something like this? Any pointers will be appreciated. Thanks!
Thanks!
#define BACKEND_FLATFILE 0
#define BACKEND_SQLDB 1
void insert_flatfile(const t_value *v) {
...
}
void insert_sqldb(const t_value *v) {
...
}
void (*insert_functions[]) (const t_value *) = {
insert_flatfile,
insert_sqldb,
};
void insert_wrapper(t_value *v, int backend) {
insert_functions[backend](v);
}
Besides, the different functions for one backend should be stuffed into a struct and you should create an array of such structs instead of one array per wrapper function.
You can use a simple version such as:
struct backend {
int (*insert)(...);
int (*remove)(...);
...
};
static struct backend db_backend = { db_insert, db_remove, ... };
static struct backend other_backend = { other_insert, other_remove, ... };
const struct backend *get_backend(enum backend_type type)
{
switch (type)
{
case DB_BACKEND:
return &db_backend;
case DB_OTHER:
return &db_other;
...
}
}
All of the above can be hidden inside a C file, with get_backend and the enumeration being public. Then you can use it like this:
struct backend *b = get_backend(DB_BACKEND);
b->insert(...);
b->remove(...);
Many details are missing, of course (many people like using typedef, for example). This is a basic setup, you can also create wrapper functions if you don't like the b->insert(...) syntax or if you want to set the back end once and then use insert() and remove() in the code. This is also useful if you already have some code that calls insert() directly and you want to direct the call to the right back end.
If you want a more elaborate solution, have a look at http://www.cs.rit.edu/~ats/books/ooc.pdf. You don't have to implement every last detail from it, but it can give you a few ideas.

Resources