Skip to content

Commit

Permalink
Add support for types int64, double, T, F, Nil and Impulse
Browse files Browse the repository at this point in the history
This also updates the UnitTest to include those types
  • Loading branch information
benkuper committed Jun 22, 2024
1 parent 61a0309 commit d49fee0
Show file tree
Hide file tree
Showing 8 changed files with 359 additions and 14 deletions.
169 changes: 169 additions & 0 deletions modules/juce_osc/osc/juce_OSCArgument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ namespace juce
{

OSCArgument::OSCArgument (int32 v) : type (OSCTypes::int32), intValue (v) {}
OSCArgument::OSCArgument (int64 v) : type (OSCTypes::int64), int64Value (v) {}
OSCArgument::OSCArgument (float v) : type (OSCTypes::float32), floatValue (v) {}
OSCArgument::OSCArgument (double v) : type (OSCTypes::double64),doubleValue (v) {}
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()) {}
OSCArgument::OSCArgument(bool b) : type (b ? OSCTypes::T : OSCTypes::F) {}
OSCArgument::OSCArgument(OSCType t) : type (t) {} //for nil and impulse


//==============================================================================
String OSCArgument::getString() const noexcept
Expand All @@ -60,6 +65,15 @@ int32 OSCArgument::getInt32() const noexcept
return 0;
}

int64 OSCArgument::getInt64() const noexcept
{
if (isInt64())
return int64Value;

jassertfalse; // you must check the type of an argument before attempting to get its value!
return 0;
}

float OSCArgument::getFloat32() const noexcept
{
if (isFloat32())
Expand All @@ -69,6 +83,15 @@ float OSCArgument::getFloat32() const noexcept
return 0.0f;
}

double OSCArgument::getDouble() const noexcept
{
if (isDouble())
return doubleValue;

jassertfalse; // you must check the type of an argument before attempting to get its value!
return 0.0;
}

const MemoryBlock& OSCArgument::getBlob() const noexcept
{
// you must check the type of an argument before attempting to get its value!
Expand All @@ -86,6 +109,15 @@ OSCColour OSCArgument::getColour() const noexcept
return { 0, 0, 0, 0 };
}

bool OSCArgument::getBool() const noexcept
{
if (isBool())
return type == OSCTypes::T;

jassertfalse; // you must check the type of an argument before attempting to get its value!
return false;
}


//==============================================================================
//==============================================================================
Expand Down Expand Up @@ -126,14 +158,40 @@ class OSCArgumentTests final : public UnitTest

expect (arg.getType() == OSCTypes::int32);
expect (arg.isInt32());
expect (! arg.isInt64());
expect (! arg.isFloat32());
expect (! arg.isDouble());
expect (! arg.isString());
expect (! arg.isBlob());
expect (! arg.isColour());
expect (! arg.isNil());
expect (! arg.isImpulse());
expect (! arg.isBool());

expect (arg.getInt32() == value);
}

beginTest("Int64");
{
int64 value = 1234567890123456789;

OSCArgument arg(value);

expect(arg.getType() == OSCTypes::int64);
expect (! arg.isInt32());
expect (arg.isInt64());
expect (! arg.isFloat32());
expect (! arg.isDouble());
expect (! arg.isString());
expect (! arg.isBlob());
expect (! arg.isColour());
expect (! arg.isNil());
expect (! arg.isImpulse());
expect (! arg.isBool());

expect(arg.getInt64() == value);
}

beginTest ("Float32");
{
float value = 12345.6789f;
Expand All @@ -142,25 +200,56 @@ class OSCArgumentTests final : public UnitTest

expect (arg.getType() == OSCTypes::float32);
expect (! arg.isInt32());
expect (! arg.isInt64());
expect (arg.isFloat32());
expect (! arg.isDouble());
expect (! arg.isString());
expect (! arg.isBlob());
expect (! arg.isColour());
expect (! arg.isNil());
expect (! arg.isImpulse());
expect (! arg.isBool());

expectEquals (arg.getFloat32(), value);
}

