Commit 52df59d3 authored by Martin Reinecke's avatar Martin Reinecke
Browse files

more stuff

parent c351cbf2
#include "error_handling.h"
#include "mr_util/error_handling.h"
using namespace std;
namespace mr {
namespace error_handling {
namespace detail {
bool abort_in_progress__ = false;
ostream &CodeLocation::print(ostream &os) const
......@@ -16,3 +22,5 @@ void killjob__()
// perhaps print stack trace?
exit(1);
}
}}}
......@@ -31,37 +31,21 @@
* \author Martin Reinecke
*/
#ifndef MRBASE_DATATYPES_H
#define MRBASE_DATATYPES_H
#ifndef MRUTIL_DATATYPES_H
#define MRUTIL_DATATYPES_H
#include <string>
#include <cstddef>
#include <cstdint>
#include "error_handling.h"
#include "mr_util/error_handling.h"
using int8 = std::int8_t;
using uint8 = std::uint8_t;
namespace mr {
using int16 = std::int16_t;
using uint16 = std::uint16_t;
using int32 = std::int32_t;
using uint32 = std::uint32_t;
using int64 = std::int64_t;
using uint64 = std::uint64_t;
using float32 = float;
using float64 = double;
/*! unsigned integer type which should be used for array sizes */
using tsize = std::size_t;
/*! signed integer type which should be used for relative array indices */
using tdiff = std::ptrdiff_t;
namespace datatypes {
/*! Returns a C string describing the data type \a T. */
template<typename T> inline const char *type2typename ()
{ myfail(T::UNSUPPORTED_DATA_TYPE); }
{ MR_fail(T::UNSUPPORTED_DATA_TYPE); }
template<> inline const char *type2typename<signed char> ()
{ return "signed char"; }
template<> inline const char *type2typename<unsigned char> ()
......@@ -114,7 +98,7 @@ enum NDT {
/*! Returns the \a NDT constant associated with \a T. */
template<typename T> inline NDT nativeType()
{ myfail(T::UNSUPPORTED_DATA_TYPE); }
{ MR_fail(T::UNSUPPORTED_DATA_TYPE); }
template<> inline NDT nativeType<char> () { return NAT_CHAR; }
template<> inline NDT nativeType<signed char> () { return NAT_SCHAR; }
template<> inline NDT nativeType<unsigned char> () { return NAT_UCHAR; }
......@@ -153,8 +137,10 @@ inline int ndt2size (NDT type)
case NAT_LONGDOUBLE: return sizeof(long double);
case NAT_BOOL : return sizeof(bool);
default:
myfail ("ndt2size: unsupported data type");
MR_fail ("ndt2size: unsupported data type");
}
}
}}
#endif
#ifndef MRBASE_ERROR_HANDLING_H
#define MRBASE_ERROR_HANDLING_H
/*
* This file is part of the MR utility library.
*
* This code is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this code; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* Copyright (C) 2019 Max-Planck-Society
Author: Martin Reinecke */
#ifndef MRUTIL_ERROR_HANDLING_H
#define MRUTIL_ERROR_HANDLING_H
#include <iostream>
#include <cstdlib>
namespace mr {
namespace error_handling {
namespace detail {
#if defined (__GNUC__)
#define LOC_ CodeLocation(__FILE__, __LINE__, __PRETTY_FUNCTION__)
#define MR_ERROR_HANDLING_LOC_ ::mr::error_handling::detail::CodeLocation(__FILE__, __LINE__, __PRETTY_FUNCTION__)
#else
#define LOC_ CodeLocation(__FILE__, __LINE__)
#define MRERROR_HANDLING_LOC_ ::mr::error_handling::detail::CodeLocation(__FILE__, __LINE__)
#endif
#define myfail(...) \
#define MR_fail(...) \
do { \
if (!abort_in_progress__) \
if (!::mr::error_handling::detail::abort_in_progress__) \
{ \
abort_in_progress__ = true; \
streamDump__(std::cerr, LOC_, "\n", ##__VA_ARGS__, "\n"); \
killjob__(); \
::mr::error_handling::detail::abort_in_progress__ = true; \
::mr::error_handling::detail::streamDump__(::std::cerr, MR_ERROR_HANDLING_LOC_, "\n", ##__VA_ARGS__, "\n"); \
::mr::error_handling::detail::killjob__(); \
} \
std::exit(1); \
::std::exit(1); \
} while(0)
#define myassert(cond,...) \
#define MR_assert(cond,...) \
do { \
if (cond); \
else { myfail("Assertion failure\n", ##__VA_ARGS__); } \
else { MR_fail("Assertion failure\n", ##__VA_ARGS__); } \
} while(0)
// to be replaced with std::source_location once available
......@@ -38,10 +65,10 @@ class CodeLocation
CodeLocation(const char *file_, int line_, const char *func_=nullptr)
: file(file_), func(func_), line(line_) {}
std::ostream &print(std::ostream &os) const;
::std::ostream &print(::std::ostream &os) const;
};
inline std::ostream &operator<<(std::ostream &os, const CodeLocation &loc)
inline ::std::ostream &operator<<(::std::ostream &os, const CodeLocation &loc)
{ return loc.print(os); }
extern bool abort_in_progress__;
......@@ -49,11 +76,11 @@ void killjob__();
#if 1
template<typename T>
inline void streamDump__(std::ostream &os, const T& value)
inline void streamDump__(::std::ostream &os, const T& value)
{ os << value; }
template<typename T, typename ... Args>
inline void streamDump__(std::ostream &os, const T& value,
inline void streamDump__(::std::ostream &os, const T& value,
const Args& ... args)
{
os << value;
......@@ -62,8 +89,10 @@ inline void streamDump__(std::ostream &os, const T& value,
#else
// hyper-elegant C++2017 version
template<typename ...Args>
inline void streamDump__(std::ostream &os, Args&&... args)
inline void streamDump__(::std::ostream &os, Args&&... args)
{ (os << ... << args); }
#endif
}}}
#endif
......@@ -29,12 +29,15 @@
* \author Martin Reinecke
*/
#ifndef MRBASE_STRING_UTILS_H
#define MRBASE_STRING_UTILS_H
#ifndef MRUTIL_STRING_UTILS_H
#define MRUTIL_STRING_UTILS_H
#include <vector>
#include <map>
#include "datatypes.h"
namespace mr {
namespace string_utils {
/*! \defgroup stringutilsgroup String handling helper functions */
/*! \{ */
......@@ -53,7 +56,7 @@ template<> std::string dataToString (const long double &x);
/*! Returns a string containing the text representation of \a x, padded
with leading zeroes to \a width characters. */
std::string intToString(int64 x, tsize width);
std::string intToString(std::int64_t x, std::size_t width);
/*! Reads a value of a given datatype from a string */
template<typename T> void stringToData (const std::string &x, T &value);
......@@ -120,4 +123,6 @@ void parse_words_from_file (const std::string &filename,
/*! \} */
}}
#endif
#ifndef MRBASE_SYSTEM_H
#define MRBASE_SYSTEM_H
#ifndef MRUTIL_SYSTEM_H
#define MRUTIL_SYSTEM_H
#include <string>
#include "datatypes.h"
#include <cstdlib>
tsize getProcessInfo(const std::string &quantity);
tsize getMemInfo(const std::string &quantity);
tsize usable_memory();
namespace mr {
namespace system {
std::size_t getProcessInfo(const std::string &quantity);
std::size_t getMemInfo(const std::string &quantity);
std::size_t usable_memory();
}}
#endif
#ifndef MRBASE_TIMERS_H
#define MRBASE_TIMERS_H
#ifndef MRUTIL_TIMERS_H
#define MRUTIL_TIMERS_H
#include <chrono>
#include <string>
#include <map>
#include "error_handling.h"
#include "string_utils.h"
#include "mr_util/error_handling.h"
#include "mr_util/string_utils.h"
namespace mr {
namespace timers {
class SimpleTimer
{
......@@ -65,7 +69,7 @@ class TimerHierarchy
auto it=curnode->child.find(name);
if (it==curnode->child.end())
{
myassert(name.find(':') == std::string::npos, "reserved character");
MR_assert(name.find(':') == std::string::npos, "reserved character");
it = curnode->child.insert(make_pair(name,tstack_node(curnode))).first;
}
curnode=&(it->second);
......@@ -83,7 +87,7 @@ class TimerHierarchy
{
adjust_time();
curnode = curnode->parent;
myassert(curnode!=nullptr, "tried to pop from empty timer stack");
MR_assert(curnode!=nullptr, "tried to pop from empty timer stack");
}
void poppush(const std::string &name)
{
......@@ -99,4 +103,6 @@ class TimerHierarchy
}
};
}}
#endif
......@@ -37,10 +37,18 @@
#include <string>
#include <cstring>
#include <cctype>
#include "string_utils.h"
#include "mr_util/string_utils.h"
#include "mr_util/error_handling.h"
#include "mr_util/datatypes.h"
using namespace std;
namespace mr {
namespace string_utils {
using namespace mr::datatypes;
string trim (const string &orig)
{
string::size_type p1=orig.find_first_not_of(" \t");
......@@ -90,13 +98,13 @@ template string dataToString (const unsigned long &x);
template string dataToString (const long long &x);
template string dataToString (const unsigned long long &x);
string intToString(int64 x, tsize width)
string intToString(int64_t x, size_t width)
{
ostringstream strstrm;
(x>=0) ? strstrm << setw(width) << setfill('0') << x
: strstrm << "-" << setw(width-1) << setfill('0') << -x;
string res = strstrm.str();
myassert(res.size()==width,"number too large");
MR_assert(res.size()==width,"number too large");
return trim(res);
}
......@@ -104,12 +112,12 @@ namespace {
void end_stringToData (const string &x, const char *tn, istringstream &strstrm)
{
myassert (strstrm,
MR_assert (strstrm,
"conversion error in stringToData<", tn, ">('", x, "')");
string rest;
strstrm >> rest;
// rest=trim(rest);
myassert (rest.length()==0,
MR_assert (rest.length()==0,
"conversion error in stringToData<", tn, ">('", x, "')");
}
......@@ -129,11 +137,11 @@ template<> void stringToData (const string &x, bool &value)
{
const char *fval[] = {"f","n","false",".false."};
const char *tval[] = {"t","y","true",".true."};
for (tsize i=0; i< sizeof(fval)/sizeof(fval[0]); ++i)
for (size_t i=0; i< sizeof(fval)/sizeof(fval[0]); ++i)
if (equal_nocase(x,fval[i])) { value=false; return; }
for (tsize i=0; i< sizeof(tval)/sizeof(tval[0]); ++i)
for (size_t i=0; i< sizeof(tval)/sizeof(tval[0]); ++i)
if (equal_nocase(x,tval[i])) { value=true; return; }
myfail("conversion error in stringToData<bool>(",x,")");
MR_fail("conversion error in stringToData<bool>(",x,")");
}
template void stringToData (const string &x, signed char &value);
......@@ -153,16 +161,16 @@ template void stringToData (const string &x, long double &value);
bool equal_nocase (const string &a, const string &b)
{
if (a.size()!=b.size()) return false;
for (tsize m=0; m<a.size(); ++m)
if (tolower(a[m])!=tolower(b[m])) return false;
for (size_t m=0; m<a.size(); ++m)
if (std::tolower(a[m])!=std::tolower(b[m])) return false;
return true;
}
string tolower(const string &input)
{
string result=input;
for (tsize m=0; m<result.size(); ++m)
result[m]=char(tolower(result[m]));
for (size_t m=0; m<result.size(); ++m)
result[m]=char(std::tolower(result[m]));
return result;
}
......@@ -171,7 +179,7 @@ void parse_file (const string &filename, map<string,string> &dict)
int lineno=0;
dict.clear();
ifstream inp(filename.c_str());
myassert (inp, "Could not open parameter file '", filename, "'.");
MR_assert (inp, "Could not open parameter file '", filename, "'.");
while (inp)
{
string line;
......@@ -221,14 +229,14 @@ void parse_cmdline_classic (int argc, const char **argv,
const vector<string> &leading_args, map<string,string> &dict)
{
dict.clear();
myassert(tsize(argc)>leading_args.size(),"not enough arguments");
for (tsize i=0; i<leading_args.size(); ++i)
MR_assert(size_t(argc)>leading_args.size(),"not enough arguments");
for (size_t i=0; i<leading_args.size(); ++i)
dict[leading_args[i]] = argv[i+1];
int curarg=leading_args.size()+1;
while (curarg<argc)
{
string param=argv[curarg];
myassert(isParam(param),"unrecognized command line format");
MR_assert(isParam(param),"unrecognized command line format");
if ((curarg==argc-1) || isParam(argv[curarg+1]))
{
dict[param.substr(1)]="true";
......@@ -250,8 +258,8 @@ void parse_cmdline_equalsign (int argc, const char **argv,
const vector<string> &leading_args, map<string,string> &dict)
{
dict.clear();
myassert(tsize(argc)>leading_args.size(),"not enough arguments");
for (tsize i=0; i<leading_args.size(); ++i)
MR_assert(size_t(argc)>leading_args.size(),"not enough arguments");
for (size_t i=0; i<leading_args.size(); ++i)
dict[leading_args[i]] = argv[i+1];
for (int i=leading_args.size()+1; i<argc; ++i)
{
......@@ -292,7 +300,7 @@ template<typename T> void split (istream &stream, vector<T> &list)
{
string word;
stream >> word;
myassert (stream||stream.eof(),
MR_assert (stream||stream.eof(),
"error while splitting stream into ", type2typename<T>(), " components");
if (stream) list.push_back(stringToData<T>(word));
}
......@@ -325,7 +333,7 @@ void parse_words_from_file (const string &filename, vector<string> &words)
{
words.clear();
ifstream inp(filename.c_str());
myassert (inp,"Could not open file '", filename, "'.");
MR_assert (inp,"Could not open file '", filename, "'.");
while (inp)
{
string word;
......@@ -334,3 +342,5 @@ void parse_words_from_file (const string &filename, vector<string> &words)
if (word!="") words.push_back(word);
}
}
}}
......@@ -3,10 +3,16 @@
#include <fstream>
#include <sstream>
#include "system.h"
#include "string_utils.h"
#include "mr_util/error_handling.h"
#include "mr_util/system.h"
#include "mr_util/string_utils.h"
using namespace std;
using namespace mr::string_utils;
namespace mr {
namespace system {
namespace {
......@@ -23,28 +29,30 @@ template<typename T> T find(const string &s, const string &pattern)
regex re(pattern);
sregex_iterator it(s.begin(), s.end(), re);
sregex_iterator it_end;
myassert (it!=it_end, "did not find pattern '", pattern, "'");
MR_assert (it!=it_end, "did not find pattern '", pattern, "'");
return stringToData<T>(it->str(1));
}
} // unnamed namespace
tsize getProcessInfo(const string &quantity)
size_t getProcessInfo(const string &quantity)
{
string text = fileToString("/proc/self/status");
return find<tsize>(text, quantity + R"(:\s+(\d+))");
return find<size_t>(text, quantity + R"(:\s+(\d+))");
}
tsize getMemInfo(const string &quantity)
size_t getMemInfo(const string &quantity)
{
string text = fileToString("/proc/meminfo");
return find<tsize>(text, quantity + R"(:\s+(\d+) kB)");
return find<size_t>(text, quantity + R"(:\s+(\d+) kB)");
}
tsize usable_memory()
size_t usable_memory()
{
string text = fileToString("/proc/meminfo");
tsize MemTotal = find<tsize>(text, R"(MemTotal:\s+(\d+) kB)");
tsize Committed = find<tsize>(text, R"(Committed_AS:\s+(\d+) kB)");
size_t MemTotal = find<size_t>(text, R"(MemTotal:\s+(\d+) kB)");
size_t Committed = find<size_t>(text, R"(Committed_AS:\s+(\d+) kB)");
return MemTotal-Committed;
}
}}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment