Skip to content

Commit

Permalink
[Reactor] Implement newFlowDevice3 to return shared_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl authored and speth committed Jun 21, 2023
1 parent 03c7264 commit 3f60d35
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
12 changes: 10 additions & 2 deletions include/cantera/zeroD/FlowDeviceFactory.h
Expand Up @@ -14,10 +14,10 @@ namespace Cantera

//! Factory class to create FlowDevice objects.
//!
//! This class is mainly used via the newFlowDevice() function, for example:
//! This class is mainly used via the newFlowDevice3() function, for example:
//!
//! ```cpp
//! unique_ptr<FlowDevice> mfc(newFlowDevice("MassFlowController"));
//! shared_ptr<FlowDevice> mfc = newFlowDevice3("MassFlowController");
//! ```
//!
//! @ingroup ZeroD
Expand All @@ -31,6 +31,7 @@ class FlowDeviceFactory : public Factory<FlowDevice>
//! Create a new flow device by type name.
/*!
* @param flowDeviceType the type to be created.
* @deprecated To be removed after Cantera 3.0; replaceable by newFlowDevice3.
*/
virtual FlowDevice* newFlowDevice(const std::string& flowDeviceType);

Expand All @@ -41,9 +42,16 @@ class FlowDeviceFactory : public Factory<FlowDevice>
};

//! Create a FlowDevice object of the specified type
//! @deprecated To be changed after Cantera 3.0; for new behavior, see newFlowDevice3.
//! @ingroup ZeroD
FlowDevice* newFlowDevice(const string& model);

//! Create a FlowDevice object of the specified type
//! @ingroup ZeroD
//! @since New in Cantera 3.0.
//! @todo Transition back to newFlowDevice after Cantera 3.0
shared_ptr<FlowDevice> newFlowDevice3(const string& model);

}

#endif
3 changes: 2 additions & 1 deletion interfaces/cython/cantera/reactor.pxd
Expand Up @@ -31,7 +31,7 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":

# factories
cdef shared_ptr[CxxReactorBase] newReactor3(string) except +translate_exception
cdef CxxFlowDevice* newFlowDevice(string) except +translate_exception
cdef shared_ptr[CxxFlowDevice] newFlowDevice3(string) except +translate_exception
cdef shared_ptr[CxxWallBase] newWall3(string) except +translate_exception

# reactors
Expand Down Expand Up @@ -265,6 +265,7 @@ cdef class Wall(WallBase):
pass

cdef class FlowDevice:
cdef shared_ptr[CxxFlowDevice] _dev
cdef CxxFlowDevice* dev
cdef Func1 _rate_func
cdef Func1 _time_func
Expand Down
6 changes: 2 additions & 4 deletions interfaces/cython/cantera/reactor.pyx
Expand Up @@ -1023,7 +1023,8 @@ cdef class FlowDevice:
"""
flowdevice_type = "none"
def __cinit__(self, *args, **kwargs):
self.dev = newFlowDevice(stringify(self.flowdevice_type))
self._dev = newFlowDevice3(stringify(self.flowdevice_type))
self.dev = self._dev.get()

def __init__(self, upstream, downstream, *, name=None):
assert self.dev != NULL
Expand All @@ -1038,9 +1039,6 @@ cdef class FlowDevice:

self._install(upstream, downstream)

def __dealloc__(self):
del self.dev

property type:
"""The type of the flow device."""
def __get__(self):
Expand Down
5 changes: 2 additions & 3 deletions src/clib/ctreactor.cpp
Expand Up @@ -18,7 +18,7 @@ using namespace Cantera;

typedef SharedCabinet<ReactorBase> ReactorCabinet;
typedef SharedCabinet<ReactorNet> NetworkCabinet;
typedef Cabinet<FlowDevice> FlowDeviceCabinet;
typedef SharedCabinet<FlowDevice> FlowDeviceCabinet;
typedef SharedCabinet<WallBase> WallCabinet;
typedef Cabinet<Func1> FuncCabinet;
typedef SharedCabinet<ThermoPhase> ThermoCabinet;
Expand Down Expand Up @@ -352,8 +352,7 @@ extern "C" {
int flowdev_new(const char* type)
{
try {
FlowDevice* f = FlowDeviceFactory::factory()->newFlowDevice(type);
return FlowDeviceCabinet::add(f);
return FlowDeviceCabinet::add(newFlowDevice3(type));
} catch (...) {
return handleAllExceptions(-1, ERR);
}
Expand Down
10 changes: 10 additions & 0 deletions src/zeroD/FlowDeviceFactory.cpp
Expand Up @@ -36,12 +36,22 @@ void FlowDeviceFactory::deleteFactory() {

FlowDevice* FlowDeviceFactory::newFlowDevice(const std::string& flowDeviceType)
{
warn_deprecated("FlowDeviceFactory::newFlowDevice",
"To be removed after Cantera 3.0; for new behavior, see 'newFlowDevice3'.");
return create(flowDeviceType);
}

FlowDevice* newFlowDevice(const string& model)
{
warn_deprecated("newFlowDevice",
"To be changed after Cantera 3.0; for new behavior, see 'newFlowDevice3'.");
return FlowDeviceFactory::factory()->newFlowDevice(model);
}

shared_ptr<FlowDevice> newFlowDevice3(const string& model)
{
shared_ptr<FlowDevice> fdptr(FlowDeviceFactory::factory()->create(model));
return fdptr;
}

}

0 comments on commit 3f60d35

Please sign in to comment.