Skip to content

Commit

Permalink
[ossia.cue] Windows & test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Nov 21, 2022
1 parent 14e13c7 commit b69d066
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/ossia-max/src/cue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ static rapidjson::StringBuffer cues_to_string(const ossia::cues& c)
doc.StartObject();
doc.Key("cues");
doc.StartArray();
for(auto& cue : c.cues)
for(auto& cue : c.m_cues)
{
cue_to_json(doc, cue);
}
Expand Down Expand Up @@ -593,7 +593,7 @@ void ocue::read(int argc, t_atom* argv)
return;
}
m_cues->clear();
read_cues_from_json(cues->value.GetArray(), m_cues->cues);
read_cues_from_json(cues->value.GetArray(), m_cues->m_cues);
});
}

Expand Down
65 changes: 32 additions & 33 deletions src/ossia/preset/cue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
#include <ossia/network/base/device.hpp>
#include <ossia/network/base/node.hpp>
#include <ossia/network/common/path.hpp>
#include <boost/container/small_vector.hpp>
#include <boost/container/flat_map.hpp>
#include <boost/container/map.hpp>

#include <cassert>
#include <iostream>

namespace ossia
{
Expand Down Expand Up @@ -114,7 +115,7 @@ void cues::recall(int idx)
{
if(idx < 0)
return;
if(idx >= std::ssize(cues))
if(idx >= std::ssize(m_cues))
return;
m_current = idx;
recall();
Expand All @@ -130,9 +131,10 @@ struct priority_sort
void cues::recall()
{
auto& root = dev->get_root_node();
boost::container::small_flat_map<ossia::net::parameter_base*, ossia::value*, 512, priority_sort> params;
params.reserve(cues[m_current].preset.size());
for(auto& [addr, val] : cues[m_current].preset)

boost::container::small_flat_multimap<ossia::net::parameter_base*, ossia::value*, 512, priority_sort> params;
params.reserve(m_cues[m_current].preset.size());
for(auto& [addr, val] : this->m_cues[m_current].preset)
{
// No pattern in saved cue
if(auto n = ossia::net::find_node(root, addr))
Expand All @@ -141,10 +143,8 @@ void cues::recall()
params.emplace(p, &val);
}

for(auto [p, v] : params)
{
p->push_value(*v);
}
for(auto elt : params)
elt.first->push_value(*elt.second);
}

void cues::remove()
Expand Down Expand Up @@ -189,7 +189,6 @@ void cues::namespace_deselect(std::string_view pattern)
*/

auto nodes = ossia::net::find_nodes(dev->get_root_node(), pattern);
// std::cout << "namespace_deselect: " << pattern << " : " << nodes.size() << std::endl;

// v1
// Actually this does not change the preset at all. Only the selection
Expand Down Expand Up @@ -238,28 +237,28 @@ void cues::on_node_removed(const net::node_base& n)
int cues::get_cue(std::string_view name)
{
auto it = std::find_if(
cues.begin(), cues.end(), [=](const cue& c) { return c.name == name; });
if(BOOST_UNLIKELY(it == cues.end()))
m_cues.begin(), m_cues.end(), [=](const cue& c) { return c.name == name; });
if(BOOST_UNLIKELY(it == m_cues.end()))
{
cues.push_back(cue{.name = std::string(name)});
return cues.size() - 1;
m_cues.push_back(cue{.name = std::string(name)});
return m_cues.size() - 1;
}
else
{
return std::distance(cues.begin(), it);
return std::distance(m_cues.begin(), it);
}
}
std::optional<int> cues::find_cue(std::string_view name)
{
auto it = std::find_if(
cues.begin(), cues.end(), [=](const cue& c) { return c.name == name; });
if(BOOST_UNLIKELY(it == cues.end()))
m_cues.begin(), m_cues.end(), [=](const cue& c) { return c.name == name; });
if(BOOST_UNLIKELY(it == m_cues.end()))
{
return std::nullopt;
}
else
{
return std::distance(cues.begin(), it);
return std::distance(m_cues.begin(), it);
}
}

