Browse Source

Got rid of the nasty general-purpose ints and void*s that were in the Message class. Instead of using a plain old Message class, just create a subclass which contains whatever data you need in a more meaningful format.

tags/2021-05-28
Julian Storer 14 years ago
parent
commit
5224a3d9b8
10 changed files with 106 additions and 152 deletions
  1. +1
    -1
      modules/juce_cryptography/hashing/juce_MD5.h
  2. +5
    -6
      modules/juce_events/broadcasters/juce_ActionBroadcaster.cpp
  3. +35
    -20
      modules/juce_events/interprocess/juce_InterprocessConnection.cpp
  4. +0
    -1
      modules/juce_events/juce_events.cpp
  5. +0
    -54
      modules/juce_events/messages/juce_Message.cpp
  6. +0
    -19
      modules/juce_events/messages/juce_Message.h
  7. +1
    -1
      modules/juce_events/messages/juce_MessageListener.cpp
  8. +23
    -15
      modules/juce_events/messages/juce_MessageManager.cpp
  9. +37
    -18
      modules/juce_gui_basics/commands/juce_ApplicationCommandTarget.cpp
  10. +4
    -17
      modules/juce_gui_basics/commands/juce_ApplicationCommandTarget.h

+ 1
- 1
modules/juce_cryptography/hashing/juce_MD5.h View File

@@ -72,7 +72,7 @@ public:
@code MD5 checksum (myString.toUTF8());
@endcode
*/
MD5 (const CharPointer_UTF8& utf8Text) noexcept;
explicit MD5 (const CharPointer_UTF8& utf8Text) noexcept;
/** Destructor. */
~MD5() noexcept;


+ 5
- 6
modules/juce_events/broadcasters/juce_ActionBroadcaster.cpp View File

@@ -26,17 +26,17 @@
BEGIN_JUCE_NAMESPACE
//==============================================================================
// special message of our own with a string in it
class ActionMessage : public Message
{
public:
ActionMessage (const String& messageText, ActionListener* const listener_) noexcept
: message (messageText)
: message (messageText),
listener (listener_)
{
pointerParameter = listener_;
}
const String message;
ActionListener* const listener;
private:
JUCE_DECLARE_NON_COPYABLE (ActionMessage);
@@ -47,10 +47,9 @@ ActionBroadcaster::CallbackReceiver::CallbackReceiver() {}
void ActionBroadcaster::CallbackReceiver::handleMessage (const Message& message)
{
const ActionMessage& am = static_cast <const ActionMessage&> (message);
ActionListener* const target = static_cast <ActionListener*> (am.pointerParameter);
if (owner->actionListeners.contains (target))
target->actionListenerCallback (am.message);
if (owner->actionListeners.contains (am.listener))
am.listener->actionListenerCallback (am.message);
}
//==============================================================================


+ 35
- 20
modules/juce_events/interprocess/juce_InterprocessConnection.cpp View File

@@ -190,29 +190,44 @@ void InterprocessConnection::initialiseWithPipe (NamedPipe* const pipe_)
startThread();
}
const int messageMagicNumber = 0xb734128b;
//==============================================================================
struct ConnectionStateMessage : public Message
{
public:
ConnectionStateMessage (bool connectionMade_)
: connectionMade (connectionMade_)
{}
bool connectionMade;
};
struct DataDeliveryMessage : public Message
{
public:
DataDeliveryMessage (const MemoryBlock& data_)
: data (data_)
{}
MemoryBlock data;
};
void InterprocessConnection::handleMessage (const Message& message)
{
if (message.intParameter1 == messageMagicNumber)
{
switch (message.intParameter2)
{
case 0:
{
ScopedPointer <MemoryBlock> data (static_cast <MemoryBlock*> (message.pointerParameter));
messageReceived (*data);
break;
}
const ConnectionStateMessage* m = dynamic_cast <const ConnectionStateMessage*> (&message);
case 1:
if (m != nullptr)
{
if (m->connectionMade)
connectionMade();
break;
case 2:
else
connectionLost();
break;
}
}
else
{
const DataDeliveryMessage* d = dynamic_cast <const DataDeliveryMessage*> (&message);
if (d != nullptr)
messageReceived (d->data);
}
}
@@ -223,7 +238,7 @@ void InterprocessConnection::connectionMadeInt()
callbackConnectionState = true;
if (useMessageThread)
postMessage (new Message (messageMagicNumber, 1, 0, 0));
postMessage (new ConnectionStateMessage (true));
else
connectionMade();
}
@@ -236,7 +251,7 @@ void InterprocessConnection::connectionLostInt()
callbackConnectionState = false;
if (useMessageThread)
postMessage (new Message (messageMagicNumber, 2, 0, 0));
postMessage (new ConnectionStateMessage (false));
else
connectionLost();
}
@@ -247,7 +262,7 @@ void InterprocessConnection::deliverDataInt (const MemoryBlock& data)
jassert (callbackConnectionState);
if (useMessageThread)
postMessage (new Message (messageMagicNumber, 0, 0, new MemoryBlock (data)));
postMessage (new DataDeliveryMessage (data));
else
messageReceived (data);
}


