Index: code/branches/pch/src/tolua/lua/declaration.lua
===================================================================
--- code/branches/pch/src/tolua/lua/declaration.lua	(revision 3114)
+++ code/branches/pch/src/tolua/lua/declaration.lua	(revision 3127)
@@ -140,11 +140,15 @@
         for i=1, table.getn(m) do
             m[i] = string.gsub(m[i],"%s*([%*&])", "%1")
-            m[i] = findtype(m[i]) or m[i]
-            m[i] = resolve_template_types(m[i])
+            if not isbasic(m[i]) then
+                if not isenum(m[i]) then _, m[i] = applytypedef("", m[i]) end
+                m[i] = findtype(m[i]) or m[i]
+                m[i] = resolve_template_types(m[i])
+            end
         end
 
         local b,i
         type,b,i = break_template(type)
-        local template_part = "<"..string.gsub(concat(m, 1, m.n), " ", ",")..">"
+        --print("concat is ",concat(m, 1, m.n))
+        local template_part = "<"..concat(m, 1, m.n, ",")..">"
         type = rebuild_template(type, b, template_part)
         type = string.gsub(type, ">>", "> >")
@@ -187,8 +191,8 @@
 -- check if array of values are returned to Lua
 function classDeclaration:requirecollection (t)
- if self.mod ~= 'const' and
+    if self.mod ~= 'const' and
         self.dim and self.dim ~= '' and
                  not isbasic(self.type) and
-                 self.ptr == '' then
+                 self.ptr == '' and self:check_public_access() then
         local type = gsub(self.type,"%s*const%s+","")
         t[type] = "tolua_collect_" .. clean_template(type)
@@ -222,11 +226,16 @@
         --    return 'tolua_isstringarray(tolua_S,'..narg..','..def..',&tolua_err)'
         --else
-        return 'tolua_istable(tolua_S,'..narg..',0,&tolua_err)'
+        return '!tolua_istable(tolua_S,'..narg..',0,&tolua_err)'
         --end
     elseif t then
-        return 'tolua_is'..t..'(tolua_S,'..narg..','..def..',&tolua_err)'
-    else
-        return 'tolua_isusertype(tolua_S,'..narg..',"'..self.type..'",'..def..',&tolua_err)'
-    end
+        return '!tolua_is'..t..'(tolua_S,'..narg..','..def..',&tolua_err)'
+    else
+        local is_func = get_is_function(self.type)
+        if self.ptr == '&' or self.ptr == '' then
+            return '(tolua_isvaluenil(tolua_S,'..narg..',&tolua_err) || !'..is_func..'(tolua_S,'..narg..',"'..self.type..'",'..def..',&tolua_err))'
+        else
+            return '!'..is_func..'(tolua_S,'..narg..',"'..self.type..'",'..def..',&tolua_err)'
+        end
+   end
 end
 
@@ -237,4 +246,5 @@
     local mod
     local type = self.type
+    local nctype = gsub(self.type,'const%s+','')
     if self.dim ~= '' then
         type = gsub(self.type,'const%s+','')  -- eliminates const modifier for arrays
@@ -251,5 +261,5 @@
         else
             if cplusplus then
-                line = concatparam(line,' = new',type,ptr,'['..self.dim..'];')
+                line = concatparam(line,' = Mtolua_new_dim(',type,ptr,', '..self.dim..');')
             else
                 line = concatparam(line,' = (',type,ptr,'*)',
@@ -273,5 +283,5 @@
             end
             line = concatparam(line,') ')
-            if isenum(type) then
+            if isenum(nctype) then
                 line = concatparam(line,'(int) ')
             end
@@ -286,5 +296,6 @@
                 line = concatparam(line,'tolua_to'..t,'(tolua_S,',narg,',',def,'));')
             else
-                line = concatparam(line,'tolua_tousertype(tolua_S,',narg,',',def,'));')
+                local to_func = get_to_function(type)
+                line = concatparam(line,to_func..'(tolua_S,',narg,',',def,'));')
             end
         end
@@ -361,5 +372,5 @@
                 output('   {')
                 output('#ifdef __cplusplus\n')
-                output('    void* tolua_obj = new',type,'(',self.name,'[i]);')
+                output('    void* tolua_obj = Mtolua_new((',type,')(',self.name,'[i]));')
                 output('    tolua_pushfieldusertype_and_takeownership(tolua_S,',narg,',i+1,tolua_obj,"',type,'");')
                 output('#else\n')
@@ -380,5 +391,5 @@
     if self.dim ~= '' and tonumber(self.dim)==nil then
         output('#ifdef __cplusplus\n')
-        output('  delete []',self.name,';')
+        output('  Mtolua_delete_dim(',self.name,');')
         output('#else\n')
         output('  free(',self.name,');')
@@ -405,5 +416,6 @@
             output('   tolua_push'..t..'(tolua_S,(',ct,')'..self.name..');')
         else
-            output('   tolua_pushusertype(tolua_S,(void*)'..self.name..',"',self.type,'");')
+            local push_func = get_push_function(self.type)
+            output('   ',push_func,'(tolua_S,(void*)'..self.name..',"',self.type,'");')
         end
         return 1
