Skip to content

Commit fc77893

Browse files
committed
Make everything use boost::shared_ptr
NotepadPlusWrapper and all ScintillaWrapper are now held for python in boost:shared_ptr<>. boost::python has special handling for boost::shared_ptr (not yet for std::shared_ptr<>)
1 parent c13f021 commit fc77893

13 files changed

Lines changed: 49 additions & 46 deletions

PythonScript/src/NotepadPlusWrapper.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -376,18 +376,18 @@ void NotepadPlusWrapper::saveCurrentSession(const char *filename)
376376

377377
}
378378

379-
ScintillaWrapper NotepadPlusWrapper::createScintilla()
379+
boost::shared_ptr<ScintillaWrapper> NotepadPlusWrapper::createScintilla()
380380
{
381381
LRESULT handle = callNotepad(NPPM_CREATESCINTILLAHANDLE, 0, NULL);
382382

383383
// return copy
384-
return ScintillaWrapper((HWND)handle, m_nppHandle);
384+
return boost::shared_ptr<ScintillaWrapper>(new ScintillaWrapper((HWND)handle, m_nppHandle));
385385
}
386386

387-
void NotepadPlusWrapper::destroyScintilla(ScintillaWrapper& buffer)
387+
void NotepadPlusWrapper::destroyScintilla(boost::shared_ptr<ScintillaWrapper> buffer)
388388
{
389-
callNotepad(NPPM_DESTROYSCINTILLAHANDLE, 0, reinterpret_cast<LPARAM>(buffer.getHandle()));
390-
buffer.invalidateHandle();
389+
callNotepad(NPPM_DESTROYSCINTILLAHANDLE, 0, reinterpret_cast<LPARAM>(buffer->getHandle()));
390+
buffer->invalidateHandle();
391391
}
392392

393393
idx_t NotepadPlusWrapper::getCurrentDocIndex(int view)

PythonScript/src/NotepadPlusWrapper.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,8 @@ class NotepadPlusWrapper
460460
void saveSession(const char *sessionFilename, boost::python::list files);
461461
void saveCurrentSession(const char *filename);
462462

463-
ScintillaWrapper createScintilla();
464-
void destroyScintilla(ScintillaWrapper& buffer);
463+
boost::shared_ptr<ScintillaWrapper> createScintilla();
464+
void destroyScintilla(boost::shared_ptr<ScintillaWrapper> buffer);
465465

466466

467467
idx_t getCurrentDocIndex(int view);

PythonScript/src/NotepadPython.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void export_notepad()
1717
//lint -e1793 While calling ’Symbol’: Initializing the implicit object parameter ’Type’ (a non-const reference) with a non-lvalue
1818
// The class and enum declarations are used as designed, but they mess up Lint.
1919
boost::python::register_exception_translator<process_start_exception>(&translateProcessStart);
20-
boost::python::class_<NotepadPlusWrapper>("Notepad", boost::python::no_init)
20+
boost::python::class_<NotepadPlusWrapper, boost::shared_ptr<NotepadPlusWrapper>, boost::noncopyable>("Notepad", boost::python::no_init)
2121
.def("new", &NotepadPlusWrapper::newDocument, "Create a new document")
2222
.def("new", &NotepadPlusWrapper::newDocumentWithFilename, "Create a new document with the given filename")
2323
.def("save", &NotepadPlusWrapper::save, "Save the current file")

PythonScript/src/NotepadPython.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class NotepadPlusWrapper;
77

88
void export_notepad();
99

10-
void importNotepad(NotepadPlusWrapper* instance);
10+
void importNotepad(boost::shared_ptr<NotepadPlusWrapper> instance);
1111

1212
}
1313

PythonScript/src/PyProducerConsumer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class PyProducerConsumer
2424
DWORD getConsumerThreadID() { return m_dwThreadId; };
2525

2626
private:
27+
PyProducerConsumer(const PyProducerConsumer& copy);
28+
PyProducerConsumer& operator = (const PyProducerConsumer& assign);
29+
2730
HANDLE m_queueMutex;
2831
HANDLE m_dataAvailable;
2932
HANDLE m_shutdown;

