Music Hub  ..
A session-wide music playback service
utils.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2016 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17 
18 #ifndef UTILS_H_
19 #define UTILS_H_
20 
21 #include <boost/format.hpp>
22 
23 #include <string>
24 #include <vector>
25 
26 #define MCS_STR_VALUE(str) #str
27 
28 namespace core {
29 namespace ubuntu {
30 namespace media {
31 typedef int64_t TimestampNs;
32 typedef int64_t TimestampUs;
33 struct Utils
34 {
35  // Merely used as a namespace.
36  Utils() = delete;
37 
38  // Sprintf - much like what you would expect :)
39  template<typename... Types>
40  static std::string Sprintf(const std::string& fmt_str, Types&&... args);
41  // GetEnv - returns a variable value from the environment
42  static uint64_t GetNowNs();
43  // GetNowUs - get a timestamp in microseconds
44  static uint64_t GetNowUs();
45 };
46 
47 namespace impl {
48 // Base case, just return the passed in boost::format instance.
49 inline boost::format& Sprintf(boost::format& f)
50 {
51  return f;
52 }
53 // Sprintf recursively walks the parameter pack at compile time.
54 template <typename Head, typename... Tail>
55 inline boost::format& Sprintf(boost::format& f, Head const& head, Tail&&... tail) {
56  return Sprintf(f % head, std::forward<Tail>(tail)...);
57 }
58 } // namespace impl
59 } // namespace media
60 } // namespace ubuntu
61 } // namespace core
62 
63 template <typename... Types>
64 inline std::string core::ubuntu::media::Utils::Sprintf(const std::string& format, Types&&... args) {
65  boost::format f(format);
66  return core::ubuntu::media::impl::Sprintf(f, std::forward<Types>(args)...).str();
67 }
68 
69 #endif
int64_t TimestampNs
Definition: utils.h:31
Definition: player.h:33
static uint64_t GetNowNs()
Definition: utils.cpp:32
static std::string Sprintf(const std::string &fmt_str, Types &&... args)
Definition: utils.h:64
boost::format & Sprintf(boost::format &f)
Definition: utils.h:49
int64_t TimestampUs
Definition: utils.h:32
static uint64_t GetNowUs()
Definition: utils.cpp:39