Expand All @@ -274,10 +273,10 @@ void cues::remove(int idx)
{
if(idx < 0)
return;
if(idx >= std::ssize(cues))
if(idx >= std::ssize(m_cues))
return;

cues.erase(cues.begin() + idx);
m_cues.erase(m_cues.begin() + idx);

if(m_current < idx)
{
Expand All @@ -286,7 +285,7 @@ void cues::remove(int idx)
else if(m_current == idx)
{
// We keep the same index selected
if(m_current >= std::ssize(cues))
if(m_current >= std::ssize(m_cues))
{
m_current -= 1;
}
Expand All @@ -298,21 +297,21 @@ void cues::remove(int idx)
m_current -= 1;
}

if(cues.empty())
if(m_cues.empty())
{
cues.push_back({.name{"Init"}});
m_cues.push_back({.name{"Init"}});
m_current = 0;
}
}

void cues::remove(std::string_view name)
{
auto it = std::find_if(
cues.begin(), cues.end(), [=](const cue& c) { return c.name == name; });
this->m_cues.begin(), this->m_cues.end(), [=](const cue& c) { return c.name == name; });

if(it != cues.end())
if(it != this->m_cues.end())
{
int idx = std::distance(cues.begin(), it);
int idx = std::distance(this->m_cues.begin(), it);
remove(idx);
}
}
Expand All @@ -330,8 +329,8 @@ void cues::update(int idx)
if(!dev)
return;
assert(idx >= 0);
assert(idx < std::ssize(cues));
auto& cue = cues[idx];
assert(idx < std::ssize(this->m_cues));
auto& cue = this->m_cues[idx];

// v1
/*
Expand Down Expand Up @@ -363,7 +362,7 @@ void cues::update(int idx)
}

// And we also add the new ones
for(auto node : m_selection)
for(auto node : this->m_selection)
{
if(auto p = node->get_parameter())
{
Expand Down Expand Up @@ -419,7 +418,7 @@ void cues::output(std::string_view name, std::string_view pattern)

void cues::clear()
{
cues.clear();
m_cues.clear();
}

void cues::move(std::string_view name, int to)
Expand All @@ -432,7 +431,7 @@ void cues::move(int from, int to)
{
if(from < 0 || to < 0 || from == to)
return;
change_item_position(cues, from, to);
change_item_position(m_cues, from, to);
}

static bool filter_all_pass(const ossia::net::node_base& n, const selection_filters &pat)
Expand All @@ -456,7 +455,7 @@ static bool filter_all_pass(const ossia::net::node_base& n, const selection_filt
if(nn->get_value_type() != v)
return false;
}
for(auto v : pat.tags)
for(const auto& v : pat.tags)
{
const auto& tags = ossia::net::get_tags(n);
if(!tags || !ossia::contains(*tags, v))
Expand Down Expand Up @@ -485,7 +484,7 @@ static bool filter_any_pass(const ossia::net::node_base& n, const selection_filt
if(nn->get_value_type() == v)
return true;
}
for(auto v : pat.tags)
for(const auto& v : pat.tags)
{
const auto& tags = ossia::net::get_tags(n);
if(tags && ossia::contains(*tags, v))
Expand Down
10 changes: 6 additions & 4 deletions src/ossia/preset/cue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <nano_observer.hpp>

#include <unordered_set>
#include <optional>

namespace ossia
{
namespace net
Expand Down Expand Up @@ -35,15 +37,15 @@ struct selection_filters
class OSSIA_EXPORT cues : Nano::Observer
{
public:
std::vector<cue> cues{{.name{"Init"}}};
std::vector<cue> m_cues{{.name{"Init"}}};

void set_device(ossia::net::device_base* dev);

int size() const noexcept { return cues.size(); }
cue& current_cue() noexcept { return cues[m_current]; }
int size() const noexcept { return this->m_cues.size(); }
cue& current_cue() noexcept { return this->m_cues[m_current]; }
cue* get_cue(int idx) noexcept
{
return (idx >= 0 && idx < std::ssize(cues)) ? &cues[idx] : nullptr;
return (idx >= 0 && idx < std::ssize(this->m_cues)) ? &this->m_cues[idx] : nullptr;
}
std::optional<int> find_cue(std::string_view name);
int get_cue(std::string_view name);
Expand Down
10 changes: 9 additions & 1 deletion tests/Preset/CueTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,16 @@ TEST_CASE("test_cue", "test_cue")

WHEN("recalling the cue")
{
// Check that the device is in the right state
REQUIRE(device.a1.value() == 13579);
REQUIRE(device.a2.value() == 3.1415);
REQUIRE(device.a3.value() == "foo");
REQUIRE(device.a4.value() == "bar");

c.create("Cue 1");
c.update();
REQUIRE(c.m_cues[1].name == "Cue 1");
REQUIRE(c.m_cues[1].preset.size() == 4);

device.a1.set_value(10);
device.a2.set_value(1.2);
Expand Down Expand Up @@ -158,7 +166,7 @@ TEST_CASE("test_cue_ns", "test_cue_ns")
REQUIRE(c.m_selection.empty());

c.namespace_select("/bim");
REQUIRE(c.m_selection.size() == 3);
REQUIRE(c.m_selection.size() == 4);

c.update();

Expand Down

0 comments on commit b69d066

Please sign in to comment.