1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Add support for OSC Types T,F and I

This commit is contained in:
Ben Kuper 2022-02-05 17:30:25 +01:00
parent 6575d24a81
commit cb79e18b65
8 changed files with 64 additions and 16 deletions

View file

@ -26,13 +26,24 @@
namespace juce namespace juce
{ {
OSCArgument::OSCArgument (int32 v) : type (OSCTypes::int32), intValue (v) {} OSCArgument::OSCArgument() : type(OSCTypes::I) {}
OSCArgument::OSCArgument (float v) : type (OSCTypes::float32), floatValue (v) {} OSCArgument::OSCArgument(bool v) : type (v?OSCTypes::T:OSCTypes::F) {}
OSCArgument::OSCArgument (const String& s) : type (OSCTypes::string), stringValue (s) {} OSCArgument::OSCArgument (int32 v) : type (OSCTypes::int32), intValue (v) {}
OSCArgument::OSCArgument (MemoryBlock b) : type (OSCTypes::blob), blob (std::move (b)) {} OSCArgument::OSCArgument (float v) : type (OSCTypes::float32), floatValue (v) {}
OSCArgument::OSCArgument (OSCColour c) : type (OSCTypes::colour), intValue ((int32) c.toInt32()) {} OSCArgument::OSCArgument (const String& s) : type (OSCTypes::string), stringValue (s) {}
OSCArgument::OSCArgument (MemoryBlock b) : type (OSCTypes::blob), blob (std::move (b)) {}
OSCArgument::OSCArgument (OSCColour c) : type (OSCTypes::colour), intValue ((int32) c.toInt32()) {}
//============================================================================== //==============================================================================
bool OSCArgument::getBool() const noexcept
{
if (isTorF())
return type == OSCTypes::T;
jassertfalse; // you must check the type of an argument before attempting to get its value!
return {};
}
String OSCArgument::getString() const noexcept String OSCArgument::getString() const noexcept
{ {
if (isString()) if (isString())

View file

@ -40,6 +40,12 @@ namespace juce
class JUCE_API OSCArgument class JUCE_API OSCArgument
{ {
public: public:
/** Constructs an OSCArgument with type I (for Impulse). */
OSCArgument();
/** Constructs an OSCArgument with type boolean T or F depending on the value. */
OSCArgument(bool trueOrFalse);
/** Constructs an OSCArgument with type int32 and a given value. */ /** Constructs an OSCArgument with type int32 and a given value. */
OSCArgument (int32 value); OSCArgument (int32 value);
@ -65,6 +71,12 @@ public:
*/ */
OSCType getType() const noexcept { return type; } OSCType getType() const noexcept { return type; }
/** Returns whether the type of the OSCArgument is T or F. */
bool isImpulse() const noexcept { return type == OSCTypes::I; }
/** Returns whether the type of the OSCArgument is T or F. */
bool isTorF() const noexcept { return type == OSCTypes::T || type == OSCTypes::F; }
/** Returns whether the type of the OSCArgument is int32. */ /** Returns whether the type of the OSCArgument is int32. */
bool isInt32() const noexcept { return type == OSCTypes::int32; } bool isInt32() const noexcept { return type == OSCTypes::int32; }
@ -80,6 +92,11 @@ public:
/** Returns whether the type of the OSCArgument is colour. */ /** Returns whether the type of the OSCArgument is colour. */
bool isColour() const noexcept { return type == OSCTypes::colour; } bool isColour() const noexcept { return type == OSCTypes::colour; }
/** Returns the value of the OSCArgument as an int32.
If the type of the OSCArgument is not int32, the behaviour is undefined.
*/
bool getBool() const noexcept;
/** Returns the value of the OSCArgument as an int32. /** Returns the value of the OSCArgument as an int32.
If the type of the OSCArgument is not int32, the behaviour is undefined. If the type of the OSCArgument is not int32, the behaviour is undefined.
*/ */

View file

@ -88,6 +88,7 @@ void OSCMessage::clear()
} }
//============================================================================== //==============================================================================
void OSCMessage::addBool(bool value) { arguments.add(OSCArgument(value)); }
void OSCMessage::addInt32 (int32 value) { arguments.add (OSCArgument (value)); } void OSCMessage::addInt32 (int32 value) { arguments.add (OSCArgument (value)); }
void OSCMessage::addFloat32 (float value) { arguments.add (OSCArgument (value)); } void OSCMessage::addFloat32 (float value) { arguments.add (OSCArgument (value)); }
void OSCMessage::addString (const String& value) { arguments.add (OSCArgument (value)); } void OSCMessage::addString (const String& value) { arguments.add (OSCArgument (value)); }

View file

@ -119,6 +119,11 @@ public:
void clear(); void clear();
//============================================================================== //==============================================================================
/** Creates a new OSCArgument of type T or F depending on the given value,
and adds it to the OSCMessage object.
*/
void addBool(bool value);
/** Creates a new OSCArgument of type int32 with the given value, /** Creates a new OSCArgument of type int32 with the given value,
and adds it to the OSCMessage object. and adds it to the OSCMessage object.
*/ */

View file

@ -174,17 +174,19 @@ namespace
return typeList; return typeList;
} }
//============================================================================== //==============================================================================
OSCArgument readArgument (OSCType type) OSCArgument readArgument(OSCType type)
{ {
switch (type) switch (type)
{ {
case OSCTypes::int32: return OSCArgument (readInt32()); case OSCTypes::I: return OSCArgument();
case OSCTypes::float32: return OSCArgument (readFloat32()); case OSCTypes::T: return OSCArgument(true);
case OSCTypes::string: return OSCArgument (readString()); case OSCTypes::F: return OSCArgument(false);
case OSCTypes::blob: return OSCArgument (readBlob()); case OSCTypes::int32: return OSCArgument(readInt32());
case OSCTypes::colour: return OSCArgument (readColour()); case OSCTypes::float32: return OSCArgument(readFloat32());
case OSCTypes::string: return OSCArgument(readString());
case OSCTypes::blob: return OSCArgument(readBlob());
case OSCTypes::colour: return OSCArgument(readColour());
default: default:
// You supplied an invalid OSCType when calling readArgument! This should never happen. // You supplied an invalid OSCType when calling readArgument! This should never happen.
jassertfalse; jassertfalse;

View file

@ -123,6 +123,9 @@ namespace
{ {
switch (arg.getType()) switch (arg.getType())
{ {
case OSCTypes::T: return true;
case OSCTypes::F: return true;
case OSCTypes::I: return true;
case OSCTypes::int32: return writeInt32 (arg.getInt32()); case OSCTypes::int32: return writeInt32 (arg.getInt32());
case OSCTypes::float32: return writeFloat32 (arg.getFloat32()); case OSCTypes::float32: return writeFloat32 (arg.getFloat32());
case OSCTypes::string: return writeString (arg.getString()); case OSCTypes::string: return writeString (arg.getString());

View file

@ -26,6 +26,9 @@
namespace juce namespace juce
{ {
const OSCType OSCTypes::T = 'T';
const OSCType OSCTypes::F = 'F';
const OSCType OSCTypes::I = 'I';
const OSCType OSCTypes::int32 = 'i'; const OSCType OSCTypes::int32 = 'i';
const OSCType OSCTypes::float32 = 'f'; const OSCType OSCTypes::float32 = 'f';
const OSCType OSCTypes::string = 's'; const OSCType OSCTypes::string = 's';

View file

@ -46,6 +46,9 @@ using OSCTypeList = Array<OSCType>;
class JUCE_API OSCTypes class JUCE_API OSCTypes
{ {
public: public:
static const OSCType T;
static const OSCType F;
static const OSCType I;
static const OSCType int32; static const OSCType int32;
static const OSCType float32; static const OSCType float32;
static const OSCType string; static const OSCType string;
@ -54,7 +57,10 @@ public:
static bool isSupportedType (OSCType type) noexcept static bool isSupportedType (OSCType type) noexcept
{ {
return type == OSCTypes::int32 return type == OSCTypes::T
|| type == OSCTypes::F
|| type == OSCTypes::I
|| type == OSCTypes::int32
|| type == OSCTypes::float32 || type == OSCTypes::float32
|| type == OSCTypes::string || type == OSCTypes::string
|| type == OSCTypes::blob || type == OSCTypes::blob