Skip to content

Commit 97abf91

Browse files
committed
Merge branch 'development' into eb-scopedpointer
2 parents c17a056 + 7aeec06 commit 97abf91

9 files changed

Lines changed: 72 additions & 159 deletions

File tree

Source/Main.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,15 @@ class OpenEphysApplication : public JUCEApplication
6161

6262
StringArray parameters;
6363
parameters.addTokens(commandLine," ","\"");
64+
parameters.removeEmptyStrings();
6465

6566
#ifdef WIN32
6667
//glWinInit();
6768

68-
if (parameters.contains("--console",true))
69+
int consoleArg = parameters.indexOf("--console", true);
70+
if (consoleArg != -1)
6971
{
72+
parameters.remove(consoleArg);
7073
if (AllocConsole())
7174
{
7275
freopen("CONOUT$","w",stdout);
@@ -81,14 +84,20 @@ class OpenEphysApplication : public JUCEApplication
8184

8285
#endif
8386

84-
8587
customLookAndFeel = new CustomLookAndFeel();
8688
LookAndFeel::setDefaultLookAndFeel(customLookAndFeel);
8789

88-
mainWindow = new MainWindow();
89-
90-
9190

91+
// signal chain to load
92+
if (!parameters.isEmpty())
93+
{
94+
File fileToLoad(File::getCurrentWorkingDirectory().getChildFile(parameters[0]));
95+
mainWindow = new MainWindow(fileToLoad);
96+
}
97+
else
98+
{
99+
mainWindow = new MainWindow();
100+
}
92101
}
93102

94103
void shutdown() { }

Source/MainWindow.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static inline File getSavedStateDirectory() {
3939
#endif
4040
}
4141

42-
MainWindow::MainWindow()
42+
MainWindow::MainWindow(const File& fileToLoad)
4343
: DocumentWindow(JUCEApplication::getInstance()->getApplicationName(),
4444
Colour(Colours::black),
4545
DocumentWindow::allButtons)
@@ -83,7 +83,11 @@ static inline File getSavedStateDirectory() {
8383
// Constraining the window's size doesn't seem to work:
8484
setResizeLimits(500, 500, 10000, 10000);
8585

86-
if (shouldReloadOnStartup)
86+
if (!fileToLoad.getFullPathName().isEmpty())
87+
{
88+
ui->getEditorViewport()->loadState(fileToLoad);
89+
}
90+
else if (shouldReloadOnStartup)
8791
{
8892
File file = getSavedStateDirectory().getChildFile("lastConfig.xml");
8993
ui->getEditorViewport()->loadState(file);

Source/MainWindow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class MainWindow : public DocumentWindow
4646

4747
/** Initializes the MainWindow, creates the AudioComponent, ProcessorGraph,
4848
and UIComponent, and sets the window boundaries. */
49-
MainWindow();
49+
MainWindow(const File& fileToLoad = File());
5050

5151
/** Destroys the AudioComponent, ProcessorGraph, and UIComponent, and saves the window boundaries. */
5252
~MainWindow();

Source/Plugins/EventBroadcaster/EventBroadcaster.cpp

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,18 @@
1111
#include "EventBroadcaster.h"
1212
#include "EventBroadcasterEditor.h"
1313

14-
EventBroadcaster::ZMQContext* EventBroadcaster::sharedContext = nullptr;
15-
CriticalSection EventBroadcaster::sharedContextLock{};
16-
17-
EventBroadcaster::ZMQContext::ZMQContext(const ScopedLock& lock)
14+
EventBroadcaster::ZMQContext::ZMQContext()
1815
#ifdef ZEROMQ
1916
: context(zmq_ctx_new())
2017
#else
2118
: context(nullptr)
2219
#endif
23-
{
24-
sharedContext = this;
25-
}
20+
{}
2621

2722
// ZMQContext is a ReferenceCountedObject with a pointer in each instance's
2823
// socket pointer, so this only happens when the last instance is destroyed.
2924
EventBroadcaster::ZMQContext::~ZMQContext()
3025
{
31-
ScopedLock lock(sharedContextLock);
32-
sharedContext = nullptr;
3326
#ifdef ZEROMQ
3427
zmq_ctx_destroy(context);
3528
#endif
@@ -49,18 +42,6 @@ EventBroadcaster::ZMQSocket::ZMQSocket()
4942
: socket (nullptr)
5043
, boundPort (0)
5144
{
52-
ScopedLock lock(sharedContextLock);
53-
if (sharedContext == nullptr)
54-
{
55-
// first one, create the context
56-
context = new ZMQContext(lock);
57-
}
58-
else
59-
{
60-
// use already-created context
61-
context = sharedContext;
62-
}
63-
6445
#ifdef ZEROMQ
6546
socket = context->createZMQSocket();
6647
#endif
@@ -122,6 +103,7 @@ int EventBroadcaster::ZMQSocket::unbind()
122103
{
123104
boundPort = 0;
124105
}
106+
return status;
125107
}
126108
#endif
127109
return 0;
@@ -157,7 +139,10 @@ AudioProcessorEditor* EventBroadcaster::createEditor()
157139

158140
int EventBroadcaster::getListeningPort() const
159141
{
160-
if (zmqSocket == nullptr) { return 0; }
142+
if (zmqSocket == nullptr)
143+
{
144+
return 0;
145+
}
161146
return zmqSocket->getBoundPort();
162147
}
163148

@@ -197,7 +182,7 @@ int EventBroadcaster::setListeningPort(int port, bool forceRestart)
197182
}
198183
}
199184

200-
if (status != 0 && zmqSocket)
185+
if (status != 0 && zmqSocket != nullptr)
201186
{
202187
// try to rebind current socket to previous port
203188
zmqSocket->bind(currPort);
@@ -208,7 +193,7 @@ int EventBroadcaster::setListeningPort(int port, bool forceRestart)
208193

209194
// update editor
210195
auto editor = static_cast<EventBroadcasterEditor*>(getEditor());
211-
if (editor)
196+
if (editor != nullptr)
212197
{
213198
editor->setDisplayedPort(getListeningPort());
214199
}
@@ -229,7 +214,7 @@ void EventBroadcaster::sendEvent(const MidiMessage& event, float eventSampleRate
229214
double timestampSeconds = double(Event::getTimestamp(event)) / eventSampleRate;
230215
uint16 type = Event::getBaseType(event);
231216

232-
if (!zmqSocket)
217+
if (zmqSocket == nullptr)
233218
{
234219
std::cout << "Failed to send message: no socket" << std::endl;
235220
}

Source/Plugins/EventBroadcaster/EventBroadcaster.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ class EventBroadcaster : public GenericProcessor
4242

4343

4444
private:
45-
class ZMQContext : public ReferenceCountedObject
45+
class ZMQContext
4646
{
4747
public:
48-
ZMQContext(const ScopedLock& lock);
49-
~ZMQContext() override;
48+
ZMQContext();
49+
~ZMQContext();
5050
void* createZMQSocket();
5151
private:
5252
void* context;
@@ -67,18 +67,15 @@ class EventBroadcaster : public GenericProcessor
6767
private:
6868
int boundPort;
6969
void* socket;
70-
ReferenceCountedObjectPtr<ZMQContext> context;
70+
71+
// see here for why the context can't just be static:
72+
// https://github.com/zeromq/libzmq/issues/1708
73+
SharedResourcePointer<ZMQContext> context;
7174
};
7275

7376
void sendEvent(const MidiMessage& event, float eventSampleRate) const;
7477

7578
static String getEndpoint(int port);
76-
77-
// share a "dumb" pointer that doesn't take part in reference counting.
78-
// want the context to be terminated by the time the static members are
79-
// destroyed (see: https://github.com/zeromq/libzmq/issues/1708)
80-
static ZMQContext* sharedContext;
81-
static CriticalSection sharedContextLock;
8279

8380
ScopedPointer<ZMQSocket> zmqSocket;
8481
};

Source/Plugins/NetworkEvents/NetworkEvents.cpp

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ const int MAX_MESSAGE_LENGTH = 64000;
3535
#include <unistd.h>
3636
#endif
3737

38-
NetworkEvents::ZMQContext* NetworkEvents::sharedContext = nullptr;
39-
CriticalSection NetworkEvents::sharedContextLock{};
40-
4138
NetworkEvents::NetworkEvents()
4239
: GenericProcessor ("Network Events")
4340
, Thread ("NetworkThread")
@@ -79,6 +76,7 @@ String NetworkEvents::getCurrPortString() const
7976

8077
void NetworkEvents::restartConnection()
8178
{
79+
requestedPort = boundPort.load();
8280
makeNewSocket = true;
8381
}
8482

@@ -430,21 +428,15 @@ String NetworkEvents::getPortString(uint16 port)
430428

431429
/*** ZMQContext ***/
432430

433-
NetworkEvents::ZMQContext::ZMQContext(const ScopedLock& lock)
431+
NetworkEvents::ZMQContext::ZMQContext()
434432
#ifdef ZEROMQ
435433
: context(zmq_ctx_new())
436434
#endif
437-
{
438-
// sharedContextLock should already be held here
439-
sharedContext = this;
440-
}
435+
{}
441436

442-
// ZMQContext is a ReferenceCountedObject with a pointer in each instance's
443-
// socket pointer, so this only happens when the last instance is destroyed.
437+
// only happens when the last SharedResourcePointer is destroyed.
444438
NetworkEvents::ZMQContext::~ZMQContext()
445439
{
446-
ScopedLock lock(sharedContextLock);
447-
sharedContext = nullptr;
448440
#ifdef ZEROMQ
449441
zmq_ctx_destroy(context);
450442
#endif
@@ -471,20 +463,6 @@ NetworkEvents::Responder::Responder(uint16 port)
471463
, boundPort (0)
472464
, lastErrno (0)
473465
{
474-
{
475-
ScopedLock lock(sharedContextLock);
476-
if (sharedContext == nullptr)
477-
{
478-
// first one, create the context
479-
context = new ZMQContext(lock);
480-
}
481-
else
482-
{
483-
// use already-created context
484-
context = sharedContext;
485-
}
486-
}
487-
488466
#ifdef ZEROMQ
489467
socket = context->createSocket();
490468
if (!socket)

Source/Plugins/NetworkEvents/NetworkEvents.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,12 @@ class NetworkEvents : public GenericProcessor
9090
int64 timestamp;
9191
};
9292

93-
class ZMQContext : public ReferenceCountedObject
93+
class ZMQContext
9494
{
9595
public:
96-
ZMQContext(const ScopedLock& lock);
97-
~ZMQContext() override;
96+
ZMQContext();
97+
~ZMQContext();
9898
void* createSocket();
99-
100-
typedef ReferenceCountedObjectPtr<ZMQContext> Ptr;
101-
10299
private:
103100
void* context;
104101

@@ -134,7 +131,7 @@ class NetworkEvents : public GenericProcessor
134131
int send(StringRef response);
135132

136133
private:
137-
ZMQContext::Ptr context;
134+
SharedResourcePointer<ZMQContext> context;
138135
void* socket;
139136
bool valid;
140137
uint16 boundPort;
@@ -161,12 +158,6 @@ class NetworkEvents : public GenericProcessor
161158
// get a representation of the given port for use on the editor
162159
static String getPortString(uint16 port);
163160

164-
// share a "dumb" pointer that doesn't take part in reference counting.
165-
// want the context to be terminated by the time the static members are
166-
// destroyed (see: https://github.com/zeromq/libzmq/issues/1708)
167-
static ZMQContext* sharedContext;
168-
static CriticalSection sharedContextLock;
169-
170161
std::atomic<bool> makeNewSocket; // port change or restart needed (depending on requestedPort)
171162
std::atomic<uint16> requestedPort; // never set by the thread; 0 means any free port
172163
std::atomic<uint16> boundPort; // only set by the thread; 0 means no connection

0 commit comments

Comments
 (0)