207 lines
5.0 KiB
C++
207 lines
5.0 KiB
C++
/*
|
|
osc/method.cpp
|
|
|
|
Copyright (c) 2023 Kevin Matz (kevin.matz@gmail.com)
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
*/
|
|
|
|
#include "method.h"
|
|
#include <regex>
|
|
|
|
namespace OSC {
|
|
|
|
/**
|
|
* @brief Method::Method
|
|
* @param parent
|
|
*/
|
|
Method::Method(Method *parent)
|
|
: parent_(parent)
|
|
, name_("")
|
|
{
|
|
if (parent)
|
|
parent->addChild(this);
|
|
}
|
|
|
|
|
|
Method::~Method()
|
|
{
|
|
for (auto &child: children_)
|
|
delete child;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Method::addChild
|
|
* @param child
|
|
*/
|
|
void Method::addChild(Method *child)
|
|
{
|
|
children_.push_back(child);
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Method::address
|
|
* @return
|
|
*/
|
|
std::string Method::address() const
|
|
{
|
|
if (!parent_)
|
|
return name_; // root container
|
|
return parent_->address() + "/" + name_;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Method::name
|
|
* @return
|
|
*/
|
|
std::string Method::name() const
|
|
{
|
|
return name_;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Method::setName
|
|
* @param name
|
|
* @return
|
|
*/
|
|
bool Method::setName(std::string name)
|
|
{
|
|
std::regex bad_chars("[ #*,/\?\[]\{}]"); // ' ' # * , / ? [ ] { }
|
|
if (std::regex_search(name, bad_chars))
|
|
return false;
|
|
name_ = name;
|
|
return true;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Method::matchesName
|
|
* @param name
|
|
* @return
|
|
*/
|
|
bool Method::matchesName(std::string &name) const
|
|
{
|
|
// try to optimize matches before resorting to expensive pattern matching
|
|
if (name == name_)
|
|
return true; // exact match
|
|
if (name.empty())
|
|
return false; // reject non-exact nulls
|
|
if (name == "*")
|
|
return true; // wildcard match
|
|
|
|
std::regex re(name);
|
|
return std::regex_match(name_, re); // regex match
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Method::isContainer
|
|
* @return
|
|
*/
|
|
bool Method::isContainer() const
|
|
{
|
|
return !children_.empty();
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Method::dispatch
|
|
* @param patterns
|
|
* @param msg
|
|
* @return
|
|
*/
|
|
bool Method::dispatch(std::list<std::string> patterns, const std::shared_ptr<Message> msg) const
|
|
{
|
|
if (patterns.empty())
|
|
return false; // NO pattern to match
|
|
|
|
auto name = patterns.front();
|
|
patterns.pop_front(); // take the first pattern
|
|
|
|
if (patterns.empty()) // LEAF pattern
|
|
{
|
|
if (isContainer()) // containters cannot be leafs
|
|
return false;
|
|
if (matchesName(name)) // accept the message
|
|
{
|
|
trigger(msg); // pass it along
|
|
return true;
|
|
}
|
|
}
|
|
else // BRANCH pattern
|
|
{
|
|
if (!isContainer()) // no children to match to
|
|
return false;
|
|
if (matchesName(name)) // accept pattern
|
|
{
|
|
for (const auto &child: children_) // offer the remaining patterns to children
|
|
child->dispatch(std::list<std::string>(patterns), msg);
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
if (name.empty()) // empty patterns, ie "//", apply to all (depth search)
|
|
{
|
|
if (!dispatch(std::list<std::string>(patterns), msg)) // try again with the remaining patterns
|
|
{
|
|
patterns.push_front(name); // return the empty pattern to the list
|
|
for (const auto &child: children_) // offer full search depth to children
|
|
child->dispatch(std::list<std::string>(patterns), msg);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false; // reject the message
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Method::onTrigger
|
|
* @param cb
|
|
* @return
|
|
*/
|
|
std::shared_ptr<void> Method::onTrigger(const std::function<void(std::shared_ptr<Message>)> cb)
|
|
{
|
|
// wrap the callback with a shared pointer
|
|
auto sp = std::make_shared<std::function<void(std::shared_ptr<Message>)>>(std::move(cb));
|
|
// add callback to list (as a weak pointer)
|
|
cb_trigger.push_back(sp);
|
|
// return token that caller must keep throughout it's scope
|
|
return sp;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Method::trigger
|
|
* @param msg
|
|
*/
|
|
void Method::trigger(std::shared_ptr<Message> msg) const
|
|
{
|
|
for (const auto &wp: cb_trigger)
|
|
if (auto sp = wp.lock()) // the owner is still holding the token
|
|
(*sp)(msg);
|
|
}
|
|
|
|
} // namespace OSC
|