Bug 767240 - Make it easier to create type-specific scoped pointers (TYPE_SPECIFIC_SC...
[gecko.git] / mfbt / Types.h
blobf803586ec117d8444bc4461445b4c7b55f1bebf9
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* mfbt foundational types and macros. */
8 #ifndef mozilla_Types_h_
9 #define mozilla_Types_h_
12 * This header must be valid C and C++, includable by code embedding either
13 * SpiderMonkey or Gecko.
17 * Expose all the integer types defined in C99's <stdint.h> (and the integer
18 * limit and constant macros, if compiling C code or if compiling C++ code and
19 * the right __STDC_*_MACRO has been defined for each). These are all usable
20 * throughout mfbt code, and throughout Mozilla code more generally.
22 #include "mozilla/StandardInteger.h"
24 /* Also expose size_t. */
25 #include <stddef.h>
27 /* Implement compiler and linker macros needed for APIs. */
30 * MOZ_EXPORT_API is used to declare and define a method which is externally
31 * visible to users of the current library. It encapsulates various decorations
32 * needed to properly export the method's symbol. MOZ_EXPORT_DATA serves the
33 * same purpose for data.
35 * api.h:
36 * extern MOZ_EXPORT_API(int) MeaningOfLife(void);
37 * extern MOZ_EXPORT_DATA(int) LuggageCombination;
39 * api.c:
40 * MOZ_EXPORT_API(int) MeaningOfLife(void) { return 42; }
41 * MOZ_EXPORT_DATA(int) LuggageCombination = 12345;
43 * If you are merely sharing a method across files, just use plain |extern|.
44 * These macros are designed for use by library interfaces -- not for normal
45 * methods or data used cross-file.
47 #if defined(WIN32) || defined(XP_OS2)
48 # define MOZ_EXPORT_API(type) __declspec(dllexport) type
49 # define MOZ_EXPORT_DATA(type) __declspec(dllexport) type
50 #else /* Unix */
51 # ifdef HAVE_VISIBILITY_ATTRIBUTE
52 # define MOZ_EXTERNAL_VIS __attribute__((visibility("default")))
53 # elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
54 # define MOZ_EXTERNAL_VIS __global
55 # else
56 # define MOZ_EXTERNAL_VIS
57 # endif
58 # define MOZ_EXPORT_API(type) MOZ_EXTERNAL_VIS type
59 # define MOZ_EXPORT_DATA(type) MOZ_EXTERNAL_VIS type
60 #endif
63 * Whereas implementers use MOZ_EXPORT_API and MOZ_EXPORT_DATA to declare and
64 * define library symbols, users use MOZ_IMPORT_API and MOZ_IMPORT_DATA to
65 * access them. Most often the implementer of the library will expose an API
66 * macro which expands to either the export or import version of the macro,
67 * depending upon the compilation mode.
69 #ifdef _WIN32
70 # if defined(__MWERKS__)
71 # define MOZ_IMPORT_API(x) x
72 # else
73 # define MOZ_IMPORT_API(x) __declspec(dllimport) x
74 # endif
75 #elif defined(XP_OS2)
76 # define MOZ_IMPORT_API(x) __declspec(dllimport) x
77 #else
78 # define MOZ_IMPORT_API(x) MOZ_EXPORT_API(x)
79 #endif
81 #if defined(_WIN32) && !defined(__MWERKS__)
82 # define MOZ_IMPORT_DATA(x) __declspec(dllimport) x
83 #elif defined(XP_OS2)
84 # define MOZ_IMPORT_DATA(x) __declspec(dllimport) x
85 #else
86 # define MOZ_IMPORT_DATA(x) MOZ_EXPORT_DATA(x)
87 #endif
90 * Consistent with the above comment, the MFBT_API and MFBT_DATA macros expose
91 * export mfbt declarations when building mfbt, and they expose import mfbt
92 * declarations when using mfbt.
94 #if defined(IMPL_MFBT)
95 # define MFBT_API(type) MOZ_EXPORT_API(type)
96 # define MFBT_DATA(type) MOZ_EXPORT_DATA(type)
97 #else
99 * When mozglue is linked in the program, we need the MFBT API symbols
100 * to be weak.
102 # if defined(MOZ_GLUE_IN_PROGRAM)
103 # define MFBT_API(type) __attribute__((weak)) MOZ_IMPORT_API(type)
104 # define MFBT_DATA(type) __attribute__((weak)) MOZ_IMPORT_DATA(type)
105 # else
106 # define MFBT_API(type) MOZ_IMPORT_API(type)
107 # define MFBT_DATA(type) MOZ_IMPORT_DATA(type)
108 # endif
109 #endif
112 * C symbols in C++ code must be declared immediately within |extern "C"|
113 * blocks. However, in C code, they need not be declared specially. This
114 * difference is abstracted behind the MOZ_BEGIN_EXTERN_C and MOZ_END_EXTERN_C
115 * macros, so that the user need not know whether he is being used in C or C++
116 * code.
118 * MOZ_BEGIN_EXTERN_C
120 * extern MOZ_EXPORT_API(int) MostRandomNumber(void);
121 * ...other declarations...
123 * MOZ_END_EXTERN_C
125 * This said, it is preferable to just use |extern "C"| in C++ header files for
126 * its greater clarity.
128 #ifdef __cplusplus
129 # define MOZ_BEGIN_EXTERN_C extern "C" {
130 # define MOZ_END_EXTERN_C }
131 #else
132 # define MOZ_BEGIN_EXTERN_C
133 # define MOZ_END_EXTERN_C
134 #endif
136 #endif /* mozilla_Types_h_ */