Replace statics in headers with inline constants (#15100)

C++ is a very well balanced and reasonable language, which is why
`static` inside classes means "shared between all instances", whereas
`static` outside of classes means "not shared between all .cpp files".

32 years after this problem was written onto parchment it was fixed with
the introduction of inline variables in C++17, which tell the compiler
to deduplicate variables the same way it deduplicates functions.
This commit is contained in:
Leonard Hecker
2023-04-04 17:19:20 +02:00
committed by GitHub
parent c0f14567f3
commit 47a17cf2d7
11 changed files with 32 additions and 32 deletions

View File

@@ -15,9 +15,6 @@
#define DECLARE_ACTION_HANDLER(action) void _Handle##action(const IInspectable& sender, const Microsoft::Terminal::Settings::Model::ActionEventArgs& args);
static constexpr uint32_t DefaultRowsToScroll{ 3 };
static constexpr std::wstring_view TabletInputServiceKey{ L"TabletInputService" };
namespace TerminalAppLocalTests
{
class TabTests;
@@ -31,6 +28,9 @@ namespace Microsoft::Terminal::Core
namespace winrt::TerminalApp::implementation
{
inline constexpr uint32_t DefaultRowsToScroll{ 3 };
inline constexpr std::wstring_view TabletInputServiceKey{ L"TabletInputService" };
enum StartupState : int
{
NotInitialized = 0,

View File

@@ -7,9 +7,6 @@
#include "TabBase.h"
#include "TerminalTab.g.h"
static constexpr double HeaderRenameBoxWidthDefault{ 165 };
static constexpr double HeaderRenameBoxWidthTitleLength{ std::numeric_limits<double>::infinity() };
// fwdecl unittest classes
namespace TerminalAppLocalTests
{
@@ -109,6 +106,9 @@ namespace winrt::TerminalApp::implementation
TYPED_EVENT(TaskbarProgressChanged, IInspectable, IInspectable);
private:
static constexpr double HeaderRenameBoxWidthDefault{ 165 };
static constexpr double HeaderRenameBoxWidthTitleLength{ std::numeric_limits<double>::infinity() };
std::shared_ptr<Pane> _rootPane{ nullptr };
std::shared_ptr<Pane> _activePane{ nullptr };
std::shared_ptr<Pane> _zoomedPane{ nullptr };

View File

@@ -3,4 +3,4 @@
#pragma once
static constexpr std::wstring_view AzureClientID = L"0";
inline constexpr std::wstring_view AzureClientID = L"0";

View File

@@ -17,8 +17,8 @@
#include <til/ticket_lock.h>
static constexpr std::wstring_view linkPattern{ LR"(\b(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|$!:,.;]*[A-Za-z0-9+&@#/%=~_|$])" };
static constexpr size_t TaskbarMinProgress{ 10 };
inline constexpr std::wstring_view linkPattern{ LR"(\b(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|$!:,.;]*[A-Za-z0-9+&@#/%=~_|$])" };
inline constexpr size_t TaskbarMinProgress{ 10 };
// You have to forward decl the ICoreSettings here, instead of including the header.
// If you include the header, there will be compilation errors with other

View File

@@ -10,13 +10,13 @@
namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
{
inline static constexpr uint8_t ColorTableDivider{ 8 };
inline static constexpr uint8_t ColorTableSize{ 16 };
inline constexpr uint8_t ColorTableDivider{ 8 };
inline constexpr uint8_t ColorTableSize{ 16 };
inline static constexpr std::wstring_view ForegroundColorTag{ L"Foreground" };
inline static constexpr std::wstring_view BackgroundColorTag{ L"Background" };
inline static constexpr std::wstring_view CursorColorTag{ L"CursorColor" };
inline static constexpr std::wstring_view SelectionBackgroundColorTag{ L"SelectionBackground" };
inline constexpr std::wstring_view ForegroundColorTag{ L"Foreground" };
inline constexpr std::wstring_view BackgroundColorTag{ L"Background" };
inline constexpr std::wstring_view CursorColorTag{ L"CursorColor" };
inline constexpr std::wstring_view SelectionBackgroundColorTag{ L"SelectionBackground" };
struct ColorSchemeViewModel : ColorSchemeViewModelT<ColorSchemeViewModel>, ViewModelHelper<ColorSchemeViewModel>
{

View File

@@ -21,6 +21,6 @@ Author(s):
// will become disconnected from user settings.
// {2bde4a90-d05f-401c-9492-e40884ead1d8}
// uuidv5 properties: name format is UTF-16LE bytes
static constexpr GUID TERMINAL_PROFILE_NAMESPACE_GUID = { 0x2bde4a90, 0xd05f, 0x401c, { 0x94, 0x92, 0xe4, 0x8, 0x84, 0xea, 0xd1, 0xd8 } };
inline constexpr GUID TERMINAL_PROFILE_NAMESPACE_GUID = { 0x2bde4a90, 0xd05f, 0x401c, { 0x94, 0x92, 0xe4, 0x8, 0x84, 0xea, 0xd1, 0xd8 } };
winrt::com_ptr<winrt::Microsoft::Terminal::Settings::Model::implementation::Profile> CreateDynamicProfile(const std::wstring_view& name);

View File

@@ -16,6 +16,6 @@ Author(s):
#pragma once
static constexpr std::wstring_view WslGeneratorNamespace{ L"Windows.Terminal.Wsl" };
static constexpr std::wstring_view AzureGeneratorNamespace{ L"Windows.Terminal.Azure" };
static constexpr std::wstring_view PowershellCoreGeneratorNamespace{ L"Windows.Terminal.PowershellCore" };
inline constexpr std::wstring_view WslGeneratorNamespace{ L"Windows.Terminal.Wsl" };
inline constexpr std::wstring_view AzureGeneratorNamespace{ L"Windows.Terminal.Azure" };
inline constexpr std::wstring_view PowershellCoreGeneratorNamespace{ L"Windows.Terminal.PowershellCore" };

View File

@@ -4,11 +4,11 @@ Licensed under the MIT license.
--*/
#pragma once
constexpr int32_t WindowingBehaviorUseCurrent{ 0 };
constexpr int32_t WindowingBehaviorUseNew{ -1 };
constexpr int32_t WindowingBehaviorUseExisting{ -2 };
constexpr int32_t WindowingBehaviorUseAnyExisting{ -3 };
constexpr int32_t WindowingBehaviorUseName{ -4 };
constexpr int32_t WindowingBehaviorUseNone{ -5 };
inline constexpr int32_t WindowingBehaviorUseCurrent{ 0 };
inline constexpr int32_t WindowingBehaviorUseNew{ -1 };
inline constexpr int32_t WindowingBehaviorUseExisting{ -2 };
inline constexpr int32_t WindowingBehaviorUseAnyExisting{ -3 };
inline constexpr int32_t WindowingBehaviorUseName{ -4 };
inline constexpr int32_t WindowingBehaviorUseNone{ -5 };
static constexpr std::wstring_view QuakeWindowName{ L"_quake" };
inline constexpr std::wstring_view QuakeWindowName{ L"_quake" };

View File

@@ -24,9 +24,9 @@ namespace til::spsc
namespace details
{
static constexpr size_type position_mask = std::numeric_limits<size_type>::max() >> 2u; // 0b00111....
static constexpr size_type revolution_flag = 1u << (std::numeric_limits<size_type>::digits - 2u); // 0b01000....
static constexpr size_type drop_flag = 1u << (std::numeric_limits<size_type>::digits - 1u); // 0b10000....
inline constexpr size_type position_mask = std::numeric_limits<size_type>::max() >> 2u; // 0b00111....
inline constexpr size_type revolution_flag = 1u << (std::numeric_limits<size_type>::digits - 2u); // 0b01000....
inline constexpr size_type drop_flag = 1u << (std::numeric_limits<size_type>::digits - 1u); // 0b10000....
struct block_initially_policy
{

View File

@@ -36,7 +36,7 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
inline constexpr uint8_t F_ = 0b10; // stripped in clean_filename
inline constexpr uint8_t _P = 0b01; // stripped in clean_path
inline constexpr uint8_t FP = 0b11; // stripped in clean_filename and clean_path
static constexpr std::array<uint8_t, 128> pathFilter{ {
inline constexpr std::array<uint8_t, 128> pathFilter{ {
// clang-format off
__ /* NUL */, __ /* SOH */, __ /* STX */, __ /* ETX */, __ /* EOT */, __ /* ENQ */, __ /* ACK */, __ /* BEL */, __ /* BS */, __ /* HT */, __ /* LF */, __ /* VT */, __ /* FF */, __ /* CR */, __ /* SO */, __ /* SI */,
__ /* DLE */, __ /* DC1 */, __ /* DC2 */, __ /* DC3 */, __ /* DC4 */, __ /* NAK */, __ /* SYN */, __ /* ETB */, __ /* CAN */, __ /* EM */, __ /* SUB */, __ /* ESC */, __ /* FS */, __ /* GS */, __ /* RS */, __ /* US */,

View File

@@ -20,8 +20,8 @@ Author(s):
#include "IFontDefaultList.hpp"
static constexpr wchar_t DEFAULT_TT_FONT_FACENAME[]{ L"__DefaultTTFont__" };
static constexpr wchar_t DEFAULT_RASTER_FONT_FACENAME[]{ L"Terminal" };
inline constexpr wchar_t DEFAULT_TT_FONT_FACENAME[]{ L"__DefaultTTFont__" };
inline constexpr wchar_t DEFAULT_RASTER_FONT_FACENAME[]{ L"Terminal" };
class FontInfoBase
{