xref: /trunk/main/sal/inc/sal/main.h (revision 2de5e723)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _SAL_MAIN_H_
25 #define _SAL_MAIN_H_
26 
27 #include <sal/types.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 void SAL_CALL sal_detail_initialize(int argc, char ** argv);
34 void SAL_CALL sal_detail_deinitialize();
35 
36 #define SAL_MAIN_WITH_ARGS_IMPL \
37 int SAL_CALL main(int argc, char ** argv) \
38 { \
39 	int ret; \
40 	sal_detail_initialize(argc, argv);   \
41 	ret = sal_main_with_args(argc, argv); \
42 	sal_detail_deinitialize(); \
43 	return ret; \
44 }
45 
46 #define SAL_MAIN_IMPL \
47 int SAL_CALL main(int argc, char ** argv) \
48 { \
49 	int ret; \
50 	sal_detail_initialize(argc, argv); \
51 	ret = sal_main(); \
52 	sal_detail_deinitialize(); \
53 	return ret; \
54 }
55 
56 
57 /* Definition macros for CRT entries */
58 
59 #ifdef SAL_W32
60 
61 #ifndef INCLUDED_STDLIB_H
62 #include <stdlib.h>
63 #define INCLUDED_STDLIB_H
64 #endif
65 
66 /* Sorry but this is necessary cause HINSTANCE is a typedef that differs (C++ causes an error) */
67 
68 #if 0
69 
70 #ifndef _WINDOWS_
71 #include <windows.h>
72 #endif
73 
74 #else /* Simulated what windows.h does */
75 
76 #ifndef WINAPI
77 #	define WINAPI	__stdcall
78 #endif
79 
80 #if !defined(DECLARE_HANDLE)
81 #	ifdef STRICT
82 		typedef void *HANDLE;
83 #		define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name
84 #	else
85 		typedef void *PVOID;
86 		typedef PVOID HANDLE;
87 #		define DECLARE_HANDLE(name) typedef HANDLE name
88 #	endif
89 DECLARE_HANDLE(HINSTANCE);
90 #endif
91 
92 #endif
93 
94 #define SAL_WIN_WinMain \
95 int WINAPI WinMain( HINSTANCE _hinst, HINSTANCE _dummy, char* _cmdline, int _nshow ) \
96 { \
97 	int argc = __argc; char ** argv = __argv; \
98     (void) _hinst; (void) _dummy; (void) _cmdline; (void) _nshow; /* unused */ \
99 	return main(argc, argv); \
100 }
101 
102 #else	/* ! SAL_W32 */
103 
104 # define SAL_WIN_WinMain
105 
106 #endif /* ! SAL_W32 */
107 
108 /* Implementation macro */
109 
110 #define SAL_IMPLEMENT_MAIN_WITH_ARGS(_argc_, _argv_) \
111     static int  SAL_CALL sal_main_with_args (int _argc_, char ** _argv_); \
112     SAL_MAIN_WITH_ARGS_IMPL \
113     SAL_WIN_WinMain \
114 	static int SAL_CALL sal_main_with_args(int _argc_, char ** _argv_)
115 
116 #define SAL_IMPLEMENT_MAIN() \
117     static int  SAL_CALL sal_main(void); \
118     SAL_MAIN_IMPL \
119     SAL_WIN_WinMain \
120 	static int SAL_CALL sal_main(void)
121 
122 /*
123 	"How to use" Examples:
124 
125 	#include <sal/main.h>
126 
127 	SAL_IMPLEMENT_MAIN()
128 	{
129 		DoSomething();
130 
131 		return 0;
132 	}
133 
134 	SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
135 	{
136 		DoSomethingWithArgs(argc, argv);
137 
138 		return 0;
139 	}
140 
141 */
142 
143 #ifdef __cplusplus
144 }	/* extern "C" */
145 #endif
146 
147 #endif	/* _SAL_MAIN_H_ */
148 
149