Talking about the role of #define pre-compilation instructions in J2ME project management

xiaoxiao2021-04-03  229

Talking about the role of #define pre-compilation instructions in J2ME project management

Author: Yu for

(This article is original, repost, please indicate the source: http://blog.9cbs.net/kyosukeno1)

#define is not a fresh thing for C programmers, most of the programmers will even look out: Isn't it macro? Yes, # define can act as a macro, reduce the number of lines of code and the readability of increasing code. However, the use of # define is not only like this: After mixing the precompiled instruction of #define and # idef, # Endif, it will greatly enhance the flexibility of the project.

Before entering the discussion, let's take a look at the list of relevant instructions and their respective roles:

#define DEF_A - Defines DEF_A to exist (but there is no specific value)

#define DEF_A 22 - Defines DEF_A to make it worth 22

#undef def_a - Cancel Def_a definition

#if DEF_A - Compile the following code if DEF_A is satisfied.

#ifdef DEF_A - is equivalent to #if defined DEF_A. If DEF_A is defined, compile the following code.

#ifndef dev_a - If DEF_A is not defined, compile the following code.

#if Defined DEF_A | Defined DEF_B | Defined DEF_C

- If you must def_a or DEF_B or DEF_C, compile the following code.

#ELIF DEF_B - If DEF_B is defined, compile the following code.

#ELSE - If the above IF statement is not satisfied, compile the following code.

#endif - End of a definition block

Speaking here, some people may have an objection: #define instructions cannot be used in Java. Yes, Java compiler does not use #define, but we only need to connect other external precompiled procedures with the current project, so you can enjoy the advantage of C , while compiling and debug Java. This magical external precompiled program is CL.EXE, which comes with this program in any version of Visual Studio. Since the use of this procedure is not discussed in this article, the reader can study the program by itself, here, only give the reference command parameters:

Cl * .java / p / EP / c / NOLOGO / FIMDEFINES.JAVA

This command applies all the .java files in the current directory to the definition of mydefines.java for the precompile, the precompiled output file is the text file of the .i extension, as long as an automatic renamed batch can be output. The file is changed back. Java. In addition, if the reader is dissatisfied with the precompile function of Cl.exe - for example, it will direct the macro to the value of the macro, reduce the readability of the output code - you can try to write your own pre-compiler. If possible, please give me a copy of your improvement version of Cl.exe :-)

Everything is ready, let us see the role of the above pre-compilation instructions in the actual project:

Application 1. In the corresponding multi-platform project, you need to implement a definition of each project, and you need to adapt to each project in the case of minimal changes - for example, for a variety of different phones, screen size and respective Native APIs are not complete In the same case, if each mobile phone writes a code, it is obvious that time, then we can define the project with #define. First, we can summarize the screen resolution of the phone into a definition, for example:

#define res_176_208 // 176X208 resolution, such as NOKIA 6680 and N70

#define res_240_300 // 240x300 resolution, such as Samsung Z510

#define res_352_416 // 352x416 resolution, such as NOKIA N90

In addition, according to the unique API of each manufacturer, it can be defined:

#define nokia_api

Also, some mobile phones are particularly small, and these models can be defined:

#define low_memory

Now, according to our specific situation, the project is defined, for example:

#if Defined Nokia_6680 | Nokia_n70

#define res_176_208 // 176X208 resolution, such as NOKIA 6680 and N70

#define nokia_api // Nokia unique API, some Sony Ericsson mobile phone also support

#elif defined Samsung_Z510 | Defined Samsung_Z500

#define res_240_300 // 240x300 resolution, such as Samsung Z510, Z500

#define low_memory

#elif defined nokia_n90

#define res_352_416 // 352x416 resolution, such as NOKIA N90

#define nokia_api

#ELSE

#define res_176_208 // 176X208 resolution, such as NOKIA 6680 and N70

#define low_memory

#ENDIF

After the above definition is complete, our project definition is quite simple:

When we need to compile NOKIA_6680, we only need to remove the comment "//" in front of the #define nokia_6680, and annotate the definition of other mobile phone projects, you can get the corresponding engineering settings. E.g:

#define nokia_6680

// # define nokia_n70

// # Define Samsung_Z510

// # define samsung_z500

// # define nokia_n90

