Skip to content

Instantly share code, notes, and snippets.

@2bbb
Created October 2, 2016 11:10
Show Gist options
  • Select an option

  • Save 2bbb/3fb59af832d2c43fdab43f91ac2c0c9c to your computer and use it in GitHub Desktop.

Select an option

Save 2bbb/3fb59af832d2c43fdab43f91ac2c0c9c to your computer and use it in GitHub Desktop.
ofxExtendedOscMessage
#include "ofMain.h"
#include "ofxOsc.h"
#include "ofxExtendedOscMessage.h"
class ofApp : public ofBaseApp {
ofxOscReceiver receiver;
ofxOscSender sender;
public:
void setup() {
sender.setup("localhost", 9005);
receiver.setup(9005);
ofSetBackgroundColor(0);
ofSetColor(255);
}
void update() {
while(receiver.hasWaitingMessages()) {
ofxExtendedOscMessage m;
receiver.getNextMessage(m);
const std::string address = m;
if(address == "/int") {
int n = m[0];
ofLogNotice() << address << " / " << n;
} else if(address == "/float") {
float x = m[0], y = m[1];
ofLogNotice() << address << " / " << x << ", " << y;
}
}
ofxExtendedOscMessage m;
if(ofRandomuf() < 0.5f) {
m = "/int";
m.push_back(static_cast<int>(ofRandom(100.0f)));
} else {
m = "/float";
m.push_back(ofRandom(100.0f));
m.push_back(ofRandom(100.0f));
}
sender.sendMessage(m);
}
void keyPressed(int key) {}
void keyReleased(int key) {}
void mouseMoved(int x, int y) {}
void mouseDragged(int x, int y, int button) {}
void mousePressed(int x, int y, int button) {}
void mouseReleased(int x, int y, int button) {}
void mouseEntered(int x, int y) {}
void mouseExited(int x, int y) {}
void windowResized(int w, int h) {}
void dragEvent(ofDragInfo dragInfo) {}
void gotMessage(ofMessage msg) {}
};
int main() {
ofSetupOpenGL(200, 200, OF_WINDOW);
ofRunApp(new ofApp);
}
//
// ofxExtendedOscMessage.h
// ofxExtendedOscMessage
//
// Created by ISHII 2bit on 2016/10/02.
//
//
#pragma once
#ifndef ofxExtendedOscMessage_h
#define ofxExtendedOscMessage_h
#include "ofxOscMessage.h"
namespace ofx {
struct ExtendedOscMessage : public ofxOscMessage {
struct ExtendedOscArg {
const ExtendedOscMessage &body;
const std::size_t index;
ExtendedOscArg(const ExtendedOscMessage &body, std::size_t index)
: body(body)
, index(index)
{};
inline operator bool() const { return body.getArgAsBool(index); }
inline operator char() const { return body.getArgAsChar(index); }
inline operator std::uint8_t() const { return body.getArgAsInt32(index); };
inline operator std::int16_t() const { return body.getArgAsInt32(index); };
inline operator std::uint16_t() const { return body.getArgAsInt32(index); };
inline operator std::int32_t() const { return body.getArgAsInt32(index); };
inline operator std::uint32_t() const { return body.getArgAsInt64(index); };
inline operator std::int64_t() const { return body.getArgAsInt64(index); };
inline operator std::uint64_t() const { return body.getArgAsInt64(index); };
inline operator std::string() const { return body.getArgAsString(index); };
inline operator float() const { return body.getArgAsFloat(index); };
inline operator double() const { return body.getArgAsDouble(index); };
inline operator ofBuffer() const { return body.getArgAsBlob(index); };
};
using parent = ofxOscMessage;
ExtendedOscMessage()
: parent() {}
ExtendedOscMessage(const ofxOscMessage &other) { parent::copy(other); }
ExtendedOscMessage(ofxOscMessage &&other) { parent::copy(std::move(other)); }
ExtendedOscMessage &operator=(const ofxOscMessage &other) {
parent::copy(other);
return *this;
}
ExtendedOscMessage &operator=(ofxOscMessage &&other) {
parent::copy(std::move(other));
return *this;
}
ExtendedOscMessage &operator=(const std::string &address) {
setAddress(address);
return *this;
}
std::size_t size() const { return getNumArgs(); }
ExtendedOscArg operator[](std::size_t index) const { return {*this, index}; }
operator std::string() const { return getAddress(); }
inline void push_back(bool arg) { addBoolArg(arg); };
inline void push_back(char arg) { addCharArg(arg); };
inline void push_back(std::uint8_t arg) { addInt32Arg(arg); };
inline void push_back(std::int16_t arg) { addInt32Arg(arg); };
inline void push_back(std::uint16_t arg) { addInt32Arg(arg); };
inline void push_back(std::int32_t arg) { addInt32Arg(arg); };
inline void push_back(std::uint32_t arg) { addInt64Arg(arg); };
inline void push_back(std::int64_t arg) { addInt64Arg(arg); };
inline void push_back(std::uint64_t arg) { addInt64Arg(arg); };
inline void push_back(float arg) { addFloatArg(arg); };
inline void push_back(double arg) { addDoubleArg(arg); };
inline void push_back(const std::string &arg) { addStringArg(arg); };
inline void push_back(const ofBuffer &arg) { addBlobArg(arg); };
template <typename type>
ExtendedOscMessage &operator<<(type &&arg) {
push_back(std::forward<type>(arg));
return *this;
}
};
};
using ofxExtendedOscMessage = ofx::ExtendedOscMessage;
using ofxExOscMessage = ofxExtendedOscMessage;
#endif /* ofxExtendedOscMessage_h */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment