RAVENNAKIT
Loading...
Searching...
No Matches
stl_helpers.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3 * Project: RAVENNAKIT (RAVENNA / AES67 / ST2110-30 SDK)
4 * Copyright (c) 2024-2025 Sound on Digital
5 *
6 * This file is part of RAVENNAKIT.
7 *
8 * RAVENNAKIT is dual-licensed:
9 * 1) Under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version (the "AGPL License"); and
12 * 2) Under a commercial license from Sound on Digital, for customers who
13 * cannot (or do not wish to) comply with the AGPL License terms.
14 *
15 * If you obtained this file under the AGPL License, you may redistribute it
16 * and/or modify it under the terms of the AGPL License. See the LICENSE
17 * file in the project root for details.
18 *
19 * For commercial licensing, support, and other inquiries, please visit:
20 *
21 * https://ravennakit.com
22 *
23 */
24
25#pragma once
26
27#include <algorithm>
28#include <array>
29#include <utility>
30
31namespace rav {
32
41template<typename Container, typename Pred>
42size_t stl_remove_if(Container& container, Pred pred) {
43 auto old_size = container.size();
44 container.erase(std::remove_if(container.begin(), container.end(), pred), container.end());
45 return old_size - container.size();
46}
47
56template<typename Map, typename Key>
57auto stl_get_or_default(const Map& map, const Key& key) -> typename Map::mapped_type {
58 if (auto it = map.find(key); it != map.end())
59 return it->second;
60 return typename Map::mapped_type {};
61}
62
63// Helper for `generate_array`
64template<typename T, std::size_t N, typename F, std::size_t... Is>
65std::array<T, N> generate_array_impl(F&& f, std::index_sequence<Is...>) {
66 return {{f(Is)...}};
67}
68
72template<typename T, std::size_t N, typename F>
73std::array<T, N> generate_array(F&& f) {
74 return generate_array_impl<T, N>(std::forward<F>(f), std::make_index_sequence<N> {});
75}
76
77} // namespace rav
size_t stl_remove_if(Container &container, Pred pred)
Convenience function to remove items from an stl container which match pred.
std::array< T, N > generate_array(F &&f)
Helper function to initialize std::array with non-copyable types.
auto stl_get_or_default(const Map &map, const Key &key) -> typename Map::mapped_type
Convenience function to get an item from a map, or if the item doesn't exist return a default value.
std::array< T, N > generate_array_impl(F &&f, std::index_sequence< Is... >)