PythonScript/src/PythonConsole.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ PythonConsole::PythonConsole(HWND hNotepad) :
2727
m_nppData(new NppData)
2828
{
2929
}
30-
30+
/*
3131
//lint -e1554 Direct pointer copy of member 'name' within copy constructor: 'PythonConsole::PythonConsole(const PythonConsole &)')
3232
// We indeed copy pointers, and it's okay. These are not allocated within the
3333
// scope of this class but rather passed in and copied anyway.
@@ -47,14 +47,15 @@ PythonConsole::PythonConsole(const PythonConsole& other) :
4747
{
4848
}
4949
//lint +e1554
50+
*/
5051

5152
PythonConsole::~PythonConsole()
5253
{
5354
try
5455
{
5556
delete mp_consoleDlg;
5657
delete m_nppData;
57-
delete mp_scintillaWrapper;
58+
5859
}
5960
catch (...)
6061
{
@@ -310,7 +311,7 @@ void export_console()
310311
{
311312
//lint -e1793 While calling ’Symbol’: Initializing the implicit object parameter ’Type’ (a non-const reference) with a non-lvalue
312313
// The class and enum declarations are used as designed, but they mess up Lint.
313-
boost::python::class_<PythonConsole>("Console", boost::python::no_init)
314+
boost::python::class_<PythonConsole, boost::shared_ptr<PythonConsole>, boost::noncopyable >("Console", boost::python::no_init)
314315
.def("write", &PythonConsole::writeText, "Writes text to the console. Uses the __str__ function of the object passed.")
315316
.def("clear", &PythonConsole::clear, "Clears the console window")
316317
.def("writeError", &PythonConsole::writeError, "Writes text in the console in a red colour")
@@ -320,7 +321,7 @@ void export_console()
320321
.def("run", &PythonConsole::runCommandNoStderr, "Runs a command on the console")
321322
.def("run", &PythonConsole::runCommandNoStdout, "Runs a command on the console")
322323
.add_static_property("encoding", &PythonConsole::getEncoding)
323-
.add_property("editor", boost::python::make_function(&PythonConsole::getScintillaWrapper, boost::python::return_internal_reference<>()));
324+
.add_property("editor", &PythonConsole::getScintillaWrapper);
324325
//lint +e1793
325326
}
326327

PythonScript/src/PythonConsole.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class PythonConsole : public PyProducerConsumer<std::string>, public ConsoleInte
2323
{
2424
public:
2525
explicit PythonConsole(HWND hNotepad);
26-
PythonConsole(const PythonConsole& other);
2726
~PythonConsole();
2827

2928
void init(HINSTANCE hInst, NppData& nppData);
@@ -68,9 +67,9 @@ class PythonConsole : public PyProducerConsumer<std::string>, public ConsoleInte
6867

6968
HWND getScintillaHwnd();
7069

71-
ScintillaWrapper& getScintillaWrapper() { return *mp_scintillaWrapper; }
70+
boost::shared_ptr<ScintillaWrapper> getScintillaWrapper() { return mp_scintillaWrapper; }
7271

73-
ScintillaWrapper* mp_scintillaWrapper;
72+
boost::shared_ptr<ScintillaWrapper> mp_scintillaWrapper;
7473

7574
static boost::python::str getEncoding() { return boost::python::str("utf-8"); }
7675

@@ -81,6 +80,7 @@ class PythonConsole : public PyProducerConsumer<std::string>, public ConsoleInte
8180
private:
8281
PythonConsole(); // default constructor disabled
8382
PythonConsole& operator = (const PythonConsole&); // assignment operator disabled
83+
PythonConsole(const PythonConsole& other);
8484

8585
ConsoleDialog *mp_consoleDlg;
8686

@@ -97,7 +97,7 @@ class PythonConsole : public PyProducerConsumer<std::string>, public ConsoleInte
9797

9898
void export_console();
9999

100-
void importConsole(PythonConsole *console);
100+
void importConsole(boost::shared_ptr<PythonConsole> console);
101101

102102
struct RunStatementArgs
103103
{

PythonScript/src/PythonHandler.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ PythonHandler::PythonHandler(TCHAR *pluginsDir, TCHAR *configDir, HINSTANCE hIns
3232

3333
mp_notepad = createNotepadPlusWrapper();
3434
mp_scintilla = createScintillaWrapper();
35-
mp_scintilla1 = new ScintillaWrapper(scintilla1Handle, m_nppHandle);
36-
mp_scintilla2 = new ScintillaWrapper(scintilla2Handle, m_nppHandle);
35+
mp_scintilla1.reset(new ScintillaWrapper(scintilla1Handle, m_nppHandle));
36+
mp_scintilla2.reset(new ScintillaWrapper(scintilla2Handle, m_nppHandle));
3737
}
3838

3939
PythonHandler::~PythonHandler(void)
@@ -56,10 +56,6 @@ PythonHandler::~PythonHandler(void)
5656

5757
}
5858

59-
delete mp_scintilla2;
60-
delete mp_scintilla1;
61-
delete mp_scintilla;
62-
delete mp_notepad;
6359

6460
// To please Lint, let's NULL these handles
6561
m_hInst = NULL;
@@ -76,15 +72,15 @@ PythonHandler::~PythonHandler(void)
7672
}
7773

7874

79-
ScintillaWrapper* PythonHandler::createScintillaWrapper()
75+
boost::shared_ptr<ScintillaWrapper> PythonHandler::createScintillaWrapper()
8076
{
8177
m_currentView = mp_notepad->getCurrentView();
82-
return new ScintillaWrapper(m_currentView ? m_scintilla2Handle : m_scintilla1Handle, m_nppHandle);
78+
return boost::shared_ptr<ScintillaWrapper>(new ScintillaWrapper(m_currentView ? m_scintilla2Handle : m_scintilla1Handle, m_nppHandle));
8379
}
8480

85-
NotepadPlusWrapper* PythonHandler::createNotepadPlusWrapper()
81+
boost::shared_ptr<NotepadPlusWrapper> PythonHandler::createNotepadPlusWrapper()
8682
{
87-
return new NotepadPlusWrapper(m_hInst, m_nppHandle);
83+
return boost::shared_ptr<NotepadPlusWrapper>(new NotepadPlusWrapper(m_hInst, m_nppHandle));
8884
}
8985

9086
void PythonHandler::initPython()

PythonScript/src/PythonHandler.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ class PythonHandler : public PyProducerConsumer<RunScriptArgs>
7171
protected:
7272
void consume(std::shared_ptr<RunScriptArgs> args);
7373

74-
ScintillaWrapper* createScintillaWrapper();
75-
NotepadPlusWrapper* createNotepadPlusWrapper();
74+
boost::shared_ptr<ScintillaWrapper> createScintillaWrapper();
75+
boost::shared_ptr<NotepadPlusWrapper> createNotepadPlusWrapper();
7676
virtual void queueComplete();
7777

7878
// Handles
@@ -99,13 +99,13 @@ class PythonHandler : public PyProducerConsumer<RunScriptArgs>
9999

100100
tstring m_machineBaseDir;
101101
tstring m_userBaseDir;
102-
NppPythonScript::ScintillaWrapper *mp_scintilla;
103-
NppPythonScript::ScintillaWrapper *mp_scintilla1;
104-
NppPythonScript::ScintillaWrapper *mp_scintilla2;
102+
boost::shared_ptr<ScintillaWrapper> mp_scintilla;
103+
boost::shared_ptr<ScintillaWrapper> mp_scintilla1;
104+
boost::shared_ptr<ScintillaWrapper> mp_scintilla2;
105105

106-
NotepadPlusWrapper *mp_notepad;
106+
boost::shared_ptr<NotepadPlusWrapper> mp_notepad;
107107

108-
PythonConsole *mp_console;
108+
boost::shared_ptr<PythonConsole> mp_console;
109109

110110
int m_currentView;
111111

PythonScript/src/ScintillaPython.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ BOOST_PYTHON_MODULE(Npp)
2525
boost::python::register_exception_translator<ArgumentException>(&translateArgumentException);
2626
boost::python::register_exception_translator<GroupNotFoundException>(&translateGroupNotFoundException);
2727

28-
boost::python::class_<ScintillaWrapper>("Editor", boost::python::no_init)
28+
boost::python::class_<ScintillaWrapper, boost::shared_ptr<ScintillaWrapper>, boost::noncopyable >("Editor", boost::python::no_init)
2929
.def("write", &ScintillaWrapper::AddText, "Add text to the document at current position (alias for addText).")
3030
.def("callback", &ScintillaWrapper::addCallback, "Registers a callback to a Python function when a Scintilla event occurs. e.g. editor.callback(my_function, [ScintillaNotification.CHARADDED])")
3131
.def("__getitem__", &ScintillaWrapper::GetLine, "Gets a line from the given (zero based) index")
@@ -780,16 +780,16 @@ void preinitScintillaModule()
780780
PyImport_AppendInittab("Npp", &initNpp);
781781
}
782782

783-
void importScintilla(ScintillaWrapper* editor, ScintillaWrapper* editor1, ScintillaWrapper* editor2)
783+
void importScintilla(boost::shared_ptr<ScintillaWrapper> editor, boost::shared_ptr<ScintillaWrapper> editor1, boost::shared_ptr<ScintillaWrapper> editor2)
784784
{
785785
// Get the __main__ module/namespace
786786
//object main_module(handle<>(borrowed(PyImport_AddModule("Npp"))));
787787
boost::python::object npp_module( (boost::python::handle<>(PyImport_ImportModule("Npp"))) );
788788
boost::python::object npp_namespace = npp_module.attr("__dict__");
789789
// Create an instance variable buffer in __main__ that points to the ScintillaWrapper instance
790-
npp_namespace["editor"] = boost::python::ptr(editor);
791-
npp_namespace["editor1"] = boost::python::ptr(editor1);
792-
npp_namespace["editor2"] = boost::python::ptr(editor2);
790+
npp_namespace["editor"] = editor;
791+
npp_namespace["editor1"] = editor1;
792+
npp_namespace["editor2"] = editor2;
793793

794794
// Import our Scintilla object
795795
// object main_module( (handle<>(PyImport_ImportModule("__main__"))) );
@@ -801,7 +801,7 @@ void importScintilla(ScintillaWrapper* editor, ScintillaWrapper* editor1, Scinti
801801

802802
}
803803

804-
void importNotepad(NotepadPlusWrapper* instance)
804+
void importNotepad(boost::shared_ptr<NotepadPlusWrapper> instance)
805805
{
806806
// Get the __main__ module/namespace
807807
//object main_module(handle<>(borrowed(PyImport_AddModule("Npp"))));
@@ -810,12 +810,12 @@ void importNotepad(NotepadPlusWrapper* instance)
810810

811811

812812
// Create an instance variable buffer in __main__ that points to the NotepadPlusWrapper instance
813-
main_namespace["notepad"] = boost::python::ptr(instance);
813+
main_namespace["notepad"] = instance;
814814

815815
}
816816

817817

818-
void importConsole(PythonConsole* instance)
818+
void importConsole(boost::shared_ptr<PythonConsole> instance)
819819
{
820820
// Get the __main__ module/namespace
821821
//object main_module(handle<>(borrowed(PyImport_AddModule("Npp"))));
@@ -824,7 +824,7 @@ void importConsole(PythonConsole* instance)
824824

825825

826826
// Create an instance variable buffer in __main__ that points to the PythonConsole instance
827-
main_namespace["console"] = boost::python::ptr(instance);
827+
main_namespace["console"] = instance;
828828
}
829829

830830
}

0 commit comments

Comments
 (0)