2#ifndef __ASE_UTILS_HH__
3#define __ASE_UTILS_HH__
7#include <experimental/type_traits>
13const char* ase_gettext (
const String &untranslated);
14template<
class A0,
class... Ar>
const char* ase_gettext (
const char *format,
const A0 &a0,
const Ar &...restargs) ASE_PRINTF (1, 0);
24IconString
operator""_uc (
const char *key,
size_t);
25IconString
operator""_icon (
const char *key,
size_t);
30template<
typename MkFun,
size_t ...INDICES>
static auto
33 constexpr size_t N =
sizeof... (INDICES);
43template<std::
size_t LAST,
typename MkFun>
static auto
44make_case_table (
const MkFun &mkjump)
54 void operator= (
const EventFd&) =
delete;
90 static_assert (
sizeof (custom_data_) ==
sizeof (
void*));
91 CustomDataEntry& custom_data_entry (
VirtualBase *key);
96 void custom_data_destroy ();
100 {
std::any a (data); custom_data_entry (key).swap (a); }
103 {
return key->extract (custom_data_get (key)); }
106 {
return key->has_value (custom_data_get (key)); }
109 {
return custom_data_del (key); }
126 return (v >> 8) | (v << 8);
132 return __builtin_bswap32 (v);
133 return ( ((v & 0x000000ffU) << 24) |
134 ((v & 0x0000ff00U) << 8) |
135 ((v & 0x00ff0000U) >> 8) |
136 ((v & 0xff000000U) >> 24) );
142 return __builtin_bswap64 (v);
143 return ( ((v & 0x00000000000000ffUL) << 56) |
144 ((v & 0x000000000000ff00UL) << 40) |
145 ((v & 0x0000000000ff0000UL) << 24) |
146 ((v & 0x00000000ff000000UL) << 8) |
147 ((v & 0x000000ff00000000UL) >> 8) |
148 ((v & 0x0000ff0000000000UL) >> 24) |
149 ((v & 0x00ff000000000000UL) >> 40) |
150 ((v & 0xff00000000000000UL) >> 56) );
154template<
class A0,
class... Ar>
const char*
155ase_gettext (
const char *format,
const A0 &a0,
const Ar &...restargs)
157 return ase_gettext (
string_format (format, a0, restargs...));
164using callable_reserve_int =
decltype (
std::declval<T&>().reserve (
int (0)));
169template<
class Container,
class Iteratable> Container
183template<
typename RandIter,
class Cmp,
typename Arg,
int case_lookup_or_sibling_or_insertion>
185binary_lookup_fuzzy (RandIter begin, RandIter end, Cmp cmp_elements,
const Arg &arg)
187 RandIter current = end;
188 size_t n_elements = end - begin, offs = 0;
189 const bool want_lookup = case_lookup_or_sibling_or_insertion == 0;
191 const bool want_insertion_pos = case_lookup_or_sibling_or_insertion > 1;
193 while (offs < n_elements)
195 size_t i = (offs + n_elements) >> 1;
197 cmp = cmp_elements (arg, *current);
199 return want_insertion_pos ?
std::make_pair (current,
true) :
std::make_pair (current, false);
208 : (want_insertion_pos && cmp > 0)
209 ?
std::make_pair (current + 1, false)
210 :
std::make_pair (current, false));
218template<
typename RandIter,
class Cmp,
typename Arg>
222 return binary_lookup_fuzzy<RandIter,Cmp,Arg,2> (begin, end, cmp_elements, arg);
230template<
typename RandIter,
class Cmp,
typename Arg>
231extern inline RandIter
234 return binary_lookup_fuzzy<RandIter,Cmp,Arg,1> (begin, end, cmp_elements, arg).first;
242template<
typename RandIter,
class Cmp,
typename Arg>
243extern inline RandIter
244binary_lookup (RandIter begin, RandIter end, Cmp cmp_elements,
const Arg &arg)
247 return binary_lookup_fuzzy<RandIter,Cmp,Arg,0> (begin, end, cmp_elements, arg).first;
251template<
typename Value>
static inline int
252compare_lesser (
const Value &v1,
const Value &v2)
254 return -(v1 < v2) | (v2 < v1);
258template<
typename Value>
static inline int
259compare_greater (
const Value &v1,
const Value &v2)
261 return (v1 < v2) | -(v2 < v1);
267template<
class C>
inline size_t
270 for (
auto iter = container.begin(); iter != container.end(); iter++)
273 container.erase (iter);
280template<
class C>
inline size_t
284 for (
auto iter = container.begin(); iter != container.end(); )
287 iter = container.erase (iter);
296template<
typename C>
inline bool
299 for (
auto iter = container.begin(); iter != container.end(); iter++)
311 auto it = insmatch.first;
312 return vec.insert (it, value);
315template<
class IterableContainer>
ssize_t
316index_of (
const IterableContainer &c,
const std::function<
bool(
const typename IterableContainer::value_type &e)> &match)
T back_inserter(T... args)
DataListContainer - typesafe storage and retrieval of arbitrary members.
T get_custom_data(CustomDataKey< T > *key) const
Retrieve contents of the custom keyed data member, returns DataKey::fallback if nothing was set.
bool has_custom_data(CustomDataKey< T > *key) const
Retrieve wether contents of the custom keyed data member exists.
bool del_custom_data(CustomDataKey< T > *key)
Delete the current contents of the custom keyed data member, invokes DataKey::destroy.
void set_custom_data(CustomDataKey< T > *key, T data)
Assign data to the custom keyed data member, deletes any previously set data.
CustomDataKey objects are used to identify and manage custom data members of CustomDataContainer obje...
virtual T fallback()
Return default T instance.
const std::type_info & type() const noexcept
Return the typeid of T.
bool opened()
Indicates whether eventfd has been opened.
void flush()
Clear pending wakeups.
int inputfd()
Returns the file descriptor for POLLIN.
bool pollin()
Checks whether events are pending.
int open()
Opens the eventfd and returns -errno.
void wakeup()
Wakeup polling end.
std::vector< T >::iterator insert_sorted(std::vector< T > &vec, const T &value, Compare compare)
Insert value into sorted vec using binary_lookup_insertion_pos() with compare.
Container container_copy(const Iteratable &source)
Create a Container with copies of the elements of source.
RandIter binary_lookup_sibling(RandIter begin, RandIter end, Cmp cmp_elements, const Arg &arg)
Perform a binary lookup to yield exact or nearest match.
size_t erase_all(C &container, const std::function< bool(typename C::value_type const &value)> &pred)
Erase all elements for which pred() is true in vector or list.
size_t erase_first(C &container, const std::function< bool(typename C::value_type const &value)> &pred)
Erase first element for which pred() is true in vector or list.
RandIter binary_lookup(RandIter begin, RandIter end, Cmp cmp_elements, const Arg &arg)
Perform binary lookup and yield exact match or end.
std::pair< RandIter, bool > binary_lookup_insertion_pos(RandIter begin, RandIter end, Cmp cmp_elements, const Arg &arg)
Perform a binary lookup to find the insertion position for a new element.
bool contains(const C &container, const std::function< bool(typename C::value_type const &value)> &pred)
Returns true if container element for which pred() is true.
The Anklang C++ API namespace.
std::string string_format(const char *format, const Args &...args) __attribute__((__format__(__printf__
Format a string similar to sprintf(3) with support for std::string and std::ostringstream convertible...
constexpr uint64_t uint64_swap_le_be(uint64_t v)
Swap 64-Bit integers between __BIG_ENDIAN and __LITTLE_ENDIAN systems.
constexpr uint32_t uint32_swap_le_be(uint32_t v)
Swap 32-Bit integers between __BIG_ENDIAN and __LITTLE_ENDIAN systems.
constexpr uint16_t uint16_swap_le_be(uint16_t v)
Swap 16-Bit integers between __BIG_ENDIAN and __LITTLE_ENDIAN systems.
std::string String
Convenience alias for std::string.
Value type used to interface with various property types.
Common base type to allow casting between polymorphic classes.
IconString SvgIcon(const String &svgdata)
Create an IconString consisting of an SVG string.
IconString UcIcon(const String &unicode)
Create an IconString consisting of a single/double unicode character.
IconString KwIcon(const String &keywords)
Create an IconString consisting of keywords.