2023-04-23 11:34:35 -04:00
|
|
|
/*
|
|
|
|
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
|
2023-05-13 11:32:58 -04:00
|
|
|
* @param name
|
2023-04-23 11:34:35 -04:00
|
|
|
*/
|
2023-05-13 11:32:58 -04:00
|
|
|
Method::Method(Method *parent, std::string name)
|
2023-04-23 11:34:35 -04:00
|
|
|
: parent_(parent)
|
|
|
|
{
|
|
|
|
if (parent)
|
|
|
|
parent->addChild(this);
|
2023-05-13 11:32:58 -04:00
|
|
|
setName(name);
|
2023-04-23 11:34:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Method::~Method()
|
|
|
|
{
|
2023-04-23 19:29:21 -04:00
|
|
|
for (auto &child: children_)
|
2023-04-23 11:34:35 -04:00
|
|
|
delete child;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-16 10:28:14 -04:00
|
|
|
/**
|
|
|
|
* @brief Method::isRoot
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
bool Method::isRoot() const
|
|
|
|
{
|
|
|
|
return !parent_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Method::isContainer
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
bool Method::isContainer() const
|
|
|
|
{
|
|
|
|
return !children_.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-23 11:34:35 -04:00
|
|
|
/**
|
|
|
|
* @brief Method::addChild
|
|
|
|
* @param child
|
|
|
|
*/
|
|
|
|
void Method::addChild(Method *child)
|
|
|
|
{
|
|
|
|
children_.push_back(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Method::name
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
std::string Method::name() const
|
|
|
|
{
|
|
|
|
return name_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Method::setName
|
|
|
|
* @param name
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
bool Method::setName(std::string name)
|
|
|
|
{
|
2023-05-13 11:33:45 -04:00
|
|
|
std::regex bad_chars(R"([ #*,/\?\[\]\{\}])"); // ' ' # * , / ? [ ] { }
|
2023-04-23 11:34:35 -04:00
|
|
|
if (std::regex_search(name, bad_chars))
|
|
|
|
return false;
|
|
|
|
name_ = name;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-05-16 09:52:18 -04:00
|
|
|
* @brief Method::matchName
|
|
|
|
* @param pattern
|
2023-04-23 11:34:35 -04:00
|
|
|
* @return
|
|
|
|
*/
|
2023-05-16 09:52:18 -04:00
|
|
|
bool Method::matchName(const std::string &pattern) const
|
2023-04-23 11:34:35 -04:00
|
|
|
{
|
2023-05-16 09:52:18 -04:00
|
|
|
// try to optimize matches before resorting to expensive regex matching
|
|
|
|
if (isRoot())
|
|
|
|
return true; // root method matches everything
|
|
|
|
if (name_.compare(pattern) == 0)
|
|
|
|
return true; // exact match
|
|
|
|
if (pattern.empty())
|
|
|
|
return false; // reject non-exact nulls
|
|
|
|
if (pattern == "*")
|
|
|
|
return true; // wildcard match
|
|
|
|
|
|
|
|
std::regex re(pattern);
|
|
|
|
return std::regex_match(name_, re); // regex match
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Method::address
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
std::string Method::address() const
|
|
|
|
{
|
|
|
|
if (!parent_)
|
|
|
|
return name_; // root container
|
|
|
|
return parent_->address() + "/" + name_;
|
|
|
|
}
|
|
|
|
|
2023-04-23 11:34:35 -04:00
|
|
|
|
2023-05-16 09:52:18 -04:00
|
|
|
/**
|
|
|
|
* @brief Method::matchAddress
|
|
|
|
* @param hits
|
|
|
|
* @param pattern
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
bool Method::matchAddress(std::vector<const Method *> &hits, std::list<std::string> pattern) const
|
|
|
|
{
|
|
|
|
if (pattern.empty()) // NO pattern to match
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto name = pattern.front(); // pattern token to consider
|
2023-05-15 11:19:29 -04:00
|
|
|
if (!isRoot())
|
2023-05-16 09:52:18 -04:00
|
|
|
pattern.pop_front(); // take the token, if not the root method
|
2023-04-23 11:34:35 -04:00
|
|
|
|
2023-05-16 09:52:18 -04:00
|
|
|
if (pattern.empty()) // LEAF pattern
|
2023-04-23 11:34:35 -04:00
|
|
|
{
|
|
|
|
if (isContainer()) // containters cannot be leafs
|
|
|
|
return false;
|
2023-05-16 09:52:18 -04:00
|
|
|
if (matchName(name)) // token matches this method
|
2023-04-23 11:34:35 -04:00
|
|
|
{
|
2023-05-16 09:52:18 -04:00
|
|
|
hits.push_back(this); // claim pattern
|
2023-04-23 11:34:35 -04:00
|
|
|
return true;
|
|
|
|
}
|
2023-05-16 09:52:18 -04:00
|
|
|
} // end LEAF pattern
|
2023-04-23 11:34:35 -04:00
|
|
|
else // BRANCH pattern
|
|
|
|
{
|
|
|
|
if (!isContainer()) // no children to match to
|
|
|
|
return false;
|
2023-05-16 09:52:18 -04:00
|
|
|
if (matchName(name)) // token matches this method
|
2023-04-23 11:34:35 -04:00
|
|
|
{
|
2023-05-16 09:52:18 -04:00
|
|
|
for (const auto &child: children_) // offer the remaining pattern to children
|
|
|
|
child->matchAddress(hits, pattern);
|
2023-04-23 11:34:35 -04:00
|
|
|
return true;
|
|
|
|
}
|
2023-05-16 09:52:18 -04:00
|
|
|
else // token does not match this method
|
2023-04-23 11:34:35 -04:00
|
|
|
{
|
|
|
|
if (name.empty()) // empty patterns, ie "//", apply to all (depth search)
|
|
|
|
{
|
2023-05-16 09:52:18 -04:00
|
|
|
if (!matchAddress(hits, pattern)) // try again with the remaining pattern (depth reached?)
|
2023-04-23 11:34:35 -04:00
|
|
|
{
|
2023-05-16 09:52:18 -04:00
|
|
|
pattern.push_front(name); // return the empty pattern to the list
|
2023-04-23 11:34:35 -04:00
|
|
|
for (const auto &child: children_) // offer full search depth to children
|
2023-05-16 09:52:18 -04:00
|
|
|
child->matchAddress(hits, pattern);
|
2023-04-23 11:34:35 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-16 09:52:18 -04:00
|
|
|
} // end BRANCH pattern
|
|
|
|
return false; // pattern does not match this method
|
2023-04-23 19:30:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
2023-05-15 11:12:45 -04:00
|
|
|
|
2023-05-15 11:18:28 -04:00
|
|
|
/**
|
2023-05-16 09:52:18 -04:00
|
|
|
* @brief Method::onTrigger
|
|
|
|
* @param cb
|
2023-05-15 11:18:28 -04:00
|
|
|
* @return
|
|
|
|
*/
|
2023-05-16 09:52:18 -04:00
|
|
|
std::shared_ptr<void> Method::onTrigger(const std::function<void(std::shared_ptr<Message>)> cb)
|
2023-05-15 11:18:28 -04:00
|
|
|
{
|
2023-05-16 09:52:18 -04:00
|
|
|
// 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;
|
2023-05-15 11:18:28 -04:00
|
|
|
}
|
|
|
|
|
2023-04-23 11:34:35 -04:00
|
|
|
} // namespace OSC
|