Commits

wlav committed 231bc6a

advanced template support and made gbl a true namespace

Comments (0)

Files changed (5)

pypy/module/cppyy/interp_cppyy.py

         self.handle = handle
 
     def __call__(self, args_w):
+        # TODO: this is broken but unused (see pythonify.py)
         fullname = "".join([self.name, '<', self.space.str_w(args_w[0]), '>'])
         return type_byname(self.space, fullname)
 

pypy/module/cppyy/pythonify.py

         self._scope = scope
         self._name = name
 
+    def _arg_to_str(self, arg):
+        if type(arg) != str:
+            arg = arg.__name__
+        return arg
+
     def __call__(self, *args):
-        fullname = "".join([self._name, '<', str(args[0]), '>'])
+        fullname = ''.join(
+            [self._name, '<', ','.join(map(self._arg_to_str, args))])
+        if fullname[-1] == '>':
+            fullname += ' >'
+        else:
+            fullname += '>'
         return getattr(self._scope, fullname)
 
 class CppyyObject(object):
 
 _existing_cppitems = {}               # to merge with gbl.__dict__ (?)
 def get_cppitem(name, scope=None):
-    if scope:
+    if scope and not scope is gbl:
         fullname = scope.__name__+"::"+name
     else:
         fullname = name
 get_cppclass = get_cppitem         # TODO: restrict to classes only (?)
 
 
-class _gbl(object): # TODO: make a CppyyNamespace object
-    """Global C++ namespace, i.e. ::."""
-
-    def __getattr__(self, attr):
-        try:
-            cppitem = get_cppitem(attr)
-            self.__dict__[attr] = cppitem
-            return cppitem
-        except TypeError:
-            raise AttributeError("'gbl' object has no attribute '%s'" % attr)
-
-
 _loaded_shared_libs = {}
 def load_lib(name):
     try:
     
 
 # user interface objects
-gbl = _gbl()
+gbl = make_cppnamespace("::", cppyy._type_byname(""))    # global C++ namespace

pypy/module/cppyy/test/Makefile

 	$(genreflex) datatypes.h $(genreflexflags)
 	g++ -o $@ datatypes_rflx.cpp datatypes.cxx -shared -lReflex $(cppflags) $(cppflags2)
 
-advancedcppDict.so: advancedcpp.cxx advancedcpp.h
-	$(genreflex) advancedcpp.h $(genreflexflags)
+advancedcppDict.so: advancedcpp.cxx advancedcpp.h advancedcpp.xml
+	$(genreflex) advancedcpp.h $(genreflexflags) --selection=advancedcpp.xml
 	g++ -o $@ advancedcpp_rflx.cpp advancedcpp.cxx -shared -lReflex $(cppflags) $(cppflags2)
 
 stltypesDict.so: stltypes.cxx stltypes.h stltypes.xml

pypy/module/cppyy/test/advancedcpp.h

 
 
 //===========================================================================
-template< typename T >             // for template testing
+template<typename T>               // for template testing
 class T1 {
 public:
-   T1( T t = T(0) ) : m_t1( t ) {}
+   T1(T t = T(1)) : m_t1(t) {}
    T value() { return m_t1; }
 
 public:
    T m_t1;
 };
 
-template< typename T >
+template<typename T>
 class T2 {
 public:
+   T2(T t = T(2)) : m_t2(t) {}
+   T value() { return m_t2; }
+
+public:
    T m_t2;
 };
 
-namespace {
-   T1< int > tt1;
-   T2< T1< int > > tt2;
-}
+template<typename T, typename U>
+class T3 {
+public:
+   T3(T t = T(3), U u = U(33)) : m_t3(t), m_u3(u) {}
+   T value_t() { return m_t3; }
+   U value_u() { return m_u3; }
 
-// helpers for checking pass-by-ref
+public:
+   T m_t3;
+   U m_u3;
+};
+
+namespace a_ns {
+
+   template<typename T>
+   class T4 {
+   public:
+      T4(T t = T(4)) : m_t4(t) {}
+      T value() { return m_t4; }
+
+   public:
+      T m_t4;
+   };
+
+} // namespace a_ns
+
+template class T1<int>;
+template class T2<T1<int> >;
+template class T3<int, double>;
+template class T3<T1<int>, T2<T1<int> > >;
+template class a_ns::T4<int>;
+template class a_ns::T4<a_ns::T4<T3<int, double> > >;
+
+
+//===========================================================================
+// for checking pass-by-reference of builtin types
 void set_int_through_ref(int& i, int val);
 int pass_int_through_const_ref(const int& i);
 void set_long_through_ref(long& l, long val);

