Skip to content

Commit

Permalink
Revert "improve: replace type traits with STL"
Browse files Browse the repository at this point in the history
This reverts commit 859527a.
  • Loading branch information
mehah committed Jul 2, 2024
1 parent 859527a commit a354234
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 11 deletions.
17 changes: 9 additions & 8 deletions src/framework/luaengine/luabinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "luaexception.h"

#include <tuple>
#include <framework/stdext/traits.h>

/// This namespace contains some dirty metaprogamming that uses a lot of C++0x features
/// The purpose here is to create templates that can bind any function from C++
Expand Down Expand Up @@ -123,8 +124,8 @@ namespace luabinder
template<typename Ret, typename... Args>
LuaCppFunction bind_fun(const std::function<Ret(Args...)>& f)
{
using Tuple = std::tuple<std::remove_cvref_t<Args>...>;
return bind_fun_specializer<std::remove_cvref_t<Ret>,
using Tuple = std::tuple<typename stdext::remove_const_ref<Args>::type...>;
return bind_fun_specializer<typename stdext::remove_const_ref<Ret>::type,
decltype(f),
Tuple>(f);
}
Expand All @@ -138,8 +139,8 @@ namespace luabinder
{
static LuaCppFunction call(const Lambda& f)
{
using Tuple = std::tuple<std::remove_cvref_t<Args>...>;
return bind_fun_specializer<std::remove_cvref_t<Ret>,
using Tuple = std::tuple<typename stdext::remove_const_ref<Args>::type...>;
return bind_fun_specializer<typename stdext::remove_const_ref<Ret>::type,
decltype(f),
Tuple>(f);
}
Expand Down Expand Up @@ -199,9 +200,9 @@ namespace luabinder
template<typename C, typename Ret, class FC, typename... Args>
LuaCppFunction bind_mem_fun(Ret(FC::* f)(Args...))
{
using Tuple = std::tuple<std::shared_ptr<FC>, std::remove_cvref_t<Args>...>;
using Tuple = std::tuple<std::shared_ptr<FC>, typename stdext::remove_const_ref<Args>::type...>;
auto lambda = make_mem_func<Ret, FC>(f);
return bind_fun_specializer<std::remove_cvref_t<Ret>,
return bind_fun_specializer<typename stdext::remove_const_ref<Ret>::type,
decltype(lambda),
Tuple>(lambda);
}
Expand All @@ -210,10 +211,10 @@ namespace luabinder
template<typename C, typename Ret, class FC, typename... Args>
LuaCppFunction bind_singleton_mem_fun(Ret(FC::* f)(Args...), C* instance)
{
using Tuple = std::tuple<std::remove_cvref_t<Args>...>;
using Tuple = std::tuple<typename stdext::remove_const_ref<Args>::type...>;
assert(instance);
auto lambda = make_mem_func_singleton<Ret, FC>(f, static_cast<FC*>(instance));
return bind_fun_specializer<std::remove_cvref_t<Ret>,
return bind_fun_specializer<typename stdext::remove_const_ref<Ret>::type,
decltype(lambda),
Tuple>(lambda);
}
Expand Down
6 changes: 3 additions & 3 deletions src/framework/stdext/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#pragma once

#include "traits.h"

#include <cassert>
#include <cstdio>
#include <cstring>
Expand Down Expand Up @@ -68,12 +70,11 @@ namespace stdext
}
};

#ifndef _MSC_VER
// Improved snprintf that accepts std::string and other types
template<typename... Args>
int snprintf(char* s, size_t maxlen, const char* format, const Args&... args)
{
std::tuple<std::decay_t<Args>...> tuple(args...);
std::tuple<typename replace_extent<Args>::type...> tuple(args...);
return expand_snprintf<std::tuple_size_v<decltype(tuple)>>::call(s, maxlen, format, tuple);
}

Expand All @@ -84,7 +85,6 @@ namespace stdext
s[maxlen - 1] = 0;
return strlen(s);
}
#endif

template<typename... Args>
std::string format() { return {}; }
Expand Down
45 changes: 45 additions & 0 deletions src/framework/stdext/traits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2010-2022 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#pragma once

#include <type_traits>

namespace stdext
{
template<class T> struct replace_extent
{
using type = T;
};
template<class T> struct replace_extent<T[]>
{
using type = const T*;
};
template<class T, uint64_t N> struct replace_extent<T[N]>
{
using type = const T*;
};
template<typename T> struct remove_const_ref
{
using type = std::remove_const_t<std::remove_reference_t<T>>;
};
};
1 change: 1 addition & 0 deletions vc17/otclient.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ cmd /c "start ../vcpkg_installed\$(VcpkgTriplet)\$(VcpkgTriplet)\tools\protobuf\
<ClInclude Include="..\src\framework\stdext\stdext.h" />
<ClInclude Include="..\src\framework\stdext\string.h" />
<ClInclude Include="..\src\framework\stdext\time.h" />
<ClInclude Include="..\src\framework\stdext\traits.h" />
<ClInclude Include="..\src\framework\stdext\types.h" />
<ClInclude Include="..\src\framework\stdext\uri.h" />
<ClInclude Include="..\src\framework\ui\declarations.h" />
Expand Down
3 changes: 3 additions & 0 deletions vc17/otclient.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,9 @@
<ClInclude Include="..\src\framework\stdext\time.h">
<Filter>Header Files\framework\stdext</Filter>
</ClInclude>
<ClInclude Include="..\src\framework\stdext\traits.h">
<Filter>Header Files\framework\stdext</Filter>
</ClInclude>
<ClInclude Include="..\src\framework\stdext\types.h">
<Filter>Header Files\framework\stdext</Filter>
</ClInclude>
Expand Down

0 comments on commit a354234

Please sign in to comment.