+ 0
- 1
modules/juce_events/juce_events.cpp View File

@@ -60,7 +60,6 @@
// timers/*.cpp, interprocess/*.cpp
#include "messages/juce_ApplicationBase.cpp"
#include "messages/juce_DeletedAtShutdown.cpp"
#include "messages/juce_Message.cpp"
#include "messages/juce_MessageListener.cpp"
#include "messages/juce_MessageManager.cpp"
#include "broadcasters/juce_ActionBroadcaster.cpp"


+ 0
- 54
modules/juce_events/messages/juce_Message.cpp View File

@@ -1,54 +0,0 @@
/*
==============================================================================
This file is part of the JUCE library - "Jules' Utility Class Extensions"
Copyright 2004-11 by Raw Material Software Ltd.
------------------------------------------------------------------------------
JUCE can be redistributed and/or modified under the terms of the GNU General
Public License (Version 2), as published by the Free Software Foundation.
A copy of the license is included in the JUCE distribution, or can be found
online at www.gnu.org/licenses.
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.rawmaterialsoftware.com/juce for more information.
==============================================================================
*/
BEGIN_JUCE_NAMESPACE
//==============================================================================
Message::Message() noexcept
: intParameter1 (0),
intParameter2 (0),
intParameter3 (0),
pointerParameter (nullptr),
messageRecipient (nullptr)
{
}
Message::Message (const int intParameter1_,
const int intParameter2_,
const int intParameter3_,
void* const pointerParameter_) noexcept
: intParameter1 (intParameter1_),
intParameter2 (intParameter2_),
intParameter3 (intParameter3_),
pointerParameter (pointerParameter_),
messageRecipient (nullptr)
{
}
Message::~Message()
{
}
END_JUCE_NAMESPACE

+ 0
- 19
modules/juce_events/messages/juce_Message.h View File

@@ -49,28 +49,9 @@ public:
*/
Message() noexcept;
/** Creates a message object, filling in the member variables.
The corresponding public member variables will be set from the parameters
passed in.
*/
Message (int intParameter1,
int intParameter2,
int intParameter3,
void* pointerParameter) noexcept;
/** Destructor. */
virtual ~Message();
//==============================================================================
// These values can be used for carrying simple data that the application needs to
// pass around. For more complex messages, just create a subclass.
int intParameter1; /**< user-defined integer value. */
int intParameter2; /**< user-defined integer value. */
int intParameter3; /**< user-defined integer value. */
void* pointerParameter; /**< user-defined pointer value. */
/** A typedef for pointers to messages. */
typedef ReferenceCountedObjectPtr <Message> Ptr;


+ 1
- 1
modules/juce_events/messages/juce_MessageListener.cpp View File

@@ -53,7 +53,7 @@ void MessageListener::postMessage (Message* const message) const
bool MessageListener::isValidMessageListener() const noexcept
{
return (MessageManager::instance != nullptr)
return MessageManager::instance != nullptr
&& MessageManager::instance->messageListeners.contains (this);
}


+ 23
- 15
modules/juce_events/messages/juce_MessageManager.cpp View File

@@ -26,9 +26,28 @@
BEGIN_JUCE_NAMESPACE
//==============================================================================
MessageManager* MessageManager::instance = nullptr;
Message::Message() noexcept : messageRecipient (nullptr) {}
Message::~Message() {}
//==============================================================================
CallbackMessage::CallbackMessage() noexcept {}
CallbackMessage::~CallbackMessage() {}
void CallbackMessage::post()
{
if (MessageManager::instance != nullptr)
MessageManager::instance->postMessageToQueue (this);
}
//==============================================================================
struct QuitMessage : public Message
{
QuitMessage() noexcept {}
};
enum { quitMessageId = 0xfffff321 };
//==============================================================================
MessageManager* MessageManager::instance = nullptr;
MessageManager::MessageManager() noexcept
: quitMessagePosted (false),
@@ -76,17 +95,6 @@ void MessageManager::postMessageToQueue (Message* const message)
}
//==============================================================================
CallbackMessage::CallbackMessage() noexcept {}
CallbackMessage::~CallbackMessage() {}
void CallbackMessage::post()
{
if (MessageManager::instance != nullptr)
MessageManager::instance->postMessageToQueue (this);
}
//==============================================================================
// not for public use..
void MessageManager::deliverMessage (Message* const message)
{
JUCE_TRY
@@ -101,7 +109,7 @@ void MessageManager::deliverMessage (Message* const message)
{
callbackMessage->messageCallback();
}
else if (message->intParameter1 == (int) quitMessageId)
else if (dynamic_cast <QuitMessage*> (message) != nullptr)
{
quitMessageReceived = true;
}
@@ -125,7 +133,7 @@ void MessageManager::runDispatchLoop()
void MessageManager::stopDispatchLoop()
{
postMessageToQueue (new Message ((int) quitMessageId, 0, 0, nullptr));
postMessageToQueue (new QuitMessage());
quitMessagePosted = true;
}


+ 37
- 18
modules/juce_gui_basics/commands/juce_ApplicationCommandTarget.cpp View File

@@ -25,6 +25,41 @@
BEGIN_JUCE_NAMESPACE
//==============================================================================
class ApplicationCommandTarget::MessageTarget : public MessageListener
{
public:
MessageTarget (ApplicationCommandTarget& owner_)
: owner (owner_)
{
}
void handleMessage (const Message& message)
{
jassert (dynamic_cast <const InvokedMessage*> (&message) != nullptr);
owner.tryToInvoke (dynamic_cast <const InvokedMessage&> (message).info, false);
}
struct InvokedMessage : public Message
{
InvokedMessage (const InvocationInfo& info_)
: info (info_)
{}
const InvocationInfo info;
private:
JUCE_DECLARE_NON_COPYABLE (InvokedMessage);
};
private:
ApplicationCommandTarget& owner;
JUCE_DECLARE_NON_COPYABLE (MessageTarget);
};
//==============================================================================
ApplicationCommandTarget::ApplicationCommandTarget()
{
@@ -43,9 +78,9 @@ bool ApplicationCommandTarget::tryToInvoke (const InvocationInfo& info, const bo
if (async)
{
if (messageInvoker == nullptr)
messageInvoker = new CommandTargetMessageInvoker (this);
messageInvoker = new MessageTarget (*this);
messageInvoker->postMessage (new Message (0, 0, 0, new ApplicationCommandTarget::InvocationInfo (info)));
messageInvoker->postMessage (new MessageTarget::InvokedMessage (info));
return true;
}
else
@@ -174,21 +209,5 @@ ApplicationCommandTarget::InvocationInfo::InvocationInfo (const CommandID comman
{
}
//==============================================================================
ApplicationCommandTarget::CommandTargetMessageInvoker::CommandTargetMessageInvoker (ApplicationCommandTarget* const owner_)
: owner (owner_)
{
}
ApplicationCommandTarget::CommandTargetMessageInvoker::~CommandTargetMessageInvoker()
{
}
void ApplicationCommandTarget::CommandTargetMessageInvoker::handleMessage (const Message& message)
{
const ScopedPointer <InvocationInfo> info (static_cast <InvocationInfo*> (message.pointerParameter));
owner->tryToInvoke (*info, false);
}
END_JUCE_NAMESPACE

+ 4
- 17
modules/juce_gui_basics/commands/juce_ApplicationCommandTarget.h View File

@@ -234,24 +234,11 @@ public:
private:
//==============================================================================
// (for async invocation of commands)
class CommandTargetMessageInvoker : public MessageListener
{
public:
CommandTargetMessageInvoker (ApplicationCommandTarget* owner);
~CommandTargetMessageInvoker();
void handleMessage (const Message& message);
private:
ApplicationCommandTarget* const owner;
JUCE_DECLARE_NON_COPYABLE (CommandTargetMessageInvoker);
};
ScopedPointer <CommandTargetMessageInvoker> messageInvoker;
class MessageTarget;
friend class MessageTarget;
friend class ScopedPointer<MessageTarget>;
ScopedPointer<MessageTarget> messageInvoker;
friend class CommandTargetMessageInvoker;
bool tryToInvoke (const InvocationInfo& info, bool async);
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ApplicationCommandTarget);


Loading…
Cancel
Save