pypy/module/cppyy/test/test_advancedcpp.py

         """Test access to namespaces and inner classes"""
 
         import cppyy
+        gbl = cppyy.gbl
 
-# TODO: have Reflex add the globals to the dictionary ...
-#        assert cppyy.gbl.a_ns.g_a                           == 11
-        assert cppyy.gbl.a_ns.b_class.s_b                   == 22
-        assert cppyy.gbl.a_ns.b_class().m_b                 == -2
-        assert cppyy.gbl.a_ns.b_class.c_class.s_c           == 33
-        assert cppyy.gbl.a_ns.b_class.c_class().m_c         == -3
-#        assert cppyy.gbl.a_ns.d_ns.g_d                      == 44
-        assert cppyy.gbl.a_ns.d_ns.e_class.s_e              == 55
-        assert cppyy.gbl.a_ns.d_ns.e_class().m_e            == -5
-        assert cppyy.gbl.a_ns.d_ns.e_class.f_class.s_f      == 66
-        assert cppyy.gbl.a_ns.d_ns.e_class.f_class().m_f    == -6
+        assert gbl.a_ns.g_a                           == 11
+        assert gbl.a_ns.b_class.s_b                   == 22
+        assert gbl.a_ns.b_class().m_b                 == -2
+        assert gbl.a_ns.b_class.c_class.s_c           == 33
+        assert gbl.a_ns.b_class.c_class().m_c         == -3
+        assert gbl.a_ns.d_ns.g_d                      == 44
+        assert gbl.a_ns.d_ns.e_class.s_e              == 55
+        assert gbl.a_ns.d_ns.e_class().m_e            == -5
+        assert gbl.a_ns.d_ns.e_class.f_class.s_f      == 66
+        assert gbl.a_ns.d_ns.e_class.f_class().m_f    == -6
 
-        assert cppyy.gbl.a_ns      is cppyy.gbl.a_ns
-        assert cppyy.gbl.a_ns.d_ns is cppyy.gbl.a_ns.d_ns
+        assert gbl.a_ns      is gbl.a_ns
+        assert gbl.a_ns.d_ns is gbl.a_ns.d_ns
 
-        assert cppyy.gbl.a_ns.b_class              is cppyy.gbl.a_ns.b_class
-        assert cppyy.gbl.a_ns.d_ns.e_class         is cppyy.gbl.a_ns.d_ns.e_class
-        assert cppyy.gbl.a_ns.d_ns.e_class.f_class is cppyy.gbl.a_ns.d_ns.e_class.f_class
+        assert gbl.a_ns.b_class              is gbl.a_ns.b_class
+        assert gbl.a_ns.d_ns.e_class         is gbl.a_ns.d_ns.e_class
+        assert gbl.a_ns.d_ns.e_class.f_class is gbl.a_ns.d_ns.e_class.f_class
+
+    def test03_template_types(self):
+        """Test bindings of templated types"""
+
+        import cppyy
+        gbl = cppyy.gbl
+
+        assert gbl.T1 is gbl.T1
+        assert gbl.T2 is gbl.T2
+        assert gbl.T3 is gbl.T3
+        assert not gbl.T1 is gbl.T2
+        assert not gbl.T2 is gbl.T3
+
+        assert gbl.T1('int') is gbl.T1('int')
+        assert gbl.T1(int)   is gbl.T1('int')
+        assert gbl.T2('T1<int>')     is gbl.T2('T1<int>')
+        assert gbl.T2(gbl.T1('int')) is gbl.T2('T1<int>')
+        assert gbl.T3('int,double')    is gbl.T3('int,double')
+        assert gbl.T3('int', 'double') is gbl.T3('int,double')
+        assert gbl.T3(int, 'double')   is gbl.T3('int,double')
+        assert gbl.T3('T1<int>,T2<T1<int> >') is gbl.T3('T1<int>,T2<T1<int> >')
+        assert gbl.T3('T1<int>', gbl.T2(gbl.T1(int))) is gbl.T3('T1<int>,T2<T1<int> >')
+
+        assert gbl.a_ns.T4(int) is gbl.a_ns.T4('int')
+        assert gbl.a_ns.T4('a_ns::T4<T3<int,double> >')\
+               is gbl.a_ns.T4(gbl.a_ns.T4(gbl.T3(int, 'double')))
+
+        t1 = gbl.T1(int)()
+        assert t1.m_t1    == 1
+        assert t1.value() == 1
+        t1.destruct()
+
+        t1 = gbl.T1(int)(11)
+        assert t1.m_t1    == 11
+        assert t1.value() == 11
+        t1.m_t1 = 111
+        assert t1.value() == 111
+        t1.destruct()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.