CODE_STYLE.md
Filament largely uses Android's code style, which is significantly different from the Google code style and is derived from the Java code style, but not quite.
The guiding principles of the filament code style and code formatting can be resumed as:
{ at the end of the line;.cpp or .h file must be an empty linefor (int i = 0; i < max; i++) {
}
class Foo {
public:
protected:
private:
};
.h extension.cpp extension.inc extension#include must use fully qualified names#include < > for all public (exported) headers#include " " for private headersinclude foldersrc foldertest folderfoo library must live in a folder named foolibfoo.so
include/foo/FooBar.h
src/FooBar.cpp
src/data.inc
#include <foo/FooBar.h>
#include "FooBarPrivate.h"
constants are uppercase and don't have a prefixglobal variables prefixed with gstatic variables prefixed with sprivate and protected class attributes prefixed with mstatic class attributes prefixed with spublic class attributes are not prefixedextern int gGlobalWarming;
class FooBar {
public:
FooBar(int attributeName, int sizeInBytes)
: mAttributeName(attributeName),
sizeInBytes(sizeInBytes) {}
void reallyLongMethodNameWithLotsOfArguments(bool argument1,
int someSecondArgument, int bestArgument) {
std::pair<bool, int> pair = {
argument1,
argument2,
};
// etc
}
int sizeInBytes;
private:
int mAttributeName;
static int sGlobalAttribute;
static constexpr int FOO_COUNT = 10;
enum {
ONE, TWO, THREE
};
};
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
.cpp file.Headers must be grouped and separated by exactly one blank line in the following order (top to bottom):
#include "details/Bar.h" (the implementation's own header).#include <utils/unwindows.h> (always second to undefine Windows macros, only if needed).#include "PrivateStuff.h" (using " " syntax only for truly private headers located under the local module's src/ directory or its subdirectories).#include <filament/Box.h>, #include <private/filament/EngineEnums.h>, #include <filaflat/MaterialChunk.h>, #include <backend/Handle.h> (all public and private includes for other proprietary libraries must use < > brackets syntax. The layering priority order of these libraries is dynamically computed based on the CMakeLists.txt target linkage graph from most dependent to least dependent).#include <tsl/robin_map.h>, #include <jni.h> (known third-party and system includes).#include <algorithm> (standard C++ templates and <c...> wrappers like <cstdint>, <cstddef>).#include <unistd.h> (C POSIX system headers and <sys/...> headers, placed below C++ standard but above legacy C standard).#include <stddef.h> (legacy system headers ending in .h).To maximize readability for local quoted includes:
"Allocators.h") are sorted first. Files nested inside subdirectories (e.g., "components/Foo.h") are sorted second.Sorting the headers is important to help catching missing #include directives.
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Bar.cpp
// 0. Corresponding header
#include <foo/Bar.h>
// 0.5. Windows Cleanup
#include <utils/unwindows.h>
// 1. Private / local headers (grouped by subdirectory prefix, flat first)
#include "Allocators.h"
#include "components/LightManager.h"
#include "components/RenderableManager.h"
#include "details/Engine.h"
#include "details/Skybox.h"
// 2. Internal library headers (sorted by topological dependencies: filaflat above backend)
#include <private/filament/EngineEnums.h>
#include <filament/Box.h>
#include <filaflat/MaterialChunk.h>
#include <backend/Handle.h>
#include <private/utils/Tracing.h>
#include <utils/Allocator.h>
#include <math/vec3.h>
// 3. Third-party
#include <tsl/robin_map.h>
// 4. C++ Standard Library
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <utility>
// 5. POSIX / System headers
#include <unistd.h>
#include <sys/stat.h>
// 6. C Standard Library
#include <stddef.h>
#include <stdint.h>
An official script is available in the repository to automatically format and sort C++ include blocks in accordance with these rules:
./tools/reorganize_headers.py <file_or_directory>
To see what changes would be made without modifying the files, use the --dry-run option:
./tools/reorganize_headers.py --dry-run <file_or_directory>
Note: The tool has safety checks built-in and will automatically skip any files with inline preprocessor conditionals (#if, #ifdef, etc.) or nested code statements inside their include block to prevent compilation regressions.
STL limited in filament public headers to:
arrayinitializer_listiteratorlimitsoptionaltype_traitsutilityvariantFor libfilament the rule of thumb is that STL headers that don't generate code are allowed (e.g. type_traits),
conversely containers and algorithms are not allowed. There are exceptions such as array. See above for the full list.
STL headers are banned entirely, from public and private headers as well as implementation files:
iostreamstd::string in the Filament core renderer. Prefer utils::CString or std::string_view.std::string in tools, always include the std:: qualifier to disambiguate it
from other string types.auto only when the type appears on the same line or with iterators and lambdas.auto foo = new Foo();
for (auto& i : collection) { }