?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. // https://github.com/vinniefalco/LuaBridge
  2. // Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
  3. // Copyright 2007, Nathan Reed
  4. // SPDX-License-Identifier: MIT
  5.  
  6. //==============================================================================
  7. /*
  8.   This file incorporates work covered by the following copyright and
  9.   permission notice:
  10.  
  11.     The Loki Library
  12.     Copyright (c) 2001 by Andrei Alexandrescu
  13.     This code accompanies the book:
  14.     Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
  15.         Patterns Applied". Copyright (c) 2001. Addison-Wesley.
  16.     Permission to use, copy, modify, distribute and sell this software for any
  17.         purpose is hereby granted without fee, provided that the above copyright
  18.         notice appear in all copies and that both that copyright notice and this
  19.         permission notice appear in supporting documentation.
  20.     The author or Addison-Welsey Longman make no representations about the
  21.         suitability of this software for any purpose. It is provided "as is"
  22.         without express or implied warranty.
  23. */
  24. //==============================================================================
  25.  
  26. #pragma once
  27.  
  28. #include <LuaBridge/detail/Config.h>
  29. #include <LuaBridge/detail/Stack.h>
  30.  
  31. #include <string>
  32. #include <typeinfo>
  33.  
  34. namespace luabridge {
  35.  
  36. namespace detail {
  37.  
  38. /**
  39.   None type means void parameters or return value.
  40. */
  41. typedef void None;
  42.  
  43. template<typename Head, typename Tail = None>
  44. struct TypeList
  45. {
  46.     typedef Tail TailType;
  47. };
  48.  
  49. template<class List>
  50. struct TypeListSize
  51. {
  52.     static const size_t value = TypeListSize<typename List::TailType>::value + 1;
  53. };
  54.  
  55. template<>
  56. struct TypeListSize<None>
  57. {
  58.     static const size_t value = 0;
  59. };
  60.  
  61. template<class... Params>
  62. struct MakeTypeList;
  63.  
  64. template<class Param, class... Params>
  65. struct MakeTypeList<Param, Params...>
  66. {
  67.     using Result = TypeList<Param, typename MakeTypeList<Params...>::Result>;
  68. };
  69.  
  70. template<>
  71. struct MakeTypeList<>
  72. {
  73.     using Result = None;
  74. };
  75.  
  76. /**
  77.   A TypeList with actual values.
  78. */
  79. template<typename List>
  80. struct TypeListValues
  81. {
  82.     static std::string const tostring(bool) { return ""; }
  83. };
  84.  
  85. /**
  86.   TypeListValues recursive template definition.
  87. */
  88. template<typename Head, typename Tail>
  89. struct TypeListValues<TypeList<Head, Tail>>
  90. {
  91.     Head hd;
  92.     TypeListValues<Tail> tl;
  93.  
  94.     TypeListValues(Head hd_, TypeListValues<Tail> const& tl_) : hd(hd_), tl(tl_) {}
  95.  
  96.     static std::string tostring(bool comma = false)
  97.     {
  98.         std::string s;
  99.  
  100.         if (comma)
  101.             s = ", ";
  102.  
  103.         s = s + typeid(Head).name();
  104.  
  105.         return s + TypeListValues<Tail>::tostring(true);
  106.     }
  107. };
  108.  
  109. // Specializations of type/value list for head types that are references and
  110. // const-references.  We need to handle these specially since we can't count
  111. // on the referenced object hanging around for the lifetime of the list.
  112.  
  113. template<typename Head, typename Tail>
  114. struct TypeListValues<TypeList<Head&, Tail>>
  115. {
  116.     Head hd;
  117.     TypeListValues<Tail> tl;
  118.  
  119.     TypeListValues(Head& hd_, TypeListValues<Tail> const& tl_) : hd(hd_), tl(tl_) {}
  120.  
  121.     static std::string const tostring(bool comma = false)
  122.     {
  123.         std::string s;
  124.  
  125.         if (comma)
  126.             s = ", ";
  127.  
  128.         s = s + typeid(Head).name() + "&";
  129.  
  130.         return s + TypeListValues<Tail>::tostring(true);
  131.     }
  132. };
  133.  
  134. template<typename Head, typename Tail>
  135. struct TypeListValues<TypeList<Head const&, Tail>>
  136. {
  137.     Head hd;
  138.     TypeListValues<Tail> tl;
  139.  
  140.     TypeListValues(Head const& hd_, const TypeListValues<Tail>& tl_) : hd(hd_), tl(tl_) {}
  141.  
  142.     static std::string const tostring(bool comma = false)
  143.     {
  144.         std::string s;
  145.  
  146.         if (comma)
  147.             s = ", ";
  148.  
  149.         s = s + typeid(Head).name() + " const&";
  150.  
  151.         return s + TypeListValues<Tail>::tostring(true);
  152.     }
  153. };
  154.  
  155. //==============================================================================
  156. /**
  157.   Subclass of a TypeListValues constructable from the Lua stack.
  158. */
  159.  
  160. template<typename List, int Start = 1>
  161. struct ArgList
  162. {
  163. };
  164.  
  165. template<int Start>
  166. struct ArgList<None, Start> : public TypeListValues<None>
  167. {
  168.     ArgList(lua_State*) {}
  169. };
  170.  
  171. template<typename Head, typename Tail, int Start>
  172. struct ArgList<TypeList<Head, Tail>, Start> : public TypeListValues<TypeList<Head, Tail>>
  173. {
  174.     ArgList(lua_State* L)
  175.         : TypeListValues<TypeList<Head, Tail>>(Stack<Head>::get(L, Start),
  176.                                                ArgList<Tail, Start + 1>(L))
  177.     {
  178.     }
  179. };
  180.  
  181. } // namespace detail
  182.  
  183. } // namespace luabridge
  184.