hikyuu2/hikyuu_pywrap/ioredirect.h

54 lines
1.2 KiB
C++
Raw Normal View History

/*
2018-09-22 18:27:55 +08:00
pybind11/iostream.h -- Tools to assist with redirecting cout and cerr to Python
Copyright (c) 2017 Henry F. Schreiner
All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
*/
/*
* modified by: fasiondog
* modified from https://github.com/pybind/pybind11/include/pybind11/iostream.h
*/
#pragma once
#include <pybind11/iostream.h>
#include <hikyuu/DataType.h>
namespace hku {
void open_ostream_to_python();
void close_ostream_to_python();
2024-02-23 17:49:09 +08:00
class OStreamToPython final {
friend void open_ostream_to_python();
friend void close_ostream_to_python();
2018-09-22 18:27:55 +08:00
public:
explicit OStreamToPython(bool open) : m_old_opened(open) {
if (open && !ms_opened) {
ms_io_redirect.enter();
} else if (!open && ms_opened) {
ms_io_redirect.exit();
2018-09-22 18:27:55 +08:00
}
}
~OStreamToPython() {
if (m_old_opened && !ms_opened) {
ms_io_redirect.enter();
} else if (!m_old_opened && ms_opened) {
ms_io_redirect.exit();
2018-09-22 18:27:55 +08:00
}
}
private:
bool m_old_opened;
private:
static pybind11::detail::OstreamRedirect ms_io_redirect;
static bool ms_opened;
};
2024-02-23 17:49:09 +08:00
} // namespace hku