55import os .path
66import sys
77import types
8+ from enum import IntEnum
89
910# We need to patch mock modules into sys.modules to avoid pulling in csp/__init__.py and all of its baggage, which include imports of
1011# _cspimpl, which would be a circular dep
@@ -85,7 +86,7 @@ def __init__(self, module_name: str, output_filename: str, namespace: str, gener
8586
8687 if issubclass (v , Struct ) and v is not Struct :
8788 self ._struct_types .append (v )
88- elif issubclass (v , Enum ) and v is not Enum :
89+ elif issubclass (v , Enum ) and v is not Enum or issubclass ( v , IntEnum ) and v is not IntEnum :
8990 self ._enum_types .append (v )
9091
9192 def _get_dependent_headers (self ):
@@ -119,11 +120,9 @@ def cpp_filename(self):
119120 return self ._cpp_filename
120121
121122 def generate_header_code (self ):
122- include_guard = "_IN_CSP_AUTOGEN_" + self ._module_name .replace ("." , "_" ).upper ()
123- out = f"""
124- #ifndef { include_guard }
125- #define { include_guard }
126-
123+ out = f"""//// Generated from { self ._module_name }
124+ #pragma once
125+
127126"""
128127 out += self ._generate_headers ()
129128
@@ -139,11 +138,11 @@ def generate_header_code(self):
139138 for struct_type in self ._struct_types :
140139 out += self ._generate_struct_class (struct_type )
141140
142- out += "\n }\n #endif "
141+ out += "\n }"
143142 return out
144143
145144 def _generate_headers (self ):
146- common_headers = ["csp/core/Exception.h" , "csp/engine/Struct.h" , "cstddef" ]
145+ common_headers = ["csp/core/Exception.h" , "csp/engine/Struct.h" , "csp/python/Conversions.h" , " cstddef" ]
147146
148147 common_headers .extend (self ._get_dependent_headers ())
149148 return "\n " .join (f"#include <{ h } >" for h in common_headers )
@@ -173,7 +172,11 @@ class {enum_name} : public csp::CspEnum
173172 static { enum_name } create( enum_ v ) {{ return s_meta -> create( ( int64_t ) v ); }}
174173 static { enum_name } create( const char * name) {{ return s_meta -> fromString( name ); }}
175174 static { enum_name } create( const std::string & s ) {{ return create( s.c_str() ); }}
176-
175+ static { enum_name } create( PyObject * e )
176+ {{
177+ return { enum_name } ( csp::python::fromPython<CspEnum>( e, *s_cspEnumType ) );
178+ }}
179+
177180 enum_ enum_value() const {{ return ( enum_ ) value(); }}
178181
179182 static constexpr uint32_t num_types() {{ return { len ([x for x in enum_type ])} ; }}
@@ -183,7 +186,7 @@ class {enum_name} : public csp::CspEnum
183186 { enum_name } ( const csp::CspEnum & v ) : csp::CspEnum( v ) {{ CSP_TRUE_OR_THROW( v.meta() == s_meta.get(), AssertionError, "Mismatched enum meta" ); }}
184187
185188private:
186-
189+ static std::shared_ptr<const csp::CspEnumType> s_cspEnumType;
187190 static std::shared_ptr<csp::CspEnumMeta> s_meta;
188191}};
189192"""
@@ -439,14 +442,18 @@ def generate_cpp_code(self):
439442 assert_or_die( enumType != nullptr, "failed to find num type { enum_name } in module { self ._module_name } " );
440443
441444 // should add some assertion here..
442- csp::python::PyCspEnumMeta * pymeta = ( csp::python::PyCspEnumMeta * ) enumType;
443- s_meta = pymeta -> enumMeta;
445+ //csp::python::PyCspEnumMeta * pymeta = ( csp::python::PyCspEnumMeta * ) enumType;
446+ //s_meta = pymeta -> enumMeta;
447+ auto type = csp::python::CspTypeFactory::instance().typeFromPyType( enumType );
448+ s_cspEnumType = std::static_pointer_cast<const CspEnumType>( type );
449+ s_meta = s_cspEnumType -> meta();
444450 }}
445451
446452 return true;
447453}}
448454
449455bool static_init_{ enum_name } = { enum_name } ::static_init();
456+ std::shared_ptr<const csp::CspEnumType> { enum_name } ::s_cspEnumType;
450457std::shared_ptr<csp::CspEnumMeta> { enum_name } ::s_meta;
451458{ static_decls }
452459"""
@@ -460,6 +467,7 @@ def generate_cpp_code(self):
460467#include <csp/python/Common.h>
461468#include <csp/python/PyStruct.h>
462469#include <csp/python/PyCspEnum.h>
470+ #include <csp/python/CspTypeFactory.h>
463471#include <iostream>
464472#include <stdlib.h>
465473#include <Python.h>
0 commit comments