forked from facebook/yoga
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C++ Cleanup 9/N: YGAssert (facebook#1353)
Summary: X-link: facebook/react-native#39201 Pull Request resolved: facebook#1353 ## This diff This moves and renames `YGAssert`, and removes it from the public API, since external users should not need to call into internal Yoga assert functions, and the current API prevents us from making this a macro later to include the condition in the message. ## This stack The organization of the C++ internals of Yoga are in need of attention. 1. Some of the C++ internals are namespaced, but others not. 2. Some of the namespaces include `detail`, but are meant to be used outside of the translation unit (FB Clang Tidy rules warn on any usage of these) 2. Most of the files are in a flat hierarchy, except for event tracing in its own folder 3. Some files and functions begin with YG, others don’t 4. Some functions are uppercase, others are not 5. Almost all of the interesting logic is in Yoga.cpp, and the file is too large to reason about 6. There are multiple grab bag files where folks put random functions they need in (Utils, BitUtils, Yoga-Internal.h) 7. There is no clear indication from file structure or type naming what is private vs not 8. Handles like `YGNodeRef` and `YGConfigRef` can be used to access internals just by importing headers This stack does some much needed spring cleaning: 1. All non-public headers and C++ implementation details are in separate folders from the root level `yoga`. This will give us room to split up logic and add more files without too large a flat hierarchy 3. All private C++ internals are under the `facebook::yoga` namespace. Details namespaces are only ever used within the same header, as they are intended 4. Utils files are split 5. Most C++ internals drop the YG prefix 6. Most C++ internal function names are all lower camel case 7. We start to split up Yoga.cpp 8. Every header beginning with YG or at the top-level directory is public and C only, with the exception of Yoga-Internal.h which has non-public functions for bindings 9. It is not possible to use private APIs without static casting handles to internal classes This will give us more leeway to continue splitting monolithic files, and consistent guidelines for style in new files as well. These changes should not be breaking to any project using only public Yoga headers. This includes every usage of Yoga in fbsource except for RN Fabric which is currently tied to internals. This refactor should make that boundary clearer. Reviewed By: rshest Differential Revision: D48769809 fbshipit-source-id: 7714e8c3c3746433265af023a60488c96239c0c5
- Loading branch information
1 parent
5ba0725
commit 5a40d4d
Showing
6 changed files
with
117 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#include <stdexcept> | ||
|
||
#include <yoga/debug/Assert.h> | ||
#include <yoga/debug/Log.h> | ||
|
||
namespace facebook::yoga { | ||
|
||
[[noreturn]] void fatalWithMessage(const char* message) { | ||
#if defined(__cpp_exceptions) | ||
throw std::logic_error(message); | ||
#else | ||
std::terminate(); | ||
#endif | ||
} | ||
|
||
void assertFatal(const bool condition, const char* message) { | ||
if (!condition) { | ||
yoga::log( | ||
static_cast<yoga::Node*>(nullptr), | ||
YGLogLevelFatal, | ||
nullptr, | ||
"%s\n", | ||
message); | ||
fatalWithMessage(message); | ||
} | ||
} | ||
|
||
void assertFatalWithNode( | ||
const YGNodeRef node, | ||
const bool condition, | ||
const char* message) { | ||
if (!condition) { | ||
yoga::log( | ||
static_cast<yoga::Node*>(node), | ||
YGLogLevelFatal, | ||
nullptr, | ||
"%s\n", | ||
message); | ||
fatalWithMessage(message); | ||
} | ||
} | ||
|
||
void assertFatalWithConfig( | ||
const YGConfigRef config, | ||
const bool condition, | ||
const char* message) { | ||
if (!condition) { | ||
yoga::log( | ||
static_cast<yoga::Config*>(config), | ||
YGLogLevelFatal, | ||
nullptr, | ||
"%s\n", | ||
message); | ||
fatalWithMessage(message); | ||
} | ||
} | ||
|
||
} // namespace facebook::yoga |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <yoga/Yoga.h> | ||
#include <yoga/node/Node.h> | ||
#include <yoga/config/Config.h> | ||
|
||
namespace facebook::yoga { | ||
|
||
[[noreturn]] void fatalWithMessage(const char* message); | ||
|
||
void assertFatal(bool condition, const char* message); | ||
void assertFatalWithNode(YGNodeRef node, bool condition, const char* message); | ||
void assertFatalWithConfig( | ||
YGConfigRef config, | ||
bool condition, | ||
const char* message); | ||
|
||
} // namespace facebook::yoga |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters