mf
Media Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
misc.h
Go to the documentation of this file.
1 #ifndef MF_UTILITY_MISC_H_
2 #define MF_UTILITY_MISC_H_
3 
4 #include <string>
5 #include <vector>
6 
7 namespace mf {
8 
10 bool file_exists(const std::string& filename);
11 
13 template<typename Numeric>
14 Numeric sq(Numeric n) { return n * n; }
15 
17 
18 template<typename T>
19 T* advance_raw_ptr(T* ptr, std::ptrdiff_t diff) noexcept;
20 
22 template<typename T>
23 T clamp(T value, T minimum, T maximum);
24 
26 template<typename T>
27 T gcd(T a, T b);
28 
30 template<typename T>
31 bool is_power_of_two(T x) {
32  return (x != 0) && !(x & (x - 1));
33 }
34 
36 template<typename T>
37 bool is_multiple_of(T x, T base) {
38  return (x % base == 0);
39 }
40 
42 template<typename T>
43 bool is_nonzero_multiple_of(T x, T base) {
44  return (x != 0) && is_multiple_of(x, base);
45 }
46 
48 template<typename T>
49 bool is_odd(T x) { return (x % 2) != 0; }
50 
52 template<typename T>
53 bool is_even(T x) { return (x % 2) == 0; }
54 
55 }
56 
57 #include "misc.tcc"
58 
59 #endif
bool is_odd(T x)
Check if x is odd.
Definition: misc.h:49
bool is_power_of_two(T x)
Check if x is a power of 2.
Definition: misc.h:31
bool is_multiple_of(T x, T base)
Check if x is a multiple of base, including zero.
Definition: misc.h:37
bool is_even(T x)
Check if x is even.
Definition: misc.h:53
bool file_exists(const std::string &filepath)
Check if a file at filename exists.
Definition: misc.cc:8
T clamp(T value, T minimum, T maximum)
Clamp value between minimum and maximum value.
Definition: misc.tcc:17
Numeric sq(Numeric n)
Compute square of a number.
Definition: misc.h:14
T gcd(T a, T b)
Compute greatest common divisor of a and b.
Definition: misc.tcc:25
T * advance_raw_ptr(T *ptr, std::ptrdiff_t diff) noexcept
Advance a pointer ptr of any type by diff bytes.
Definition: misc.tcc:9
bool is_nonzero_multiple_of(T x, T base)
Check if x is a non-zero multiple of base.
Definition: misc.h:43