Developing Yed objects - Data hiding

If you want to forbid unauthorized direct access to an object member, or if you want to control how the external environment interacts with this member, you must specify an access type for this member. C++ language allows this data hiding through the keywords public, private and protected, availables in class definition, i.e. the interface definition. All pure object oriented programming languages have this possibility, even if they can have different keywords explicating this concept.

An object member with public access type can be directly read and written through an instance of this object: therefore, there is no difference between a public member in C++ environment and a structure element in C environment, in terms of data accessibility.

An object member with private access type cannot be directly read and written, but it can be manipulated through one or more object methods. This is not possibile in C environment; or, better, this is not possible in C environment using only his syntax and his base keywords: in this domain, all structure members are public.

Now is the time to introducing the base header file of Yed technology: a very simple C header file, that defines a few macros ( the new keywords... ) that allow member hiding inside structure, using a little trick. This header file is called yedstd.h and it looks like this:

/*************************************************
NOME : YEDSTD.H
*************************************************/


#ifndef YEDSTD
#define YEDSTD

/****************
MACRO DEFINITION
****************/

#define PUBLIC
#define PRIVATE(x) __P##x

#define PRIVATE_FUNCTION static

/**** DYNAMIC CONSTRUCTOR AND DESTRUCTOR ****/

#define New(x) (x *)_##x()
#define Delete(x,a) _D##x(a)

/****************/

#endif

The first macros we explain in detail are the macros in red color: these are the macro that allow data hiding inside structure. Let's see how.

C preprocessor substitutes PUBLIC macro with nothing: this macro is here only for clearness in interface definition, because all structure elements default to public. Our example becomes now:

// C ENVIRONMENT                     |// C++ ENVIRONMENT
#include "yedstd.h"                  |
                                     |
typedef struct{                      |class Myclass {
                                     |public:
PUBLIC char my_attr;                 |    char my_attr;
PUBLIC int (*My_Func)(void *);       |    int My_Func(void *);
                                     |
} Myclass;                           |};
                                     |

When C preprocessor parses this source code, it simply replaces PUBLIC with nothing: my_attr and My_Func object members remain unchanged.

PRIVATE(x) macro carries out data hiding. How? It simply changes object member name 'x' with the name '__Px', because the token '##' means concatenation of strings in preprocessing of C source code. In this way, it's impossible to directly access member with name 'x', simply because it does not exist.
PRIVATE macro acts only as builder of proper private attribute name; so if you want to hide a buffer like

int sibValue[1024];

you can hide it in these ways

int PRIVATE(sibValue)[1024];
int PRIVATE(sibValue[1024]);

and can access a single element in buffer in these ways:

int a=this->PRIVATE(sibValue)[10];
int a=this->PRIVATE(sibValue[10]);

Let's introduce a private member in our example:

// C ENVIRONMENT                     |// C++ ENVIRONMENT
#include "yedstd.h"                  |
                                     |
typedef struct{                      |class Myclass {
                                     |public:
PUBLIC char my_attr;                 |    char my_attr;
PUBLIC int (*My_Func)(void *);       |    int My_Func(void *);
                                     |
char PRIVATE(protected);             |private:       
                                     |    char protected;
                                     |
                                     |
} Myclass;                           |};
                                     |

When C preprocessor parses this code, it replaces char PRIVATE(protected) macro with char __Pprotected. Now, if you try to directly access to protected attribute inside your source code through an instance of Myclass, the C compiler will clearly terminate with an error: you can easily avoid this, but why are you using this technology if you want to normally access C structure members ? The use of PRIVATE macro allows to cleary identify all data in our pseudo-class that must be considered as private members not directly accessibles. If you want to access to them bypassing the PRIVATE concept, consider to use a normal struct in a normal C environment.

The PRIVATE macro is also used inside methods for accessing private members of the instance. In "Writing methods" paragraph we will see how to use it.


http://yed.sourceforge.net