home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 31,764 of 33,346   
   Miles Bader to willgunx@gmail.com   
   Re: template and variable type   
   25 Dec 11 22:34:17   
   
   From: miles@gnu.org   
      
   m5i  writes:   
   > Although there exist solutions to this specific problem, I've also met   
   > several other situations when type information is needed in a function   
   > template, just like this one. So do I misuse template here? How can I   
   > write generic code for these problems?   
      
   As you don't give any examples, it's very hard to comment.   
      
   But surely you could use template args to pass in an indicator of the   
   C++ type in question...   
      
   A likely structure for such a template'd function might be something   
   like (warning: although I adapted this example from some working code   
   of mine, and it compiles, it's otherwise untested, and probably   
   contains many bugs!):   
      
      
      // Return true if the value on the Lua stack at index INDEX can be   
      // reasonably converted to type T.   
      //   
      template   
      bool lua_value_is_compatible_with (lua_State */*L*/, int /*index*/)   
      {   
        return false;   
      }   
      template<>   
      bool lua_value_is_compatible_with (lua_State *L, int index)   
      {   
        return lua_isnumber (L, index);   
      }   
      template<>   
      bool lua_value_is_compatible_with (lua_State *L, int index)   
      {   
        return lua_isnumber (L, index);   
      }   
      template<>   
      bool lua_value_is_compatible_with (lua_State *L, int index)   
      {   
        return lua_isstring (L, index);   
      }   
      // ... etc etc for every type you care about ...   
      
      
      // Return the value on the Lua stack at index INDEX converted to typ T.   
      //   
      template   
      T convert_lua_value (lua_State */*L*/, int /*index*/)   
      {   
        return false;   
      }   
      template<>   
      double convert_lua_value (lua_State *L, int index)   
      {   
        return lua_tonumber (L, index);   
      }   
      template<>   
      float convert_lua_value (lua_State *L, int index)   
      {   
        return lua_tonumber (L, index);   
      }   
      template<>   
      std::string convert_lua_value (lua_State *L, int index)   
      {   
        return lua_tostring (L, index);   
      }   
      // ... etc etc for every type you care about ...   
      
      
      
      // Reads the contents of the table at INDEX on the Lua stack into the   
      // vector VEC.   
      //   
      // Lua values will be cast into the type T.  If the object at INDEX is   
      // not a table, or the table contains non-numbers, an exception is   
      // raised.   
      //   
      template   
      void   
      convert_lua_vector (lua_State *L, int index, std::vector &vec)   
      {   
        if (! lua_istable (L, index))   
          throw std::runtime_error ("expected Lua table in convert_lua_vector");   
      
        size_t len = lua_objlen (L, index);   
        vec.reserve (vec.size() + len);   
      
        for (size_t i = 0; i < len; i++)   
          {   
            lua_rawgeti (L, index, i + 1);   
            if (! lua_value_is_compatible_with (L, -1))   
              {   
                lua_pop (L, 1);   
                throw std::runtime_error   
                  ("incompatible Lua value in convert_lua_vector");   
              }   
            vec.push_back (convert_lua_value (L, -1));   
            lua_pop (L, 1);   
          }   
      }   
      
      
   -miles   
      
      
   --   
   The key to happiness   
    is having dreams.      [from a fortune cookie]   
      
      
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca