Skip to content

Commit

Permalink
(memoized) convenience functions for typeid and sizeof, and some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wlav committed Aug 6, 2017
1 parent b7bcd2b commit ecaffa4
Show file tree
Hide file tree
Showing 4 changed files with 327 additions and 288 deletions.
33 changes: 33 additions & 0 deletions python/cppyy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,36 @@ def cppdef(src):

def include(header):
gbl.gInterpreter.ProcessLine('#include "%s"' % header)

def _get_name(tt):
try:
ttname = tt.__cppname__
except AttributeError:
ttname = tt.__name__
return ttname

_sizes = {}
def sizeof(tt):
if not isinstance(tt, type):
tt = type(tt)
try:
return _sizes[tt]
except KeyError:
sz = gbl.gInterpreter.ProcessLine("sizeof(%s);" % (_get_name(tt),))
_sizes[tt] = sz
return sz

_typeids = {}
def typeid(tt):
if not isinstance(tt, type):
tt = type(tt)
try:
return _typeids[tt]
except KeyError:
tidname = 'typeid_'+str(len(_typeids))
gbl.gInterpreter.ProcessLine(
"namespace _cppyy_internal { auto* %s = &typeid(%s); }" %\
(tidname, _get_name(tt),))
tid = getattr(gbl._cppyy_internal, tidname)
_typeids[tt] = tid
return tid
11 changes: 8 additions & 3 deletions python/cppyy/_cpython_cppyy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

__all__ = [
'gbl',
'load_reflection_info',
'addressof',
'bind_object',
'load_reflection_info',
'addressof',
'nullptr',
'move',
'_backend',
]

Expand All @@ -32,7 +35,7 @@ def __call__(self, *args):
if type(arg) == str:
arg = ','.join(map(lambda x: x.strip(), arg.split(',')))
newargs.append(arg)
result = _backend.MakeCppTemplateClass( *newargs )
result = _backend.MakeCppTemplateClass(*newargs)

# special case pythonization (builtin_map is not available from the C-API)
if 'push_back' in result.__dict__:
Expand Down Expand Up @@ -75,8 +78,10 @@ class std(object):


#- exports -------------------------------------------------------------------
addressof = _backend.addressof
addressof = _backend.addressof
bind_object = _backend.bind_object
nullptr = _backend.nullptr
move = _backend.move

def load_reflection_info(name):
sc = gbl.gSystem.Load(name)
Expand Down

0 comments on commit ecaffa4

Please sign in to comment.