use inline function for unit conversion instead of macros

This commit is contained in:
Pawel "l0ner" Soltys 2015-01-18 20:02:46 +01:00
parent 7c883b924a
commit 2fb24571ef
6 changed files with 47 additions and 12 deletions

@ -26,7 +26,7 @@
#include "getsysctl.h"
#include "memory.h"
#include "../luts.h"
#include "../config.h"
#include "../conversions.h"
std::string mem_string( bool use_colors = false )
{
@ -63,7 +63,8 @@ std::string mem_string( bool use_colors = false )
oss << mem_lut[( 100 * used_mem ) / total_mem];
}
oss << MEGABYTES( used_mem ) << '/' << MEGABYTES( total_mem ) << "MB";
oss << convert_unit( used_mem, MEGABYTES ) << '/'
<< convert_unit( total_mem, MEGABYTES ) << "MB";
if( use_colors )
{

@ -29,7 +29,7 @@
#include "getsysctl.h"
#include "memory.h"
#include "../luts.h"
#include "../config.h"
#include "../conversions.h"
std::string mem_string( bool use_colors = false )
{
@ -65,7 +65,8 @@ std::string mem_string( bool use_colors = false )
oss << mem_lut[( 100 * used_mem ) / total_mem];
}
oss << MEGABYTES( used_mem ) << '/' << MEGABYTES( total_mem ) << "MB";
oss << convert_unit( used_mem, MEGABYTES ) << '/'
<< convert_unit( total_mem, MEGABYTES ) << "MB";
if( use_colors )
{

@ -22,7 +22,3 @@
#define tmux_mem_cpu_load_VERSION_PATCH @tmux-mem-cpu-load_VERSION_PATCH@
#define tmux_mem_cpu_load_VERSION "@tmux-mem-cpu-load_VERSION@"
// Memory Sizes
#define KILOBYTES(x) ((x)/1024)
#define MEGABYTES(x) (KILOBYTES((x))/1024)
#define GIGABYTES(x) (MEGABYTES((x))/1024)

35
conversions.h Normal file

@ -0,0 +1,35 @@
/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap
*
* Copyright 2012 Matthew McCormick
* Copyright 2015 Pawel 'l0ner' Soltys
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
enum
{
BYTES = 0,
KILOBYTES = 1,
MEGABYTES = 2,
GIGABYTES = 3
};
template <class T>
inline T convert_unit( T num, int to, int from = BYTES)
{
for(from; from < to; from++)
{
num /= 1024;
}
return num;
}

@ -22,7 +22,7 @@
#include "memory.h"
#include "../luts.h"
#include "../config.h"
#include "../conversions.h"
std::string mem_string( bool use_colors = false )
{
@ -92,7 +92,8 @@ std::string mem_string( bool use_colors = false )
// we want megabytes on output, but since the values already come as
// kilobytes we need to divide them by 1024 only once, thus we use
// KILOBYTES
oss << KILOBYTES(used_mem) << '/' << KILOBYTES(total_mem) << "MB";
oss << convert_unit(used_mem, MEGABYTES, KILOBYTES) << '/'
<< convert_unit(total_mem, MEGABYTES, KILOBYTES) << "MB";
if( use_colors )
{

@ -23,7 +23,7 @@
#include "memory.h"
#include "../luts.h"
#include "../config.h"
#include "../conversions.h"
std::string mem_string( bool use_colors )
{
@ -64,7 +64,8 @@ std::string mem_string( bool use_colors )
oss << mem_lut[( 100 * used_mem ) / total_mem];
}
oss << MEGABYTES(used_mem) << '/' << MEGABYTES(total_mem) << "MB";
oss << convert_unit( used_mem, MEGABYTES ) << '/'
<< convert_unit( total_mem, MEGABYTES ) << "MB";
if( use_colors )
{