Add braces to C++ single-line loops and conditionals (NFC) (#2973)

This makes code easier to read and more consistent between C++ and Java.
Also update clang-format settings to always add a line break (even if no braces are used).
This commit is contained in:
Peter Johnson
2020-12-28 12:58:06 -08:00
committed by GitHub
parent 0291a3ff56
commit 2aed432b4b
634 changed files with 10716 additions and 3938 deletions

View File

@@ -53,7 +53,9 @@ static void WindowPosCallback(GLFWwindow* window, int xpos, int ypos) {
static void* IniReadOpen(ImGuiContext* ctx, ImGuiSettingsHandler* handler,
const char* name) {
if (std::strcmp(name, "GLOBAL") != 0) return nullptr;
if (std::strcmp(name, "GLOBAL") != 0) {
return nullptr;
}
return static_cast<SavedSettings*>(gContext);
}
@@ -61,7 +63,9 @@ static void IniReadLine(ImGuiContext* ctx, ImGuiSettingsHandler* handler,
void* entry, const char* lineStr) {
auto impl = static_cast<SavedSettings*>(entry);
const char* value = std::strchr(lineStr, '=');
if (!value) return;
if (!value) {
return;
}
++value;
int num = std::atoi(value);
if (std::strncmp(lineStr, "width=", 6) == 0) {
@@ -85,7 +89,9 @@ static void IniReadLine(ImGuiContext* ctx, ImGuiSettingsHandler* handler,
static void IniWriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler,
ImGuiTextBuffer* out_buf) {
if (!gContext) return;
if (!gContext) {
return;
}
out_buf->appendf(
"[MainWindow][GLOBAL]\nwidth=%d\nheight=%d\nmaximized=%d\n"
"xpos=%d\nypos=%d\nuserScale=%d\nstyle=%d\n\n",
@@ -118,7 +124,9 @@ bool gui::Initialize(const char* title, int width, int height) {
glfwSetErrorCallback(ErrorCallback);
glfwInitHint(GLFW_JOYSTICK_HAT_BUTTONS, GLFW_FALSE);
PlatformGlfwInitHints();
if (!glfwInit()) return false;
if (!glfwInit()) {
return false;
}
PlatformGlfwWindowHints();
@@ -140,7 +148,9 @@ bool gui::Initialize(const char* title, int width, int height) {
io.IniFilename = gContext->iniPath.c_str();
for (auto&& initialize : gContext->initializers) {
if (initialize) initialize();
if (initialize) {
initialize();
}
}
// Load INI file
@@ -170,25 +180,32 @@ bool gui::Initialize(const char* title, int width, int height) {
}
}
}
if (gContext->xPos != -1 && gContext->yPos != -1)
if (gContext->xPos != -1 && gContext->yPos != -1) {
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
}
// Create window with graphics context
gContext->window =
glfwCreateWindow(gContext->width, gContext->height,
gContext->title.c_str(), nullptr, nullptr);
if (!gContext->window) return false;
if (!gContext->window) {
return false;
}
if (!gContext->loadedWidthHeight) {
if (windowScale == 1.0)
if (windowScale == 1.0) {
glfwGetWindowContentScale(gContext->window, &windowScale, nullptr);
}
// force user scale if window scale is smaller
if (windowScale <= 0.5)
if (windowScale <= 0.5) {
gContext->userScale = 0;
else if (windowScale <= 0.75)
} else if (windowScale <= 0.75) {
gContext->userScale = 1;
}
if (windowScale != 1.0) {
for (auto&& func : gContext->windowScalers) func(windowScale);
for (auto&& func : gContext->windowScalers) {
func(windowScale);
}
}
}
@@ -209,7 +226,9 @@ bool gui::Initialize(const char* title, int width, int height) {
if (!gContext->icons.empty()) {
glfwSetWindowIcon(gContext->window, gContext->icons.size(),
gContext->icons.data());
for (auto&& icon : gContext->icons) stbi_image_free(icon.pixels);
for (auto&& icon : gContext->icons) {
stbi_image_free(icon.pixels);
}
gContext->icons.clear();
}
@@ -232,7 +251,9 @@ bool gui::Initialize(const char* title, int width, int height) {
}
}
if (!PlatformInitRenderer()) return false;
if (!PlatformInitRenderer()) {
return false;
}
return true;
}
@@ -273,12 +294,16 @@ void gui::CommonRenderFrame() {
for (size_t i = 0; i < gContext->earlyExecutors.size(); ++i) {
auto& execute = gContext->earlyExecutors[i];
if (execute) execute();
if (execute) {
execute();
}
}
for (size_t i = 0; i < gContext->lateExecutors.size(); ++i) {
auto& execute = gContext->lateExecutors[i];
if (execute) execute();
if (execute) {
execute();
}
}
// Rendering
@@ -286,35 +311,48 @@ void gui::CommonRenderFrame() {
}
void gui::Exit() {
if (!gContext) return;
if (!gContext) {
return;
}
gContext->exit = true;
}
void gui::AddInit(std::function<void()> initialize) {
if (initialize) gContext->initializers.emplace_back(std::move(initialize));
if (initialize) {
gContext->initializers.emplace_back(std::move(initialize));
}
}
void gui::AddWindowScaler(std::function<void(float scale)> windowScaler) {
if (windowScaler)
if (windowScaler) {
gContext->windowScalers.emplace_back(std::move(windowScaler));
}
}
void gui::AddEarlyExecute(std::function<void()> execute) {
if (execute) gContext->earlyExecutors.emplace_back(std::move(execute));
if (execute) {
gContext->earlyExecutors.emplace_back(std::move(execute));
}
}
void gui::AddLateExecute(std::function<void()> execute) {
if (execute) gContext->lateExecutors.emplace_back(std::move(execute));
if (execute) {
gContext->lateExecutors.emplace_back(std::move(execute));
}
}
GLFWwindow* gui::GetSystemWindow() { return gContext->window; }
GLFWwindow* gui::GetSystemWindow() {
return gContext->window;
}
bool gui::AddIcon(const unsigned char* data, int len) {
// Load from memory
GLFWimage image;
image.pixels =
stbi_load_from_memory(data, len, &image.width, &image.height, nullptr, 4);
if (!data) return false;
if (!data) {
return false;
}
gContext->icons.emplace_back(std::move(image));
return true;
}
@@ -323,7 +361,9 @@ int gui::AddFont(
const char* name,
std::function<ImFont*(ImGuiIO& io, float size, const ImFontConfig* cfg)>
makeFont) {
if (makeFont) gContext->makeFonts.emplace_back(name, std::move(makeFont));
if (makeFont) {
gContext->makeFonts.emplace_back(name, std::move(makeFont));
}
return gContext->makeFonts.size() - 1;
}
@@ -346,13 +386,16 @@ void gui::SetStyle(Style style) {
}
}
void gui::SetClearColor(ImVec4 color) { gContext->clearColor = color; }
void gui::SetClearColor(ImVec4 color) {
gContext->clearColor = color;
}
void gui::ConfigurePlatformSaveFile(const std::string& name) {
gContext->iniPath = name;
#if defined(_MSC_VER)
const char* env = std::getenv("APPDATA");
if (env) gContext->iniPath = env + std::string("/" + name);
if (env)
gContext->iniPath = env + std::string("/" + name);
#elif defined(__APPLE__)
const char* env = std::getenv("HOME");
if (env)
@@ -360,10 +403,11 @@ void gui::ConfigurePlatformSaveFile(const std::string& name) {
#else
const char* xdg = std::getenv("XDG_CONFIG_HOME");
const char* env = std::getenv("HOME");
if (xdg)
if (xdg) {
gContext->iniPath = xdg + std::string("/" + name);
else if (env)
} else if (env) {
gContext->iniPath = env + std::string("/.config/" + name);
}
#endif
}
@@ -372,14 +416,17 @@ void gui::EmitViewMenu() {
if (ImGui::BeginMenu("Style")) {
bool selected;
selected = gContext->style == kStyleClassic;
if (ImGui::MenuItem("Classic", nullptr, &selected, true))
if (ImGui::MenuItem("Classic", nullptr, &selected, true)) {
SetStyle(kStyleClassic);
}
selected = gContext->style == kStyleDark;
if (ImGui::MenuItem("Dark", nullptr, &selected, true))
if (ImGui::MenuItem("Dark", nullptr, &selected, true)) {
SetStyle(kStyleDark);
}
selected = gContext->style == kStyleLight;
if (ImGui::MenuItem("Light", nullptr, &selected, true))
if (ImGui::MenuItem("Light", nullptr, &selected, true)) {
SetStyle(kStyleLight);
}
ImGui::EndMenu();
}
@@ -391,8 +438,9 @@ void gui::EmitViewMenu() {
bool enabled = (gContext->fontScale - gContext->userScale + i) >= 0 &&
(gContext->fontScale - gContext->userScale + i) <
Font::kScaledLevels;
if (ImGui::MenuItem(label, nullptr, &selected, enabled))
if (ImGui::MenuItem(label, nullptr, &selected, enabled)) {
gContext->userScale = i;
}
}
ImGui::EndMenu();
}
@@ -407,12 +455,15 @@ bool gui::UpdateTextureFromImage(ImTextureID* texture, int width, int height,
int height2 = 0;
unsigned char* imgData =
stbi_load_from_memory(data, len, &width2, &height2, nullptr, 4);
if (!data) return false;
if (!data) {
return false;
}
if (width2 == width && height2 == height)
if (width2 == width && height2 == height) {
UpdateTexture(texture, kPixelRGBA, width2, height2, imgData);
else
} else {
*texture = CreateTexture(kPixelRGBA, width2, height2, imgData);
}
stbi_image_free(imgData);
@@ -425,11 +476,17 @@ bool gui::CreateTextureFromFile(const char* filename, ImTextureID* out_texture,
int width = 0;
int height = 0;
unsigned char* data = stbi_load(filename, &width, &height, nullptr, 4);
if (!data) return false;
if (!data) {
return false;
}
*out_texture = CreateTexture(kPixelRGBA, width, height, data);
if (out_width) *out_width = width;
if (out_height) *out_height = height;
if (out_width) {
*out_width = width;
}
if (out_height) {
*out_height = height;
}
stbi_image_free(data);
@@ -444,11 +501,17 @@ bool gui::CreateTextureFromImage(const unsigned char* data, int len,
int height = 0;
unsigned char* imgData =
stbi_load_from_memory(data, len, &width, &height, nullptr, 4);
if (!imgData) return false;
if (!imgData) {
return false;
}
*out_texture = CreateTexture(kPixelRGBA, width, height, imgData);
if (out_width) *out_width = width;
if (out_height) *out_height = height;
if (out_width) {
*out_width = width;
}
if (out_height) {
*out_height = height;
}
stbi_image_free(imgData);
@@ -458,7 +521,9 @@ bool gui::CreateTextureFromImage(const unsigned char* data, int len,
void gui::MaxFit(ImVec2* min, ImVec2* max, float width, float height) {
float destWidth = max->x - min->x;
float destHeight = max->y - min->y;
if (width == 0 || height == 0) return;
if (width == 0 || height == 0) {
return;
}
if (destWidth * height > destHeight * width) {
float outputWidth = width * destHeight / height;
min->x += (destWidth - outputWidth) / 2;

View File

@@ -101,7 +101,9 @@ static void CleanupDeviceD3D() {
namespace wpi {
void gui::PlatformCreateContext() { gPlatformContext = new PlatformContext; }
void gui::PlatformCreateContext() {
gPlatformContext = new PlatformContext;
}
void gui::PlatformDestroyContext() {
CleanupDeviceD3D();
@@ -173,7 +175,9 @@ static inline DXGI_FORMAT DXPixelFormat(PixelFormat format) {
ImTextureID gui::CreateTexture(PixelFormat format, int width, int height,
const unsigned char* data) {
if (!gPlatformValid) return nullptr;
if (!gPlatformValid) {
return nullptr;
}
// Create texture
D3D11_TEXTURE2D_DESC desc;
@@ -212,7 +216,9 @@ ImTextureID gui::CreateTexture(PixelFormat format, int width, int height,
void gui::UpdateTexture(ImTextureID texture, PixelFormat, int width, int height,
const unsigned char* data) {
if (!texture) return;
if (!texture) {
return;
}
D3D11_BOX box;
box.front = 0;
@@ -234,8 +240,11 @@ void gui::UpdateTexture(ImTextureID texture, PixelFormat, int width, int height,
}
void gui::DeleteTexture(ImTextureID texture) {
if (!gPlatformValid) return;
if (texture) static_cast<ID3D11ShaderResourceView*>(texture)->Release();
if (!gPlatformValid) {
return;
}
if (texture)
static_cast<ID3D11ShaderResourceView*>(texture)->Release();
}
} // namespace wpi

View File

@@ -271,7 +271,9 @@ class Texture {
Texture& operator=(const Texture&) = delete;
Texture& operator=(Texture&& oth) {
if (m_texture) DeleteTexture(m_texture);
if (m_texture) {
DeleteTexture(m_texture);
}
m_texture = oth.m_texture;
oth.m_texture = 0;
m_format = oth.m_format;
@@ -281,7 +283,9 @@ class Texture {
}
~Texture() {
if (m_texture) DeleteTexture(m_texture);
if (m_texture) {
DeleteTexture(m_texture);
}
}
/**
@@ -351,8 +355,9 @@ class Texture {
static Texture CreateFromFile(const char* filename) {
Texture texture;
if (!CreateTextureFromFile(filename, &texture.m_texture, &texture.m_width,
&texture.m_height))
&texture.m_height)) {
return {};
}
return texture;
}
@@ -367,8 +372,9 @@ class Texture {
static Texture CreateFromImage(const unsigned char* data, int len) {
Texture texture;
if (!CreateTextureFromImage(data, len, &texture.m_texture, &texture.m_width,
&texture.m_height))
&texture.m_height)) {
return {};
}
return texture;
}

View File

@@ -111,7 +111,9 @@ static inline GLenum GLPixelFormat(PixelFormat format) {
ImTextureID gui::CreateTexture(PixelFormat format, int width, int height,
const unsigned char* data) {
if (!gPlatformValid) return nullptr;
if (!gPlatformValid) {
return nullptr;
}
// Create a OpenGL texture identifier
GLuint texture;
@@ -133,16 +135,22 @@ ImTextureID gui::CreateTexture(PixelFormat format, int width, int height,
void gui::UpdateTexture(ImTextureID texture, PixelFormat format, int width,
int height, const unsigned char* data) {
GLuint glTexture = static_cast<GLuint>(reinterpret_cast<uintptr_t>(texture));
if (glTexture == 0) return;
if (glTexture == 0) {
return;
}
glBindTexture(GL_TEXTURE_2D, glTexture);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GLPixelFormat(format),
GL_UNSIGNED_BYTE, data);
}
void gui::DeleteTexture(ImTextureID texture) {
if (!gPlatformValid) return;
if (!gPlatformValid) {
return;
}
GLuint glTexture = static_cast<GLuint>(reinterpret_cast<uintptr_t>(texture));
if (glTexture != 0) glDeleteTextures(1, &glTexture);
if (glTexture != 0) {
glDeleteTextures(1, &glTexture);
}
}
} // namespace wpi