Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Dec 13, 2021
1 parent f20c908 commit 99c1e30
Show file tree
Hide file tree
Showing 45 changed files with 221 additions and 193 deletions.
75 changes: 38 additions & 37 deletions far/PluginA.cpp
Expand Up @@ -711,7 +711,7 @@ static PluginPanelItem* ConvertAnsiPanelItemsToUnicode(span<const oldfar::Plugin

if (Src.Flags&oldfar::PPIF_USERDATA)
{
const auto UserData = reinterpret_cast<const void*>(Src.UserData);
const auto UserData = view_as<const void*>(Src.UserData);
const auto Size = *static_cast<const DWORD*>(UserData);
Dst.UserData.Data = new char[Size];
copy_memory(UserData, Dst.UserData.Data, Size);
Expand Down Expand Up @@ -810,7 +810,7 @@ static void FreePanelItemA(span<const oldfar::PluginPanelItem> const PanelItem)

if (Item.Flags & oldfar::PPIF_USERDATA)
{
delete[] reinterpret_cast<char*>(Item.UserData);
delete[] edit_as<char*>(Item.UserData);
}
}

Expand Down Expand Up @@ -838,7 +838,7 @@ static char *InsertQuoteA(char *Str)

static auto GetPluginUuid(intptr_t n)
{
return &reinterpret_cast<Plugin*>(n)->Id();
return &view_as<Plugin>(n).Id();
}

struct DialogData
Expand Down Expand Up @@ -892,12 +892,12 @@ static size_t GetAnsiVBufSize(const oldfar::FarDialogItem &diA)

static auto GetAnsiVBufPtr(FAR_CHAR_INFO* VBuf, size_t Size)
{
return VBuf? *reinterpret_cast<PCHAR_INFO*>(&VBuf[Size]) : nullptr;
return VBuf? *edit_as<PCHAR_INFO*>(&VBuf[Size]) : nullptr;
}

static void SetAnsiVBufPtr(FAR_CHAR_INFO* VBuf, CHAR_INFO* VBufA, size_t Size)
{
*reinterpret_cast<PCHAR_INFO*>(&VBuf[Size]) = VBufA;
*edit_as<PCHAR_INFO*>(&VBuf[Size]) = VBufA;
}

static void AnsiVBufToUnicode(CHAR_INFO* VBufA, FAR_CHAR_INFO* VBuf, size_t Size, bool NoCvt)
Expand Down Expand Up @@ -1508,7 +1508,7 @@ struct FAR_SEARCH_A_CALLBACK_PARAM
void *Param;
};

static const char* GetPluginMsg(const Plugin* PluginInstance, int MsgId);
static const char* GetPluginMsg(const Plugin& PluginInstance, int MsgId);

namespace oldpluginapi
{
Expand Down Expand Up @@ -2402,7 +2402,7 @@ static int WINAPI FarMessageFnA(intptr_t PluginNumber, DWORD Flags, const char *

if (Flags&oldfar::FMSG_ALLINONE)
{
AllInOneAnsiItem.reset(AnsiToUnicode(reinterpret_cast<const char*>(Items)));
AllInOneAnsiItem.reset(AnsiToUnicode(view_as<const char*>(Items)));
}
else
{
Expand Down Expand Up @@ -2449,7 +2449,7 @@ static int WINAPI FarMessageFnA(intptr_t PluginNumber, DWORD Flags, const char *
&FarUuid,
NewFlags,
HelpTopic? encoding::oem::get_chars(HelpTopic).c_str() : nullptr,
AnsiItems.empty()? reinterpret_cast<const wchar_t* const *>(AllInOneAnsiItem.get()) : reinterpret_cast<const wchar_t* const *>(AnsiItems.data()),
view_as<const wchar_t* const*>(AnsiItems.empty()? AllInOneAnsiItem.get() : AnsiItems.data()->get()),
ItemsNumber,
ButtonsNumber
);
Expand All @@ -2461,17 +2461,17 @@ static int WINAPI FarMessageFnA(intptr_t PluginNumber, DWORD Flags, const char *
});
}

static const char * WINAPI FarGetMsgFnA(intptr_t PluginHandle, int MsgId) noexcept
static const char* WINAPI FarGetMsgFnA(intptr_t PluginHandle, int MsgId) noexcept
{
return cpp_try(
[&]
{
//BUGBUG, надо проверять, что PluginHandle - плагин
const auto pPlugin = reinterpret_cast<Plugin*>(PluginHandle);
string_view Path = pPlugin->ModuleName();
auto& pPlugin = edit_as<Plugin>(PluginHandle);
string_view Path = pPlugin.ModuleName();
CutToSlash(Path);

if (pPlugin->InitLang(Path, Global->Opt->strLanguage))
if (pPlugin.InitLang(Path, Global->Opt->strLanguage))
return GetPluginMsg(pPlugin, MsgId);

return "";
Expand Down Expand Up @@ -2516,7 +2516,7 @@ static int WINAPI FarMenuFnA(intptr_t PluginNumber, int X, int Y, int MaxHeight,
OLDFAR_TO_FAR_MAP(MIF_HIDDEN),
};

for (const auto& [Item, AnsiItem]: zip(mi, span(reinterpret_cast<const oldfar::FarMenuItemEx*>(Items), ItemsNumber)))
for (const auto& [Item, AnsiItem]: zip(mi, span(view_as<const oldfar::FarMenuItemEx*>(Items), ItemsNumber)))
{
Item.Flags = MIF_NONE;
FirstFlagsToSecond(AnsiItem.Flags, Item.Flags, ItemFlagsMap);
Expand Down Expand Up @@ -2544,7 +2544,7 @@ static int WINAPI FarMenuFnA(intptr_t PluginNumber, int X, int Y, int MaxHeight,
Item.Flags |= MIF_CHECKED;

if (AnsiItem.Checked > 1)
AnsiToUnicodeBin({ reinterpret_cast<const char*>(&AnsiItem.Checked), 1 }, reinterpret_cast<wchar_t*>(&Item.Flags));
AnsiToUnicodeBin({ view_as<const char*>(&AnsiItem.Checked), 1 }, edit_as<wchar_t*>(&Item.Flags));
}

if (AnsiItem.Separator)
Expand Down Expand Up @@ -2752,7 +2752,7 @@ static intptr_t WINAPI DlgProcA(HANDLE hDlg, intptr_t NewMsg, intptr_t Param1, v
if (ret && ret != reinterpret_cast<intptr_t>(Param2)) // changed
{
static std::unique_ptr<wchar_t[]> HelpTopic;
HelpTopic.reset(AnsiToUnicode(reinterpret_cast<const char*>(ret)));
HelpTopic.reset(AnsiToUnicode(view_as<const char*>(ret)));
ret = reinterpret_cast<intptr_t>(HelpTopic.get());
}
return ret;
Expand Down Expand Up @@ -3149,7 +3149,8 @@ static intptr_t WINAPI FarSendDlgMessageA(HANDLE hDlg, int OldMsg, int Param1, v
{
intptr_t Size = pluginapi::apiSendDlgMessage(hDlg, DM_LISTGETDATASIZE, Param1, Param2);
intptr_t Data = pluginapi::apiSendDlgMessage(hDlg, DM_LISTGETDATA, Param1, Param2);
if(Size<=4) Data=Data?*reinterpret_cast<unsigned*>(Data):0;
if (Size <= 4)
Data = Data? view_as<unsigned>(Data) : 0;
return Data;
}
case oldfar::DM_LISTSETDATA:
Expand Down Expand Up @@ -4843,15 +4844,15 @@ static int WINAPI FarCharTableA(int Command, char *Buffer, int BufferSize) noexc
if (BufferSize != static_cast<int>(sizeof(oldfar::CharTableSet)))
return -1;

const auto TableSet = reinterpret_cast<oldfar::CharTableSet*>(Buffer);
auto& TableSet = edit_as<oldfar::CharTableSet>(Buffer);
//Preset. Also if Command != FCT_DETECT and failed, buffer must be filled by OEM data.
strcpy(TableSet->TableName,"<failed>");
strcpy(TableSet.TableName,"<failed>");

for (const auto& i: irange(256u))
{
TableSet->EncodeTable[i] = TableSet->DecodeTable[i] = i;
TableSet->UpperTable[i] = LocalUpper(i);
TableSet->LowerTable[i] = LocalLower(i);
TableSet.EncodeTable[i] = TableSet.DecodeTable[i] = i;
TableSet.UpperTable[i] = LocalUpper(i);
TableSet.LowerTable[i] = LocalLower(i);
}

const auto nCP = ConvertCharTableToCodePage(Command);
Expand All @@ -4864,17 +4865,17 @@ static int WINAPI FarCharTableA(int Command, char *Buffer, int BufferSize) noexc

auto sTableName = pad_right(str(nCP), 5);
append(sTableName, BoxSymbols[BS_V1], L' ', Info->Name);
(void)encoding::oem::get_bytes(sTableName, TableSet->TableName);
std::unique_ptr<wchar_t[]> const us(AnsiToUnicodeBin({ reinterpret_cast<char*>(TableSet->DecodeTable), std::size(TableSet->DecodeTable) }, nCP));
(void)encoding::oem::get_bytes(sTableName, TableSet.TableName);
std::unique_ptr<wchar_t[]> const us(AnsiToUnicodeBin({ edit_as<char*>(TableSet.DecodeTable), std::size(TableSet.DecodeTable) }, nCP));

inplace::lower({ us.get(), std::size(TableSet->DecodeTable) });
(void)encoding::get_bytes(nCP, { us.get(), std::size(TableSet->DecodeTable) }, { reinterpret_cast<char*>(TableSet->LowerTable), std::size(TableSet->DecodeTable) });
inplace::lower({ us.get(), std::size(TableSet.DecodeTable) });
(void)encoding::get_bytes(nCP, { us.get(), std::size(TableSet.DecodeTable) }, { edit_as<char*>(TableSet.LowerTable), std::size(TableSet.DecodeTable) });

inplace::upper({ us.get(), std::size(TableSet->DecodeTable) });
(void)encoding::get_bytes(nCP, { us.get(), std::size(TableSet->DecodeTable) }, { reinterpret_cast<char*>(TableSet->UpperTable), std::size(TableSet->DecodeTable) });
inplace::upper({ us.get(), std::size(TableSet.DecodeTable) });
(void)encoding::get_bytes(nCP, { us.get(), std::size(TableSet.DecodeTable) }, { edit_as<char*>(TableSet.UpperTable), std::size(TableSet.DecodeTable) });

MultiByteRecode(nCP, encoding::codepage::oem(), { reinterpret_cast<char*>(TableSet->DecodeTable), std::size(TableSet->DecodeTable) });
MultiByteRecode(encoding::codepage::oem(), nCP, { reinterpret_cast<char*>(TableSet->EncodeTable), std::size(TableSet->EncodeTable) });
MultiByteRecode(nCP, encoding::codepage::oem(), { edit_as<char*>(TableSet.DecodeTable), std::size(TableSet.DecodeTable) });
MultiByteRecode(encoding::codepage::oem(), nCP, { edit_as<char*>(TableSet.EncodeTable), std::size(TableSet.EncodeTable) });
return Command;
}
return -1;
Expand Down Expand Up @@ -5091,7 +5092,7 @@ class PluginA final: public Plugin
while (nb > 0)
{
--nb;
reinterpret_cast<char*>(&Info->Guid)[8 + nb] = static_cast<char>(Info->Title[nb]);
edit_as<char*>(&Info->Guid)[8 + nb] = static_cast<char>(Info->Title[nb]);
}
}

Expand Down Expand Up @@ -5238,7 +5239,7 @@ WARNING_POP()
OpenFromA = oldfar::OPEN_COMMANDLINE;
if (Info->Data)
{
Buffer.reset(UnicodeToAnsi(reinterpret_cast<const OpenCommandLineInfo*>(Info->Data)->CommandLine));
Buffer.reset(UnicodeToAnsi(view_as<OpenCommandLineInfo>(Info->Data).CommandLine));
Ptr = reinterpret_cast<intptr_t>(Buffer.get());
}
break;
Expand All @@ -5247,8 +5248,8 @@ WARNING_POP()
OpenFromA = oldfar::OPEN_SHORTCUT;
if (Info->Data)
{
const auto SInfo = reinterpret_cast<const OpenShortcutInfo*>(Info->Data);
const auto shortcutdata = SInfo->ShortcutData ? SInfo->ShortcutData : SInfo->HostFile;
const auto& SInfo = view_as<OpenShortcutInfo>(Info->Data);
const auto shortcutdata = SInfo.ShortcutData? SInfo.ShortcutData : SInfo.HostFile;
Buffer.reset(UnicodeToAnsi(shortcutdata));
Ptr = reinterpret_cast<intptr_t>(Buffer.get());
}
Expand Down Expand Up @@ -5292,14 +5293,14 @@ WARNING_POP()
case OPEN_FROMMACRO:
// BUGBUG this is not how it worked in 1.7
OpenFromA = static_cast<int>(oldfar::OPEN_FROMMACRO) | static_cast<int>(Global->CtrlObject->Macro.GetArea());
Buffer.reset(UnicodeToAnsi(reinterpret_cast<OpenMacroInfo*>(Info->Data)->Count ? reinterpret_cast<OpenMacroInfo*>(Info->Data)->Values[0].String : L""));
Buffer.reset(UnicodeToAnsi(view_as<OpenMacroInfo>(Info->Data).Count? view_as<OpenMacroInfo>(Info->Data).Values[0].String : L""));
Ptr = reinterpret_cast<intptr_t>(Buffer.get());
break;

case OPEN_DIALOG:
OpenFromA = oldfar::OPEN_DIALOG;
DlgData.ItemNumber = Info->Guid->Data1;
DlgData.hDlg = reinterpret_cast<OpenDlgPluginData*>(Info->Data)->hDlg;
DlgData.hDlg = view_as<OpenDlgPluginData>(Info->Data).hDlg;
Ptr = reinterpret_cast<intptr_t>(&DlgData);
break;

Expand Down Expand Up @@ -6022,9 +6023,9 @@ WARNING_POP()
bool opif_shortcut{};
};

static const char* GetPluginMsg(const Plugin* PluginInstance, int MsgId)
static const char* GetPluginMsg(const Plugin& PluginInstance, int MsgId)
{
return static_cast<const PluginA*>(PluginInstance)->GetMsgA(MsgId);
return static_cast<const PluginA&>(PluginInstance).GetMsgA(MsgId);
}

std::unique_ptr<Plugin> oem_plugin_factory::CreatePlugin(const string& FileName)
Expand Down
16 changes: 8 additions & 8 deletions far/cddrv.cpp
Expand Up @@ -112,14 +112,6 @@ static auto read_value_from_big_endian(unsigned char const (&Src)[N])
return read_value_from_big_endian_impl<T>(Src, std::make_index_sequence<N>{});
}

template<typename T>
static auto& edit_as(void* const Buffer)
{
static_assert(std::is_trivially_copyable_v<T>);

return *static_cast<T*>(Buffer);
}

struct SCSI_PASS_THROUGH_WITH_BUFFERS: SCSI_PASS_THROUGH
{
UCHAR SenseBuf[32];
Expand Down Expand Up @@ -195,6 +187,14 @@ WARNING_DISABLE_CLANG("-Wswitch")
WARNING_POP()
}

template<typename T, typename container>
static auto view_as_if(container const& Buffer, intptr_t const Offset = 0)
{
static_assert(std::is_trivially_copyable_v<T>);

return Buffer.size() >= Offset + sizeof(T)? view_as<T const*>(Buffer.data() + Offset) : nullptr;
}

static auto capatibilities_from_scsi_configuration(const os::fs::file& Device)
{
SCSI_PASS_THROUGH_WITH_BUFFERS Spt;
Expand Down
5 changes: 5 additions & 0 deletions far/changelog
@@ -1,3 +1,8 @@
--------------------------------------------------------------------------------
drkns 13.12.2021 02:32:37 +0000 - build 5932

1. Refactoring.

--------------------------------------------------------------------------------
drkns 10.12.2021 23:51:14 +0000 - build 5931

Expand Down
6 changes: 3 additions & 3 deletions far/clipboard.cpp
Expand Up @@ -372,7 +372,7 @@ bool clipboard::SetHDROP(const string_view NamesData, const bool bMoved)
Drop->pt.y = 0;
Drop->fNC = TRUE;
Drop->fWide = TRUE;
*copy_string(NamesData, static_cast<wchar_t*>(static_cast<void*>(Drop.get() + 1))) = {};
*copy_string(NamesData, edit_as<wchar_t*>(Drop.get() + 1)) = {};

if (!Clear() || !SetData(CF_HDROP, std::move(Memory)))
return false;
Expand Down Expand Up @@ -429,12 +429,12 @@ bool clipboard::GetHDROPAsText(string& data) const
if (!Files)
return false;

const auto StartA=reinterpret_cast<const char*>(Files.get()) + Files->pFiles;
const auto StartA = view_as<const char*>(Files.get(), Files->pFiles);

const auto Eol = eol::system.str();
if (Files->fWide)
{
const auto Start = reinterpret_cast<const wchar_t*>(StartA);
const auto Start = view_as<const wchar_t*>(StartA);

for (const auto& i: enum_substrings(Start))
{
Expand Down
2 changes: 1 addition & 1 deletion far/codepage_selection.cpp
Expand Up @@ -592,7 +592,7 @@ intptr_t codepages::EditDialogProc(Dialog* Dlg, intptr_t Msg, intptr_t Param1, v

if (Param1 == EDITCP_OK)
{
strCodePageName = reinterpret_cast<const wchar_t*>(Dlg->SendMessage(DM_GETCONSTTEXTPTR, EDITCP_EDIT, nullptr));
strCodePageName = view_as<const wchar_t*>(Dlg->SendMessage(DM_GETCONSTTEXTPTR, EDITCP_EDIT, nullptr));
}
// Если имя кодовой страницы пустое, то считаем, что имя не задано
if (strCodePageName.empty())
Expand Down
2 changes: 1 addition & 1 deletion far/common.tests.cpp
Expand Up @@ -651,7 +651,7 @@ TEST_CASE("placement")
};

std::aligned_storage_t<sizeof(raii), alignof(raii)> Data;
auto& Object = reinterpret_cast<raii&>(Data);
auto& Object = edit_as<raii>(&Data);

int Value = 0;
placement::construct(Object, Value);
Expand Down
2 changes: 1 addition & 1 deletion far/common/io.hpp
Expand Up @@ -69,7 +69,7 @@ namespace io
{
try
{
Stream.read(static_cast<char*>(static_cast<void*>(Buffer.data())), Buffer.size());
Stream.read(edit_as<char*>(Buffer.data()), Buffer.size());
}
catch (std::ios::failure const&)
{
Expand Down
2 changes: 1 addition & 1 deletion far/common/nifty_counter.hpp
Expand Up @@ -75,6 +75,6 @@ namespace Instance##_nifty_objects\
}\
}\
\
Type& Instance = reinterpret_cast<Type&>(Instance##_nifty_objects::InitBuffer)
Type& Instance = *static_cast<Type*>(static_cast<void*>(&Instance##_nifty_objects::InitBuffer))

#endif // NIFTY_COUNTER_HPP_81EED24A_897B_4E3E_A23D_4117272E29D9
2 changes: 1 addition & 1 deletion far/common/smart_ptr.hpp
Expand Up @@ -161,7 +161,7 @@ class block_ptr: public array_ptr<std::byte, Size>
decltype(auto) data() const noexcept
{
assert(this->size() >= sizeof(T));
return static_cast<T*>(static_cast<void*>((array_ptr<std::byte, Size>::data())));
return edit_as<T*>(array_ptr<std::byte, Size>::data());
}

[[nodiscard]]
Expand Down

0 comments on commit 99c1e30

Please sign in to comment.