I
Immortal Nephi
There are thousands of emulator projects written in C language and
sometimes in assembly language. The programmers always write a
wrapper in the file scope. All member variables and member functions
are located in the file scope, but they are acting to be old-fashioned
global variables and global functions.
The programmers are afraid to write object-oriental programming
because two or three indirections are used to cause overhead on the
CPU so they prefer to use procedural programming instead. They want
critical speed like real time for best performance.
I create a library such as DLL. All global variables are hidden in
the Library.c. Only getter functions are available to the clients.
The library module is designed to have only ONE instance . All global
variables are treated to be one instance. They remain existent from
the beginning of this program throughout to the end.
Please review my source code below. Please let me know if my source
code looks to be the best practice for emulator project. I will
mention multiple instances momentarily.
// Library.h
#ifndef __LIBRARY_H__
#define __LIBRARY_H__
int get_a( void );
int get_b( void );
int get_c( void );
void run( void );
#endif /* __LIBRARY_H__ */
// Library.c
#include "Library.h"
static int a = 0;
static int b = 0;
static int c = 0;
int get_a( void ) { return a; }
int get_b( void ) { return b; }
int get_c( void ) { return c; }
static void modify( void ) { a += 1; b += 2; c += 3; }
void run( void ) { modify(); }
// main.c
#include "Library.h"
int main()
{
run();
int _a = get_a();
int _b = get_b();
int _c = get_c();
return 0;
}
I have another source code below to present multiple instances. The
global variables are no longer to be located in the file scope. They
are now located in the data structure. The data structure is in the
library.h.
The problem is that member variables in the data structure are now
visible to the clients. They are no longer to be protected or
hidden. The clients are able to modify member variables.
The main function has two objects in an array like two instances.
Only object pointer is available in the library.h. The client assigns
first object to the object pointer before they invoke initialize
function and run function.
In other words, they can reassign second object to the same object
pointer. This way, both objects are running at the same time like
multithreading.
Sometimes, programmers prefer to place object pointer in the run
function’s parameter like this run( _obj *p_obj ) when they want to
switch between objects.
Please comment how member variables in the data structure be
protected. Please take a look at my source code below.
// Library.h
#ifndef __LIBRARY_H__
#define __LIBRARY_H__
struct _obj
{
int a;
int b;
int c;
};
extern _obj *p_obj;
int get_a( void );
int get_b( void );
int get_c( void );
void run( void );
void initialize( void );
#endif /* __LIBRARY_H__ */
// Library.c
#include "Library.h"
_obj *p_obj;
int get_a( void ) { return p_obj->a; }
int get_b( void ) { return p_obj->b; }
int get_c( void ) { return p_obj->c; }
static void modify( void ) { p_obj->a += 1; p_obj->b += 2; p_obj->c +=
3; }
void run( void ) { modify(); }
void initialize( void ) { p_obj->a = 0; p_obj->b = 0; p_obj->c = 0; }
// main.c
#include "Library.h"
int main()
{
int _a, _b, _c;
_obj obj[3];
p_obj = &obj[0];
initialize();
run();
_a = get_a();
_b = get_b();
_c = get_c();
p_obj = &obj[1];
initialize();
run();
_a = get_a();
_b = get_b();
_c = get_c();
return 0;
}
sometimes in assembly language. The programmers always write a
wrapper in the file scope. All member variables and member functions
are located in the file scope, but they are acting to be old-fashioned
global variables and global functions.
The programmers are afraid to write object-oriental programming
because two or three indirections are used to cause overhead on the
CPU so they prefer to use procedural programming instead. They want
critical speed like real time for best performance.
I create a library such as DLL. All global variables are hidden in
the Library.c. Only getter functions are available to the clients.
The library module is designed to have only ONE instance . All global
variables are treated to be one instance. They remain existent from
the beginning of this program throughout to the end.
Please review my source code below. Please let me know if my source
code looks to be the best practice for emulator project. I will
mention multiple instances momentarily.
// Library.h
#ifndef __LIBRARY_H__
#define __LIBRARY_H__
int get_a( void );
int get_b( void );
int get_c( void );
void run( void );
#endif /* __LIBRARY_H__ */
// Library.c
#include "Library.h"
static int a = 0;
static int b = 0;
static int c = 0;
int get_a( void ) { return a; }
int get_b( void ) { return b; }
int get_c( void ) { return c; }
static void modify( void ) { a += 1; b += 2; c += 3; }
void run( void ) { modify(); }
// main.c
#include "Library.h"
int main()
{
run();
int _a = get_a();
int _b = get_b();
int _c = get_c();
return 0;
}
I have another source code below to present multiple instances. The
global variables are no longer to be located in the file scope. They
are now located in the data structure. The data structure is in the
library.h.
The problem is that member variables in the data structure are now
visible to the clients. They are no longer to be protected or
hidden. The clients are able to modify member variables.
The main function has two objects in an array like two instances.
Only object pointer is available in the library.h. The client assigns
first object to the object pointer before they invoke initialize
function and run function.
In other words, they can reassign second object to the same object
pointer. This way, both objects are running at the same time like
multithreading.
Sometimes, programmers prefer to place object pointer in the run
function’s parameter like this run( _obj *p_obj ) when they want to
switch between objects.
Please comment how member variables in the data structure be
protected. Please take a look at my source code below.
// Library.h
#ifndef __LIBRARY_H__
#define __LIBRARY_H__
struct _obj
{
int a;
int b;
int c;
};
extern _obj *p_obj;
int get_a( void );
int get_b( void );
int get_c( void );
void run( void );
void initialize( void );
#endif /* __LIBRARY_H__ */
// Library.c
#include "Library.h"
_obj *p_obj;
int get_a( void ) { return p_obj->a; }
int get_b( void ) { return p_obj->b; }
int get_c( void ) { return p_obj->c; }
static void modify( void ) { p_obj->a += 1; p_obj->b += 2; p_obj->c +=
3; }
void run( void ) { modify(); }
void initialize( void ) { p_obj->a = 0; p_obj->b = 0; p_obj->c = 0; }
// main.c
#include "Library.h"
int main()
{
int _a, _b, _c;
_obj obj[3];
p_obj = &obj[0];
initialize();
run();
_a = get_a();
_b = get_b();
_c = get_c();
p_obj = &obj[1];
initialize();
run();
_a = get_a();
_b = get_b();
_c = get_c();
return 0;
}