Application II. In the program, sometimes you need to insert the corresponding processing statement corresponding to different platforms. For example, a corresponding MIDP2, we sometimes need additional processing to achieve more performance advantages - in the initialization process of the program, we can set the Canvas's Super Class to Nokia's fullcanvas:

#ifdef nokia_api

Public Class Thegame Extends Fullcanvas IMPLEments Runnable {

#ELSE

Public Class thegame Extends Canvas Implements Runnable {#ENDIF

......

}

Or on the mobile phone with only small memory, we need some parties that occupy more memory, you can "When you don't define Low_Memory, the program loads some resources":

#ifndef low_memory

/ / Load more memory here, but it is not something that must exist.

#ENDIF

Application III. In the end of the project, the programmer needs to be very careful to join the code to avoid any new bug, then you can use #define to control your own code segment, if you find that the new code is invalid or generated, you can cancel it immediately. New code:

Add: #DEFINE FIX_BUG_1024 in the project utility

When repairing bugs or add new code segments, use fix_bug_1024 to hide its own new code:

#ifdef fix_bug_1024

// New code

#ENDIF / / #IFDEF fix_bug_1024

When this segment code is invalid or generates new bugs or use unknown, you can comment on the #define fix_bug_1024 in the header file at any time, you can immediately turn the code back to the previous look. But note: In this process, do not upload your test code #ifdef fix_bug_1024, etc. to CVS, etc., so as to avoid confusion and inconvenience of colleagues.

However, although the use of the precompiled instruction allows the code to be flexible, improper use can cause other unnecessary troubles, and even significantly affect the readability of the program. For example, when editing in the IDE, the precompiled part will display the parts that will not be compiled (in contrast, large-scale annotation / ** / code), so that the programmer is easily identified by the programmer. :

#if 0

Public void toString () {

System.out.println ("We Defined 0.");

}

#ELSE

Public void toString () {

System.out.println ("We Didn't Define 0.");

}

#ENDIF

All of the above statements are displayed in a normal code color, however, in fact the first toString function does not be compiled into the final code - if this function is more than 1 screen, the programmer is likely to see Less up # if, # else, # Endif without knowing that the function is not compiled, so that after they waste a lot of time to modify the first toString function, it is found that the information of the output has not changed.

Therefore, please follow the following principles when using the precompilation instruction:

1. Between #IF and the next instruction, including as little code. For example, try not to put the entire function into the pre-compilation instruction, and use the dispersion definition:

Public void render () {

#ifdef nokia_6680

// Render Corresponds to Nokia6680

#elif defined Samsung_Z500

// Render corresponding to Samsung Z500 code

#ENDIF

// Other rendering code

}

Instead of defining two different render functions, Render 6680 and Z500 respectively.

2. Compare, and easily confused, in the #ELSE and #ENDIF, use the notes to the definition of its main #IF statement:

#ifdef def_aa

// DEF_AA related code

#ifdef DEF_CC

// DEF_CC related code

#ifdef DEF_DD / / DEF_DD related code

#ENDIF / / #IFDEF DEF_DD

#ENDIF / / #IFDEF DEF_CC

# odel defined DEF_BB

// DEF_BB related code

#ELSE // #ifdef Def_aa

// Common code

#ndif // #ifDef def_aa

3. For temporary definitions, immediately use the #undef to cancel it immediately after it is not needed.

4. For the definition of mutual conflict, you can use the mutex definition method. For example, after a mobile phone is defined, another mobile phone is defined to exist at the same time (if the two mobile phones have a public property, please define the public attribute into a sub-attribute), then you need to define the mutex to guarantee each Only one mobile phone is defined:

#define nokia_6680

#ifndef nokia_6680

#define nokia_n70

#ENDIF

#if! (Defined Nokia_6680 | Defined Nokia_n70)

#define Samsung_Z510

#ENDIF

Obviously, this definition is not suitable for use in a large number of parallel definitions - the information defined is getting longer - not in the case where the number of elements is less, or a good solution.

In this article, we have roughly learn the engineering management method of multi-platform J2ME projects. But in fact, the real huge project contains massive resources other than the code, such as pictures, text documents, and more, using the #define pre-compilation instruction can only control code and code to the application part of the resource, integration of the project and Control is still helpful. For overall control of large projects, due to the limitations of the space, it will be discussed in the next article. Thank you for your concern, there is any questions, welcome to leave a message.

转载请注明原文地址:https://www.9cbs.com/read-131482.html

New Post(0)