clean up indent for utils
This commit is contained in:
parent
36fe7fcff4
commit
b37a056c2e
|
@ -24,17 +24,17 @@
|
|||
template<typename T>
|
||||
struct Addressable
|
||||
{
|
||||
/* Value */
|
||||
T value;
|
||||
/* Original virtual address */
|
||||
uint32_t address;
|
||||
/* Value */
|
||||
T value;
|
||||
/* Original virtual address */
|
||||
uint32_t address;
|
||||
|
||||
Addressable() = default;
|
||||
Addressable(T value): value(value), address(-1) {}
|
||||
Addressable(uint32_t addr, T value): value(value), address(addr) {}
|
||||
Addressable() = default;
|
||||
Addressable(T value): value(value), address(-1) {}
|
||||
Addressable(uint32_t addr, T value): value(value), address(addr) {}
|
||||
|
||||
/* Implicitly decay to the base type */
|
||||
operator T () const { return value; }
|
||||
/* Implicitly decay to the base type */
|
||||
operator T () const { return value; }
|
||||
};
|
||||
|
||||
#endif /* FXOS_UTIL_ADDRESSABLE_H */
|
||||
|
|
|
@ -21,34 +21,34 @@
|
|||
|
||||
/* Format a string with printf() syntax */
|
||||
template<typename ... Args>
|
||||
std::string format_do(std::string const &format, Args && ... args)
|
||||
std::string _format_do(std::string const &format, Args && ... args)
|
||||
{
|
||||
/* Reserve space for snprintf() to put its NUL */
|
||||
size_t size = snprintf(nullptr, 0, format.c_str(), args ...) + 1;
|
||||
/* Reserve space for snprintf() to put its NUL */
|
||||
size_t size = snprintf(nullptr, 0, format.c_str(), args ...) + 1;
|
||||
|
||||
std::unique_ptr<char[]> buf(new char[size]);
|
||||
snprintf(buf.get(), size, format.c_str(), args ...);
|
||||
std::unique_ptr<char[]> buf(new char[size]);
|
||||
snprintf(buf.get(), size, format.c_str(), args ...);
|
||||
|
||||
/* Remove the NUL from the string */
|
||||
return std::string(buf.get(), buf.get() + size - 1);
|
||||
/* Remove the NUL from the string */
|
||||
return std::string(buf.get(), buf.get() + size - 1);
|
||||
}
|
||||
|
||||
/* Convert std::string to char * when printing (by @Zitrax on Github) */
|
||||
template<typename T>
|
||||
auto format_convert(T&& t)
|
||||
auto _format_convert(T&& t)
|
||||
{
|
||||
if constexpr(std::is_same<std::remove_cv_t<std::remove_reference_t<T>>,
|
||||
std::string>::value)
|
||||
return std::forward<T>(t).c_str();
|
||||
else
|
||||
return std::forward<T>(t);
|
||||
if constexpr(std::is_same<std::remove_cv_t<std::remove_reference_t<T>>,
|
||||
std::string>::value)
|
||||
return std::forward<T>(t).c_str();
|
||||
else
|
||||
return std::forward<T>(t);
|
||||
}
|
||||
|
||||
/* String formatting with std::string support in %s */
|
||||
template<typename ... Args>
|
||||
std::string format(std::string const &format, Args && ... args)
|
||||
{
|
||||
return format_do(format, format_convert(std::forward<Args>(args))...);
|
||||
return _format_do(format, _format_convert(std::forward<Args>(args))...);
|
||||
}
|
||||
|
||||
#endif /* FXOS_UTIL_FORMAT_H */
|
||||
|
|
|
@ -9,45 +9,45 @@
|
|||
#include <fxos/util/format.h>
|
||||
|
||||
Timer::Timer():
|
||||
time_ns {0}, running {false}, start_time {}, depth {0}
|
||||
time_ns {0}, running {false}, start_time {}, depth {0}
|
||||
{
|
||||
}
|
||||
|
||||
void Timer::start(void)
|
||||
{
|
||||
if(this->depth++ == 0) {
|
||||
this->running = true;
|
||||
clock_gettime(CLOCK_REALTIME, &this->start_time);
|
||||
}
|
||||
if(this->depth++ == 0) {
|
||||
this->running = true;
|
||||
clock_gettime(CLOCK_REALTIME, &this->start_time);
|
||||
}
|
||||
}
|
||||
|
||||
void Timer::stop(void)
|
||||
{
|
||||
if(--this->depth == 0) {
|
||||
this->running = false;
|
||||
struct timespec end;
|
||||
clock_gettime(CLOCK_REALTIME, &end);
|
||||
if(--this->depth == 0) {
|
||||
this->running = false;
|
||||
struct timespec end;
|
||||
clock_gettime(CLOCK_REALTIME, &end);
|
||||
|
||||
this->time_ns += 1000000000 * (end.tv_sec - this->start_time.tv_sec)
|
||||
+ (end.tv_nsec - this->start_time.tv_nsec);
|
||||
this->time_ns += 1000000000 * (end.tv_sec - this->start_time.tv_sec)
|
||||
+ (end.tv_nsec - this->start_time.tv_nsec);
|
||||
|
||||
this->start_time.tv_sec = 0;
|
||||
this->start_time.tv_nsec = 0;
|
||||
}
|
||||
this->start_time.tv_sec = 0;
|
||||
this->start_time.tv_nsec = 0;
|
||||
}
|
||||
}
|
||||
|
||||
std::string Timer::format_time(uint64_t time_ns)
|
||||
{
|
||||
if(time_ns < 2000) return format("%lld ns", time_ns);
|
||||
time_ns /= 1000;
|
||||
if(time_ns < 2000) return format("%lld us", time_ns);
|
||||
time_ns /= 1000;
|
||||
if(time_ns < 2000) return format("%lld ms", time_ns);
|
||||
time_ns /= 1000;
|
||||
return format("%lld s", time_ns);
|
||||
if(time_ns < 2000) return format("%lld ns", time_ns);
|
||||
time_ns /= 1000;
|
||||
if(time_ns < 2000) return format("%lld us", time_ns);
|
||||
time_ns /= 1000;
|
||||
if(time_ns < 2000) return format("%lld ms", time_ns);
|
||||
time_ns /= 1000;
|
||||
return format("%lld s", time_ns);
|
||||
}
|
||||
|
||||
std::string Timer::format_time() const
|
||||
{
|
||||
return Timer::format_time(this->time_ns);
|
||||
return Timer::format_time(this->time_ns);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue