refactor going on

This commit is contained in:
Mikulas Florek 2019-07-25 19:14:12 +02:00
parent 5822651aba
commit 7540f5cb58
14 changed files with 65 additions and 80 deletions

View file

@ -70,7 +70,7 @@ static void shortcutInput(OS::Keycode& shortcut)
popup_name << (i64)&shortcut;
char tmp[32];
OS::getKeyName(shortcut, tmp, sizeof(tmp));
OS::getKeyName(shortcut, Span(tmp));
StaticString<50> button_label(tmp[0] || shortcut == OS::Keycode::INVALID ? tmp : "Unknown");
button_label << "###" << (i64)&shortcut;
@ -188,7 +188,7 @@ bool Settings::load()
return false;
}
StringView content((const char*)buf.begin(), buf.size());
Span<const char> content((const char*)buf.begin(), buf.size());
if (!LuaWrapper::execute(L, content, "settings", 0)) {
logError("Editor") << path << ": " << lua_tostring(L, -1);
lua_pop(L, 1);

View file

@ -227,7 +227,7 @@ public:
case OS::Event::Type::DROP_FILE:
for(int i = 0, c = OS::getDropFileCount(event); i < c; ++i) {
char tmp[MAX_PATH_LENGTH];
OS::getDropFile(event, i, tmp, lengthOf(tmp));
OS::getDropFile(event, i, Span(tmp));
for (GUIPlugin* plugin : m_gui_plugins) {
if (plugin->onDropFile(tmp)) break;
}
@ -286,7 +286,7 @@ public:
}
char current_dir[MAX_PATH_LENGTH];
OS::getCurrentDirectory(current_dir, lengthOf(current_dir));
OS::getCurrentDirectory(Span(current_dir));
char data_dir[MAX_PATH_LENGTH] = {};
checkDataDirCommandLine(data_dir, lengthOf(data_dir));
@ -873,7 +873,7 @@ public:
buf[0] = 0;
for (int i = 0; i < lengthOf(action.shortcut); ++i) {
char tmp[64];
OS::getKeyName(action.shortcut[i], tmp, sizeof(tmp));
OS::getKeyName(action.shortcut[i], Span(tmp));
if (tmp[0] == 0) return;
if (i > 0) catString(buf, " - ");
catString(buf, tmp);
@ -1690,7 +1690,7 @@ public:
{
logInfo("Editor") << "Loading settings...";
char cmd_line[2048];
OS::getCommandLine(cmd_line, lengthOf(cmd_line));
OS::getCommandLine(Span(cmd_line));
CommandLineParser parser(cmd_line);
while (parser.next())
@ -1802,7 +1802,7 @@ public:
static bool copyPlugin(const char* src, int iteration, char (&out)[MAX_PATH_LENGTH])
{
char tmp_path[MAX_PATH_LENGTH];
OS::getExecutablePath(tmp_path, lengthOf(tmp_path));
OS::getExecutablePath(Span(tmp_path));
StaticString<MAX_PATH_LENGTH> copy_path;
PathUtils::getDir(Span(copy_path.data), tmp_path);
copy_path << "plugins/" << iteration;
@ -1837,7 +1837,7 @@ public:
void loadUserPlugins()
{
char cmd_line[2048];
OS::getCommandLine(cmd_line, lengthOf(cmd_line));
OS::getCommandLine(Span(cmd_line));
CommandLineParser parser(cmd_line);
auto& plugin_manager = m_editor->getEngine().getPluginManager();
@ -1966,7 +1966,7 @@ public:
bool shouldSleepWhenInactive()
{
char cmd_line[2048];
OS::getCommandLine(cmd_line, lengthOf(cmd_line));
OS::getCommandLine(Span(cmd_line));
CommandLineParser parser(cmd_line);
while (parser.next())
@ -1981,7 +1981,7 @@ public:
{
char cmd_line[2048];
char path[MAX_PATH_LENGTH];
OS::getCommandLine(cmd_line, lengthOf(cmd_line));
OS::getCommandLine(Span(cmd_line));
CommandLineParser parser(cmd_line);
while (parser.next())
@ -2000,7 +2000,7 @@ public:
static void checkDataDirCommandLine(char* dir, int max_size)
{
char cmd_line[2048];
OS::getCommandLine(cmd_line, lengthOf(cmd_line));
OS::getCommandLine(Span(cmd_line));
CommandLineParser parser(cmd_line);
while (parser.next())
@ -2326,7 +2326,7 @@ public:
void checkScriptCommandLine()
{
char command_line[1024];
OS::getCommandLine(command_line, lengthOf(command_line));
OS::getCommandLine(Span(command_line));
CommandLineParser parser(command_line);
while (parser.next())
{
@ -2570,7 +2570,7 @@ public:
if (!OS::fileExists("bin/app.exe"))
{
char tmp[MAX_PATH_LENGTH];
OS::getExecutablePath(tmp, lengthOf(tmp));
OS::getExecutablePath(Span(tmp));
PathUtils::getDir(Span(src_dir.data), tmp);
}
for (auto& file : bin_files)

View file

@ -13,8 +13,8 @@ namespace Lumix
struct LUMIX_EDITOR_API ResourceLocator {
ResourceLocator(const char* path);
StringView name;
StringView filepath;
Span<const char> name;
Span<const char> filepath;
char full[MAX_PATH_LENGTH];
};

View file

@ -2742,7 +2742,7 @@ public:
m_editor_icons = EditorIcons::create(*this);
char command_line[2048];
OS::getCommandLine(command_line, lengthOf(command_line));
OS::getCommandLine(Span(command_line));
CommandLineParser parser(command_line);
while (parser.next())
{

View file

@ -87,7 +87,7 @@ inline bool pcall(lua_State* L, int nargs)
inline bool execute(lua_State* L
, StringView content
, Span<const char> content
, const char* name
, int nresults)
{

View file

@ -151,7 +151,7 @@ LUMIX_ENGINE_API void destroyFileIterator(FileIterator* iterator);
LUMIX_ENGINE_API bool getNextFile(FileIterator* iterator, FileInfo* info);
LUMIX_ENGINE_API void setCurrentDirectory(const char* path);
LUMIX_ENGINE_API void getCurrentDirectory(char* buffer, int buffer_size);
LUMIX_ENGINE_API void getCurrentDirectory(Span<char> path);
LUMIX_ENGINE_API bool getOpenFilename(Span<char> out, const char* filter, const char* starting_file);
LUMIX_ENGINE_API bool getSaveFilename(Span<char> out, const char* filter, const char* default_extension);
LUMIX_ENGINE_API bool getOpenDirectory(Span<char> out, const char* starting_dir);
@ -171,7 +171,7 @@ LUMIX_ENGINE_API void unclipCursor();
LUMIX_ENGINE_API void quit();
LUMIX_ENGINE_API void getDropFile(const Event& event, int idx, char* out, int max_size);
LUMIX_ENGINE_API void getDropFile(const Event& event, int idx, Span<char> out);
LUMIX_ENGINE_API int getDropFileCount(const Event& event);
LUMIX_ENGINE_API void finishDrag(const Event& event);
@ -191,15 +191,15 @@ LUMIX_ENGINE_API bool isMaximized(WindowHandle win);
LUMIX_ENGINE_API WindowHandle getFocused();
LUMIX_ENGINE_API bool isKeyDown(Keycode keycode);
LUMIX_ENGINE_API void getKeyName(Keycode keycode, char* out, int size);
LUMIX_ENGINE_API void getKeyName(Keycode keycode, Span<char> out);
LUMIX_ENGINE_API int getDPI();
LUMIX_ENGINE_API void UTF32ToUTF8(u32 utf32, char* utf8);
LUMIX_ENGINE_API bool copyFile(const char* from, const char* to);
LUMIX_ENGINE_API void getExecutablePath(char* buffer, int buffer_size);
LUMIX_ENGINE_API void getExecutablePath(Span<char> path);
LUMIX_ENGINE_API void messageBox(const char* text);
LUMIX_ENGINE_API void setCommandLine(int, char**);
LUMIX_ENGINE_API bool getCommandLine(char* output, int max_size);
LUMIX_ENGINE_API bool getCommandLine(Span<char> output);
LUMIX_ENGINE_API void* loadLibrary(const char* path);
LUMIX_ENGINE_API void unloadLibrary(void* handle);
LUMIX_ENGINE_API void* getLibrarySymbol(void* handle, const char* name);

View file

@ -26,12 +26,12 @@ String::String(const String& rhs, int start, i32 length)
}
String::String(const char* rhs, i32 length, IAllocator& allocator)
String::String(Span<const char> rhs, IAllocator& allocator)
: m_allocator(allocator)
{
m_size = length;
m_size = rhs.length();
m_cstr = (char*)m_allocator.allocate((m_size + 1) * sizeof(char));
copyMemory(m_cstr, rhs, m_size * sizeof(char));
copyMemory(m_cstr, rhs.begin, m_size * sizeof(char));
m_cstr[m_size] = 0;
}
@ -173,15 +173,10 @@ void String::resize(int size)
}
String& String::cat(const StringView& value)
String& String::cat(Span<const char> value)
{
return cat(value.begin, value.length());
}
String& String::cat(const char* value, int length)
{
if (value < m_cstr || value >= m_cstr + m_size)
const u32 length = value.length();
if (value.begin < m_cstr || value.begin >= m_cstr + m_size)
{
if (m_cstr)
{
@ -191,13 +186,13 @@ String& String::cat(const char* value, int length)
m_allocator.deallocate(m_cstr);
m_cstr = new_cstr;
m_size = new_size;
catNString(Span(m_cstr, m_size + 1), value, length);
catNString(Span(m_cstr, m_size + 1), value.begin, length);
}
else
{
m_size = length;
m_cstr = (char*)m_allocator.allocate(m_size + 1);
copyNString(Span(m_cstr, m_size + 1), value, length);
copyNString(Span(m_cstr, m_size + 1), value.begin, length);
}
}
return *this;

View file

@ -78,16 +78,6 @@ template <int SIZE> bool catString(char(&destination)[SIZE], const char* source)
}
struct StringView : Span<const char>
{
using Span<const char>::Span;
StringView(const char* begin)
: Span<const char>(begin, stringLength(begin))
{
}
};
template <int SIZE> struct StaticString
{
StaticString() { data[0] = '\0'; }
@ -116,7 +106,7 @@ template <int SIZE> struct StaticString
template <int value_size> void add(StaticString<value_size>& value) { catString(data, value.data); }
void add(const char* value) { catString(data, value); }
void add(char* value) { catString(data, value); }
void add(const StringView& value) { catNString(Span(data), value.begin, value.length()); }
void add(const Span<const char>& value) { catNString(Span(data), value.begin, value.length()); }
void operator=(const char* str) { copyString(data, str); }
@ -167,7 +157,7 @@ class LUMIX_ENGINE_API String
public:
explicit String(IAllocator& allocator);
String(const String& rhs, int start, i32 length);
String(const char* rhs, i32 length, IAllocator& allocator);
String(Span<const char> rhs, IAllocator& allocator);
String(const String& rhs);
String(String&& rhs);
String(const char* rhs, IAllocator& allocator);
@ -188,9 +178,8 @@ public:
int length() const { return m_size; }
const char* c_str() const { return m_cstr; }
String substr(int start, int length) const;
String& cat(const char* value, int length);
String& cat(Span<const char> value);
String& cat(float value);
String& cat(const StringView& value);
String& cat(char* value);
String& cat(const char* value);
void insert(int position, const char* value);

View file

@ -184,10 +184,11 @@ OutputFile& OutputFile::operator <<(float value)
}
static void fromWChar(char* out, int size, const WCHAR* in)
static void fromWChar(Span<char> out, const WCHAR* in)
{
const WCHAR* c = in;
char* cout = out;
char* cout = out.begin;
const u32 size = out.length();
while (*c && c - in < size - 1)
{
*cout = (char)*c;
@ -229,14 +230,14 @@ struct WCharStr
};
void getDropFile(const Event& event, int idx, char* out, int max_size)
void getDropFile(const Event& event, int idx, Span<char> out)
{
ASSERT(max_size > 0);
ASSERT(out.length() > 0);
HDROP drop = (HDROP)event.file_drop.handle;
WCHAR buffer[MAX_PATH];
if (DragQueryFile(drop, idx, buffer, MAX_PATH))
{
fromWChar(out, max_size, buffer);
fromWChar(out, buffer);
}
else
{
@ -511,7 +512,7 @@ bool isKeyDown(Keycode keycode)
}
void getKeyName(Keycode keycode, char* out, int size)
void getKeyName(Keycode keycode, Span<char> out)
{
LONG scancode = MapVirtualKey((UINT)keycode, MAPVK_VK_TO_VSC);
@ -533,15 +534,14 @@ void getKeyName(Keycode keycode, char* out, int size)
}
WCHAR tmp[256];
u32 size = out.length();
ASSERT(size <= 256 && size > 0);
int res = GetKeyNameText(scancode << 16, tmp, size);
if (res == 0)
{
*out = 0;
if (res == 0) {
out[0] = 0;
}
else
{
fromWChar(out, size, tmp);
else {
fromWChar(out, tmp);
}
}
@ -704,7 +704,7 @@ bool getNextFile(FileIterator* iterator, FileInfo* info)
if (!iterator->is_valid) return false;
info->is_directory = (iterator->ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
fromWChar(info->filename, lengthOf(info->filename), iterator->ffd.cFileName);
fromWChar(Span(info->filename), iterator->ffd.cFileName);
iterator->is_valid = FindNextFile(iterator->handle, &iterator->ffd) != FALSE;
return true;
@ -718,11 +718,11 @@ void setCurrentDirectory(const char* path)
}
void getCurrentDirectory(char* buffer, int buffer_size)
void getCurrentDirectory(Span<char> output)
{
WCHAR tmp[MAX_PATH_LENGTH];
GetCurrentDirectory(lengthOf(tmp), tmp);
fromWChar(buffer, buffer_size, tmp);
fromWChar(output, tmp);
}
@ -761,7 +761,7 @@ bool getSaveFilename(Span<char> out, const char* filter, const char* default_ext
bool res = GetSaveFileName(&ofn) != FALSE;
char tmp[MAX_PATH_LENGTH];
fromWChar(tmp, lengthOf(tmp), wtmp);
fromWChar(Span(tmp), wtmp);
if (res) PathUtils::normalize(tmp, out);
return res;
}
@ -813,7 +813,7 @@ bool getOpenFilename(Span<char> out, const char* filter, const char* starting_fi
const bool res = GetOpenFileName(&ofn) != FALSE;
if (res) {
char tmp[MAX_PATH_LENGTH];
fromWChar(tmp, lengthOf(tmp), wout);
fromWChar(Span(tmp), wout);
PathUtils::normalize(tmp, out);
}
else {
@ -1049,11 +1049,11 @@ bool copyFile(const char* from, const char* to)
}
void getExecutablePath(char* buffer, int buffer_size)
void getExecutablePath(Span<char> buffer)
{
WCHAR tmp[MAX_PATH];
toWChar(tmp, buffer);
GetModuleFileName(NULL, tmp, buffer_size);
GetModuleFileName(NULL, tmp, sizeof(tmp));
fromWChar(buffer, tmp);
}
@ -1071,10 +1071,10 @@ void setCommandLine(int, char**)
}
bool getCommandLine(char* output, int max_size)
bool getCommandLine(Span<char> output)
{
WCHAR* cl = GetCommandLine();
fromWChar(output, max_size, cl);
fromWChar(output, cl);
return true;
}

View file

@ -735,12 +735,13 @@ namespace Lumix
}
static void convertPropertyToLuaName(const char* src, char* out, int max_size)
static void convertPropertyToLuaName(const char* src, Span<char> out)
{
const u32 max_size = out.length();
ASSERT(max_size > 0);
bool to_upper = true;
char* dest = out;
while (*src && dest - out < max_size - 1)
char* dest = out.begin;
while (*src && dest - out.begin < max_size - 1)
{
if (isLetter(*src))
{
@ -772,7 +773,7 @@ namespace Lumix
char tmp[50];
char setter[50];
char getter[50];
convertPropertyToLuaName(prop.name, tmp, lengthOf(tmp));
convertPropertyToLuaName(prop.name, Span(tmp));
copyString(setter, "set");
copyString(getter, "get");
catString(setter, tmp);
@ -817,7 +818,7 @@ namespace Lumix
lua_newtable(L);
lua_pushvalue(L, -1);
char tmp[50];
convertPropertyToLuaName(cmp_name, tmp, lengthOf(tmp));
convertPropertyToLuaName(cmp_name, Span(tmp));
lua_setglobal(L, tmp);
ComponentType cmp_type = Reflection::getComponentType(cmp_name);

View file

@ -365,7 +365,7 @@ struct PhysicsUIPlugin final : public StudioApp::GUIPlugin
bool packData(const char* dest_dir) override
{
char exe_path[MAX_PATH_LENGTH];
OS::getExecutablePath(exe_path, lengthOf(exe_path));
OS::getExecutablePath(Span(exe_path));
char exe_dir[MAX_PATH_LENGTH];
const char* physx_dlls[] = {

View file

@ -801,7 +801,7 @@ bool Material::load(u64 size, const u8* mem)
m_custom_flags = 0;
const StringView content((const char*)mem, (int)size);
const Span<const char> content((const char*)mem, (int)size);
if (!LuaWrapper::execute(L, content, getPath().c_str(), 0)) {
lua_close(L);
return false;

View file

@ -431,7 +431,7 @@ struct RendererImpl final : public Renderer
{
registerProperties(m_engine.getAllocator());
char cmd_line[4096];
OS::getCommandLine(cmd_line, lengthOf(cmd_line));
OS::getCommandLine(Span(cmd_line));
CommandLineParser cmd_line_parser(cmd_line);
m_vsync = true;
while (cmd_line_parser.next())

View file

@ -428,7 +428,7 @@ bool Shader::load(u64 size, const u8* mem)
lua_pushinteger(L, (int)Mesh::AttributeSemantic::BITANGENT);
lua_setglobal(L, "SEMANTICS_BITANGENT");
const StringView content((const char*)mem, (int)size);
const Span<const char> content((const char*)mem, (int)size);
if (!LuaWrapper::execute(L, content, getPath().c_str(), 0)) {
lua_close(L);
return false;