--- title: Mozilla Framework Based on Templates (MFBT) slug: Mozilla/MFBT translation_of: Mozilla/MFBT ---

The Mozilla Framework Based on Templates ("mfbt") is the central repository for macros, functions, and data structures used throughout Mozilla code, including in the JavaScript engine. Its code resides in the {{ Source("mfbt/") }} source directory, but headers within it should be included using paths like "mozilla/StandardInteger.h". It is fairly new, so its functionality is currently sparse. Feel free to file bugs to add new functionality to it, or to move existing functionality into it, as needed. 

mfbt code goes to some length to document all its interfaces in comments, including examples when possible. It also attempts to define its functionality in well-named files, such that simply skimming the contents of {{ Source("mfbt/") }} will quickly suggest the relevant header to examine. Therefore this document primarily attempts to direct readers to the correct file to read those comments. Feel free to file a documentation bug if you think this approach could be improved, and feel free to make improvements to this document if you see them.

Functionality

Types and type manipulation

{{ Source("mfbt/StandardInteger.h", "StandardInteger.h") }} implements the <stdint.h> interface. (The <stdint.h> standard header is not available on all platforms and so cannot be used directly. This header also provides a useful "hook" for embeddings that must customize the types underlying the fixed-size integer types.)

{{ Source("mfbt/Types.h", "Types.h") }} includes StandardInteger.h and further provides size_t.

{{ Source("mfbt/CheckedInt.h", "CheckedInt.h") }} implements checked integers. They behave like integers, but safely check for integer overflow and divide-by-zero. Useful for input validation.

{{ Source("mfbt/FloatingPoint.h", "FloatingPoint.h") }} provides various operations for examining and working upon double-precision floating point values, and for producing various special floating point values.

Core

{{ Source("mfbt/Types.h", "Types.h") }} further provides macros to define imported and exported C symbols.

{{ Source("mfbt/Attributes.h", "Attributes.h") }} implements various function and class attribute macros. The function macros control inlining, note whether a function returns, and enforce various C++-related restrictions on inheritance and use. The class macros permit controlling the inheritability of a class.

{{ Source("mfbt/Likely.h", "Likely.h") }} provides MOZ_LIKELY and MOZ_UNLIKELY macros to annotate conditions with their expected truthiness.

{{ Source("mfbt/Util.h", "Util.h") }} implements various other bits of useful functionality. (This header will likely be further split up so that its functionality is less grab-bag.)

Debugging

{{ Source("mfbt/Assertions.h", "Assertions.h") }} provides assertion macros in implementing runtime assertions and compile-time assertions.

{{ Source("mfbt/GuardObjects.h", "GuardObjects.h") }} provides macros which can be used to annotate an RAII-style guard class, so that any attempt to create an unnamed temporary for it will assert. (An unnamed temporary lives for a shorter period of time than the scope where it's found, so it usually isn't what was desired.)

Data structures

{{ Source("mfbt/LinkedList.h", "LinkedList.h") }} implements a type-safe doubly-linked list class. Most new code should use this rather than {{ Source("nsprpub/pr/include/prclist.h", "PRCList") }}.

{{ Source("mfbt/RangedPtr.h", "RangedPtr.h") }} implements RangedPtr, a smart pointer template whose value may be manipulated only within a range specified at construction time, and which may be dereferenced only at valid locations in that range.  This pointer is a useful way to expose access to values within an array.

{{ Source("mfbt/RefPtr.h", "RefPtr.h") }} implements various smart pointer templates to simplify reference counting of values of particular classes.

{{ Source("mfbt/Scoped.h", "Scoped.h") }} implements scope-based resource management, to simplify the task of cleaning up resources when leaving a scope.

{{ Source("mfbt/ThreadLocal.h", "ThreadLocal.h") }} implements thread-local storage, aka "TLS", also called thread-specific storage. It should be used only for static-storage-duration variables, such as global variables or static class members.