1
0
Fork 0
OpenLCP/protocol/osc/method.cpp

221 lines
5.5 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
* @param name
*/
Method::Method(Method *parent, std::string name)
: parent_(parent)
{
if (parent)
parent->addChild(this);
setName(name);
}
Method::~Method()
{
for (auto &child: children_)
delete child;
}
/**
* @brief Method::isRoot
* @return
*/
bool Method::isRoot() const
{
return !parent_;
}
/**
* @brief Method::isContainer
* @return
*/
bool Method::isContainer() const
{
return !children_.empty();
}
/**
* @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)
{
std::regex bad_chars(R"([ #*,/\?\[\]\{\}])"); // ' ' # * , / ? [ ] { }
if (std::regex_search(name, bad_chars))
return false;
name_ = name;
return true;
}
/**
* @brief Method::matchName
* @param pattern
* @return
*/
bool Method::matchName(const std::string &pattern) const
{
// 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_;
}
/**
* @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
if (!isRoot())
pattern.pop_front(); // take the token, if not the root method
if (pattern.empty()) // LEAF pattern
{
if (isContainer()) // containters cannot be leafs
return false;
if (matchName(name)) // token matches this method
{
hits.push_back(this); // claim pattern
return true;
}
} // end LEAF pattern
else // BRANCH pattern
{
if (!isContainer()) // no children to match to
return false;
if (matchName(name)) // token matches this method
{
for (const auto &child: children_) // offer the remaining pattern to children
child->matchAddress(hits, pattern);
return true;
}
else // token does not match this method
{
if (name.empty()) // empty patterns, ie "//", apply to all (depth search)
{
if (!matchAddress(hits, pattern)) // try again with the remaining pattern (depth reached?)
{
pattern.push_front(name); // return the empty pattern to the list
for (const auto &child: children_) // offer full search depth to children
child->matchAddress(hits, pattern);
return true;
}
}
}
} // end BRANCH pattern
return false; // pattern does not match this method
}
/**
* @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);
}
/**
* @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;
}
} // namespace OSC