beginTest("Double");
{
double value = 12345.6789;

OSCArgument arg(value);

expect(arg.getType() == OSCTypes::double64);
expect (! arg.isInt32());
expect (! arg.isInt64());
expect (! arg.isFloat32());
expect (arg.isDouble());
expect (! arg.isString());
expect (! arg.isBlob());
expect (! arg.isColour());
expect (! arg.isNil());
expect (! arg.isImpulse());
expect (! arg.isBool());

expectEquals(arg.getDouble(), value);
}

beginTest ("String");
{
String value = "Hello, World!";
OSCArgument arg (value);

expect (arg.getType() == OSCTypes::string);
expect (! arg.isInt32());
expect (! arg.isInt64());
expect (! arg.isFloat32());
expect (! arg.isDouble());
expect (arg.isString());
expect (! arg.isBlob());
expect (! arg.isColour());
expect (! arg.isNil());
expect (! arg.isImpulse());
expect (! arg.isBool());

expect (arg.getString() == value);
}
Expand All @@ -171,10 +260,15 @@ class OSCArgumentTests final : public UnitTest

expect (arg.getType() == OSCTypes::string);
expect (! arg.isInt32());
expect (! arg.isInt64());
expect (! arg.isFloat32());
expect (! arg.isDouble());
expect (arg.isString());
expect (! arg.isBlob());
expect (! arg.isColour());
expect (! arg.isNil());
expect (! arg.isImpulse());
expect (! arg.isBool());

expect (arg.getString() == "Hello, World!");
}
Expand All @@ -186,10 +280,15 @@ class OSCArgumentTests final : public UnitTest

expect (arg.getType() == OSCTypes::blob);
expect (! arg.isInt32());
expect (! arg.isInt64());
expect (! arg.isFloat32());
expect (! arg.isDouble());
expect (! arg.isString());
expect (arg.isBlob());
expect (! arg.isColour());
expect (! arg.isNil());
expect (! arg.isImpulse());
expect (! arg.isBool());

expect (arg.getBlob() == blob);
}
Expand All @@ -209,15 +308,85 @@ class OSCArgumentTests final : public UnitTest

expect (arg.getType() == OSCTypes::colour);
expect (! arg.isInt32());
expect (! arg.isInt64());
expect (! arg.isFloat32());
expect (! arg.isDouble());
expect (! arg.isString());
expect (! arg.isBlob());
expect (arg.isColour());
expect (! arg.isNil());
expect (! arg.isImpulse());
expect (! arg.isBool());

expect (arg.getColour().toInt32() == col.toInt32());
}
}

beginTest ("Nil");
{
OSCArgument arg (OSCTypes::nil);

expect (arg.getType() == OSCTypes::nil);
expect (! arg.isInt32());
expect (! arg.isInt64());
expect (! arg.isFloat32());
expect (! arg.isDouble());
expect (! arg.isString());
expect (! arg.isBlob());
expect (! arg.isColour());
expect (arg.isNil());
expect (! arg.isImpulse());
expect (! arg.isBool());
}

beginTest("Impulse");
{
OSCArgument arg(OSCTypes::impulse);

expect(arg.getType() == OSCTypes::impulse);
expect (! arg.isInt32());
expect (! arg.isInt64());
expect (! arg.isFloat32());
expect (! arg.isDouble());
expect (! arg.isString());
expect (! arg.isBlob());
expect (! arg.isColour());
expect (! arg.isNil());
expect (arg.isImpulse());
expect (! arg.isBool());
}

beginTest("True");
{
OSCArgument arg(OSCTypes::T);
expect (! arg.isInt32());
expect (! arg.isInt64());
expect (! arg.isFloat32());
expect (! arg.isDouble());
expect (! arg.isString());
expect (! arg.isBlob());
expect (! arg.isColour());
expect (! arg.isNil());
expect (! arg.isImpulse());
expect (arg.isBool());
}

beginTest("False");
{
OSCArgument arg(OSCTypes::F);
expect (! arg.isInt32());
expect (! arg.isInt64());
expect (! arg.isFloat32());
expect (! arg.isDouble());
expect (! arg.isString());
expect (! arg.isBlob());
expect (! arg.isColour());
expect (! arg.isNil());
expect (! arg.isImpulse());
expect (arg.isBool());
}


beginTest ("Copy, move and assignment");
{
{
Expand Down
50 changes: 50 additions & 0 deletions modules/juce_osc/osc/juce_OSCArgument.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ class JUCE_API OSCArgument
/** Constructs an OSCArgument with type int32 and a given value. */
OSCArgument (int32 value);

/** Constructs an OSCArgument with type int64 and a given value. */
OSCArgument(int64 value);

/** Constructs an OSCArgument with type float32 and a given value. */
OSCArgument (float value);

/** Constructs an OSCArgument with type double and a given value. */
OSCArgument(double value);

/** Constructs an OSCArgument with type string and a given value */
OSCArgument (const String& value);

Expand All @@ -69,6 +75,14 @@ class JUCE_API OSCArgument
/** Constructs an OSCArgument with type colour and a given colour value */
OSCArgument (OSCColour colour);

/** Constructs an OSCArgument with type T or F depending on the given boolean value */
OSCArgument(bool value);

/** Constructs an OSCArgument with the given OSC type tag.
This constructor is intended for creating OSCArgument objects with type nil or impulse.
*/
OSCArgument(OSCType type);

/** Returns the type of the OSCArgument as an OSCType.
OSCType is a char type, and its value will be the OSC type tag of the type.
*/
Expand All @@ -77,9 +91,15 @@ class JUCE_API OSCArgument
/** Returns whether the type of the OSCArgument is int32. */
bool isInt32() const noexcept { return type == OSCTypes::int32; }

/** Returns whether the type of the OSCArgument is int64. */
bool isInt64() const noexcept { return type == OSCTypes::int64; }

/** Returns whether the type of the OSCArgument is float. */
bool isFloat32() const noexcept { return type == OSCTypes::float32; }

/** Returns whether the type of the OSCArgument is double. */
bool isDouble() const noexcept { return type == OSCTypes::double64; }

/** Returns whether the type of the OSCArgument is string. */
bool isString() const noexcept { return type == OSCTypes::string; }

Expand All @@ -89,16 +109,35 @@ class JUCE_API OSCArgument
/** Returns whether the type of the OSCArgument is colour. */
bool isColour() const noexcept { return type == OSCTypes::colour; }

/** Returns whether the type of the OSCArgument is nil. */
bool isNil() const noexcept { return type == OSCTypes::nil; }

/** Returns whether the type of the OSCArgument is impulse. */
bool isImpulse() const noexcept { return type == OSCTypes::impulse; }

/** Returns whether the type of the OSCArgument is T or F. */
bool isBool() const noexcept { return type == OSCTypes::T || type == OSCTypes::F; }

/** Returns the value of the OSCArgument as an int32.
If the type of the OSCArgument is not int32, the behaviour is undefined.
*/
int32 getInt32() const noexcept;

/** Returns the value of the OSCArgument as an int64.
If the type of the OSCArgument is not int64, the behaviour is undefined.
*/
int64 getInt64() const noexcept;

/** Returns the value of the OSCArgument as a float32.
If the type of the OSCArgument is not float32, the behaviour is undefined.
*/
float getFloat32() const noexcept;

/** Returns the value of the OSCArgument as a double.
If the type of the OSCArgument is not double, the behaviour is undefined.
*/
double getDouble() const noexcept;

/** Returns the value of the OSCArgument as a string.
If the type of the OSCArgument is not string, the behaviour is undefined.
*/
Expand All @@ -116,6 +155,11 @@ class JUCE_API OSCArgument
*/
OSCColour getColour() const noexcept;

/** Returns the value of the OSCArgument as a boolean.
If the type of the OSCArgument is not T or F, the behaviour is undefined.
*/
bool getBool() const noexcept { return type == OSCTypes::T; }

private:
//==============================================================================
OSCType type;
Expand All @@ -126,6 +170,12 @@ class JUCE_API OSCArgument
float floatValue;
};

union
{
int64 int64Value;
double doubleValue;
};

String stringValue;
MemoryBlock blob;
};
Expand Down
Loading

0 comments on commit d49fee0

Please sign in to comment.