Shaka Player Embedded
utils.h
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_UTIL_UTILS_H_
16 #define SHAKA_EMBEDDED_UTIL_UTILS_H_
17 
18 #include <glog/logging.h>
19 #include <stdarg.h>
20 #include <stdint.h>
21 
22 #include <algorithm>
23 #include <functional>
24 #include <mutex>
25 #include <string>
26 #include <type_traits>
27 #include <unordered_set>
28 #include <utility>
29 #include <vector>
30 
31 #include "src/util/macros.h"
32 
33 namespace shaka {
34 namespace util {
35 
41 struct Finally {
42  explicit Finally(std::function<void()> call);
43  Finally(const Finally&) = delete;
44  Finally(Finally&&) = delete;
45  ~Finally();
46 
47  std::function<void()> call_;
48 };
49 inline Finally::Finally(std::function<void()> call) : call_(std::move(call)) {}
51  call_();
52 }
53 
54 
58 template <typename _Mutex>
59 struct Unlocker {
60  explicit Unlocker(std::unique_lock<_Mutex>* lock) : lock_(lock) {
61  DCHECK(lock_->owns_lock());
62  lock_->unlock();
63  }
65  lock_->lock();
66  }
67 
69 
70  std::unique_lock<_Mutex>* lock_;
71 };
72 
73 
74 PRINTF_FORMAT(1, 2)
75 std::string StringPrintf(const char* format, ...);
76 PRINTF_FORMAT(1, 0)
77 std::string StringPrintfV(const char* format, va_list va);
78 
79 std::vector<std::string> StringSplit(const std::string& source, char split_on);
80 
81 std::string ToAsciiLower(const std::string& source);
82 std::string TrimAsciiWhitespace(const std::string& source);
83 
84 std::string ToHexString(const uint8_t* data, size_t data_size);
85 
86 template <typename T, typename U>
87 bool contains(const std::vector<T>& vec, U&& elem) {
88  return std::find(vec.begin(), vec.end(), std::forward<U>(elem)) != vec.end();
89 }
90 template <typename T, typename U>
91 bool contains(const std::unordered_set<T>& set, U&& elem) {
92  return set.count(std::forward<U>(elem)) != 0;
93 }
94 template <typename List, typename Elem>
95 void RemoveElement(List* list, Elem&& elem) {
96  // std::remove modifies a list to put the desired element at the end.
97  // The call to erase() then removes the element.
98  list->erase(std::remove(list->begin(), list->end(), std::forward<Elem>(elem)),
99  list->end());
100 }
101 
102 } // namespace util
103 } // namespace shaka
104 
105 #endif // SHAKA_EMBEDDED_UTIL_UTILS_H_
std::string ToAsciiLower(const std::string &source)
Definition: utils.cc:76
Unlocker(std::unique_lock< _Mutex > *lock)
Definition: utils.h:60
Finally(std::function< void()> call)
Definition: utils.h:49
std::string ToHexString(const uint8_t *data, size_t data_size)
Definition: utils.cc:97
bool contains(const std::vector< T > &vec, U &&elem)
Definition: utils.h:87
std::string StringPrintf(const char *format,...)
Definition: utils.cc:49
#define SHAKA_NON_COPYABLE_OR_MOVABLE_TYPE(Type)
Definition: macros.h:51
const char * source
Definition: media_utils.cc:30
std::function< void()> call_
Definition: utils.h:47
std::unique_lock< _Mutex > * lock_
Definition: utils.h:70
std::string StringPrintfV(const char *format, va_list va)
Definition: utils.cc:58
void RemoveElement(List *list, Elem &&elem)
Definition: utils.h:95
std::string TrimAsciiWhitespace(const std::string &source)
Definition: utils.cc:82
std::vector< std::string > StringSplit(const std::string &source, char split_on)
Definition: utils.cc:64
#define PRINTF_FORMAT(format, dots)
Definition: macros.h:39