Commit b94850be authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Moved radixbug and radixcommand to radixcore package.

parent 84df7a36
#ifndef RADIX_RADIXCORE_BUG_HH_
#define RADIX_RADIXCORE_BUG_HH_
// default to c++ compiler
/*
* @file: bug.hh
* @author: Jordan P. Lefebvre, lefebvrejp@ornl.gov
* @brief Debug functionality for including debug output \
* in your source without a performance penalty.
* This is an include ONLY package. There is no library to link against
* Each function is preprocessed to be included or not.
* If DEBUG_OUTPUT is defined the MACROs are included.
*
* Available MACROs:
* radix(arg) - std::cerr << arg - Pushes content to stderr
* radix_line(arg) - std::cerr << arg << std::endl - Push newline terminated
* content to stderr
* radix_eol() - std::cerr << std::endl - Push newline to stderr
* radix(arg) - std::cerr << arg - Pushes content to stderr
* radix_warning(arg) - std::cerr << arg << std::endl - Push newline terminated
* content to stderr
* radix_tagged_line(arg) - Same as radixLine, prefixed with
* FILE and LINE
* radix_tagged(arg) - Same as radix, prefixed with FILE and LINE
* radix_tagged_warning(arg) - Same as radixWarning, prefixed with FILE and Line
* radix_block(block) - block - Simply places code block in preprocessor
*/
#include <cstdio>
#include <iostream>
#if RADIX_DEBUG_OUTPUT
#ifndef radix_stream
#define radix_stream std::cerr
#endif
#define radix_define_stream
#define radix_line(arg) radix_stream << arg << std::endl
#define radix_eol() radix_stream << std::endl
#define radix_flush_line(arg) \
radix_stream << arg << std::endl; \
fflush(stderr)
#define radix(arg) radix_stream << arg
#define radix_warning(arg) radix_stream << arg << std::endl
#define radix_tagged_warning(arg) \
radix_stream << __FILE__ << ":" << __LINE__ << ": " << arg << std::endl
#define radix_flush_warning(arg) \
radix_stream << arg << std::endl; \
fflush(stderr)
#define radix_tagged_line(arg) \
radix_stream << __FILE__ << ":" << __LINE__ << ": " << arg << std::endl
#define radix_flush_tagged_line(arg) \
radix_stream << __FILE__ << ":" << __LINE__ << ": " << arg << std::endl; \
fflush(stderr)
#define radix_tagged(arg) \
radix_stream << __FILE__ << ":" << __LINE__ << ": " << arg
#define radix_flush_tagged_warning(arg) \
radix_stream << __FILE__ << ":" << __LINE__ << ": " << arg << std::endl; \
fflush(stderr)
#define radix_tagged_block(block) \
radix_stream << __FILE__ << ":" << __LINE__ << ":" << std::endl; \
block
#define radix_block(block) block
#else
#define radix(arg)
#define radix_line(arg)
#define radix_eol()
#define radix_flush_line(arg)
#define radix_warning(arg)
#define radix_flush_warning(arg)
#define radix_tagged_line(arg)
#define radix_flush_tagged_line(arg)
#define radix_tagged(arg)
#define radix_tagged_warning(arg)
#define radix_flush_tagged_warning(arg)
#define radix_tagged_block(block)
#define radix_block(block)
#endif /* RADIX_DEBUG_OUTPUT */
#include <sstream>
#include <stdexcept>
#ifndef RADIX_DBC
#define RADIX_DBC 0
#endif
//
// Defined all levels of RADIX_DBC
// RADIX_DBC = 1 enables Require
// RADIX_DBC = 2 enables Check
// RADIX_DBC = 4 enables Remember & Ensure
// RADIX_DBC = 7 enables all
// Insist is always enabled
#if RADIX_DBC & 1
#define radix_require(c) \
do \
{ \
if (!(c)) \
{ \
std::ostringstream stream; \
stream << __FILE__ << ":" << __LINE__ << " radix_require(" << #c \
<< ") failed." << std::endl; \
throw std::runtime_error(stream.str()); \
} \
} while (false)
#else
#define radix_require(c)
#endif
#if RADIX_DBC & 2
#define radix_check(c) \
do \
{ \
if (!(c)) \
{ \
std::ostringstream stream; \
stream << __FILE__ << ":" << __LINE__ << " radix_check(" << #c \
<< ") failed." << std::endl; \
throw std::runtime_error(stream.str()); \
} \
} while (false)
#else
#define radix_check(c)
#endif
#if RADIX_DBC & 4
#define radix_ensure(c) \
do \
{ \
if (!(c)) \
{ \
std::ostringstream stream; \
stream << __FILE__ << ":" << __LINE__ << " radix_ensure(" << #c \
<< ") failed." << std::endl; \
throw std::runtime_error(stream.str()); \
} \
} while (false)
#define radix_remember(c) c
#else
#define radix_ensure(c)
#define radix_remember(c)
#endif
#define radix_insist(c, msg) \
do \
{ \
if (!(c)) \
{ \
std::ostringstream stream; \
stream << __FILE__ << ":" << __LINE__ << "radix_insist(" << #c \
<< ") failed with this message:" << std::endl \
<< msg << std::endl; \
throw std::runtime_error(stream.str()); \
} \
} while (false)
#define radix_not_implemented(msg) \
do \
{ \
std::ostringstream stream; \
stream << __FILE__ << ":" << __LINE__ << " : " << msg \
<< " is not implemented. " << std::endl; \
throw std::runtime_error(stream.str()); \
} while (false)
#define radix_not_reachable() \
do \
{ \
std::ostringstream stream; \
stream << "Execution encountered unreachable code point at " << __FILE__ \
<< ":" << __LINE__ << std::endl; \
throw std::runtime_error(stream.str()); \
} while (false)
/// set default timing to off
#ifndef RADIX_TIMING
#define RADIX_TIMING 0
#endif
#include <chrono>
#include <ctime>
namespace radix
{
/**
* @brief Timer implementation for recording duration between start and stop
* points.
*
*/
class Timer
{
private:
bool mRunning;
std::chrono::steady_clock::time_point mStart;
std::chrono::steady_clock::time_point mEnd;
std::chrono::nanoseconds mDuration;
size_t mIntervals;
public:
Timer()
: mRunning(false)
, mDuration(0)
, mIntervals(0)
{
}
/**
* @brief Mark the start time for this timer
*
*/
void start()
{
radix_check(!mRunning);
mRunning = true;
mIntervals++;
mStart = std::chrono::steady_clock::now();
}
/**
* @brief Mark the stop time and record duration of time.
*
*/
void stop()
{
radix_check(mRunning);
mEnd = std::chrono::steady_clock::now();
mRunning = false;
mDuration +=
std::chrono::duration_cast<std::chrono::nanoseconds>(mEnd - mStart);
}
/**
* @brief Returns duration this timer ran in nanoseconds
*
* @return std::chrono::nanoseconds::rep
*/
std::chrono::nanoseconds::rep duration() const
{
radix_check(!mRunning);
return mDuration.count();
}
/**
* @brief Returns the number times this timer was started and subsequently
* stopped.
*
* @return size_t
*/
size_t intervals() const { return mIntervals; }
/**
* @brief Returns the duration this timer ran in wall clock seconds.
* Does not account for intervals
* @return double durantion in seconds
*/
double wall_clock()
{
radix_require(!mRunning);
auto nanoseconds =
std::chrono::duration_cast<std::chrono::nanoseconds>(mEnd - mStart);
// 1e9 nanoseconds in a second
return nanoseconds.count() / 1e9;
}
/**
* @brief Returns the duration this timer ran accounting for start/stop
* intervals
*
* @return double duration in seconds
*/
double sum_wall_clock()
{
// 1e9 nanoseconds in a second
return mDuration.count() / 1e9;
}
/**
* @brief Returns whether this timer is currently counting.
*
* @return true The timer is running.
* @return false The timer is not running.
*/
bool running() const { return mRunning; };
};
// class Timer
} // namespace radix
#if RADIX_TIMING > 0
#define radix_timer(name) radix::Timer name
#define radix_timer_start(name) name.start()
#define radix_timer_stop(name) name.stop()
#define radix_timer_block(content) content
#else
#define radix_timer(name)
#define radix_timer_start(name)
#define radix_timer_stop(name)
#define radix_timer_block(content)
#endif
#if RADIX_TIMING > 1
#define radix_timer_2(name) radix::Timer name
#define radix_timer_start_2(name) name.start()
#define radix_timer_stop_2(name) name.stop()
#define radix_timer_block_2(content) content
#else
#define radix_timer_2(name)
#define radix_timer_start_2(name)
#define radix_timer_stop_2(name)
#define radix_timer_block_2(content)
#endif
#if RADIX_TIMING > 2
#define radix_timer_3(name) radix::Timer name
#define radix_timer_start_3(name) name.start()
#define radix_timer_stop_3(name) name.stop()
#define radix_timer_block_3(content) content
#else
#define radix_timer_3(name)
#define radix_timer_start_3(name)
#define radix_timer_stop_3(name)
#define radix_timer_block_3(content)
#endif
#endif /* RADIX_RADIXCORE_BUG_HH_*/
#ifndef RADIX_RADIXBUG_BUG_HH_
#define RADIX_RADIXBUG_BUG_HH_
#if defined(__GNUC__)
#warning \
"#include \'radixbug/bug.hh\' is deprecated. Switch to #include \"radixcore/bug.hh\""
#elif defined(_MSC_VER)
#pragma message( \
"#include \'radixbug/bug.hh\' is deprecated. Switch to #include \"radixcore/bug.hh\"")
#endif
#include "../radixcore/bug.hh"
#endif /* RADIX_RADIXBUG_BUG_HH_*/
TRIBITS_SUBPACKAGE(command)
SET(SOURCE
commandline.cc
)
SET(HEADERS
commandline.hh
commandline.i.hh
)
#
# Add library
TRIBITS_ADD_LIBRARY(radixcommand
SOURCES ${SOURCE}
NOINSTALLHEADERS ${HEADERS}
)
#
......
#ifndef RADIX_RADIXCOMMAND_COMMANDLINE_HH_
#define RADIX_RADIXCOMMAND_COMMANDLINE_HH_
#include <ostream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "radixcore/visibility.hh"
namespace radix
{
class RADIX_PUBLIC CommandLine
{
public:
CommandLine(int argc, char **argv);
/**
* @brief addOption Adds official option to this commandline
* @param name key
* @param description description of option
* @param required whether it is required or not
*/
void addOption(const std::string &name, const std::string &description,
bool required = false);
// dumps all options to \code out
void printParsedLine(std::ostream &out) const;
// dumps the optional and required options to \code out
void help(std::ostream &out) const;
// does an option exist in this commandline
bool exists(const std::string &name) const;
// does an option exist in this commandline
bool existsNotEmpty(const std::string &name) const;
const std::vector<std::string> &arguments() const;
void setArguments(const std::vector<std::string> &args);
std::string executable() const;
void setExecutable(const std::string &executable);
void declareArgument(const std::string &id,
const std::string &description = std::string());
/**
* @brief get retrieve option value as type \code return_type
* @param name key of command line option
* @return \code return_type
*/
template <typename return_type>
return_type get(const std::string &name) const;
/**
* @brief get retrieve option value as type \code return_type
* @param name key of command line option
* @param defaultValue default return value in the event name was not provided
* @return \code return_type
*/
template <typename return_type>
return_type get(const std::string &name, return_type defaultValue) const;
/**
* @brief arg retrieve argument as type \code return_type
* @param i int index of argument [0,N)
* @return \code return_type
*/
template <typename return_type>
return_type arg(size_t i) const;
/**
* @brief validate Checks for existince of required options
* @param errors list of errors
* @return success/failure
*/
bool validate(std::vector<std::string> &errors) const;
/**
* @brief validate Checks for existince of required options
* @param std::ostream& stream for errors
* @return success/failure
*/
bool validate(std::ostream &out) const;
const std::string &description() const;
void setDescription(const std::string &description);
private:
int mArgc;
char **mArgv;
std::string mDescription;
std::string mExecutable;
std::vector<std::string> mArgs;
std::vector<std::pair<std::string, std::string>> mDeclArgs;
std::unordered_map<std::string, std::tuple<std::string, std::string, bool>>
mData;
}; // class CommandLine
//
// Specialized get/arg method declarations
template <>
std::string RADIX_PUBLIC CommandLine::get(const std::string &name) const;
template <>
std::string RADIX_PUBLIC CommandLine::get(const std::string &name,
std::string defaultValue) const;
template <>
std::string RADIX_PUBLIC CommandLine::arg(size_t i) const;
} // namespace radix
#include "radixcommand/commandline.i.hh"
#if defined(__GNUC__)
#warning \
"#include \'radixcommand/commandline.hh\' is deprecated. Switch to #include \"radixcore/commandline.hh\""
#elif defined(_MSC_VER)
#pragma message( \
"#include \'radixcommand/commandline.hh\' is deprecated. Switch to #include \"radixcore/commandline.hh\"")
#endif
#include "../radixcore/commandline.hh"
#endif /** RADIX_RADIXCOMMAND_COMMANDLINE_HH_ */
......@@ -3,6 +3,8 @@ TRIBITS_SUBPACKAGE(core)
TRIBITS_CONFIGURE_FILE(visibility.hh)
SET(HEADERS
commandline.hh
commandline.i.hh
fuzzy.hh
json.hh
system.hh
......@@ -11,6 +13,7 @@ stringfunctions.hh
value.hh
)
SET(SOURCES
commandline.cc
fuzzy.cc
system.cc
stringfunctions.cc
......
#ifndef RADIX_RADIXCORE_BUG_HH_
#define RADIX_RADIXCORE_BUG_HH_
// default to c++ compiler
/*
* @file: bug.hh
* @author: Jordan P. Lefebvre, lefebvrejp@ornl.gov
* @brief Debug functionality for including debug output \
* in your source without a performance penalty.
* This is an include ONLY package. There is no library to link against
* Each function is preprocessed to be included or not.
* If DEBUG_OUTPUT is defined the MACROs are included.
*
* Available MACROs:
* radix(arg) - std::cerr << arg - Pushes content to stderr
* radix_line(arg) - std::cerr << arg << std::endl - Push newline terminated
* content to stderr
* radix_eol() - std::cerr << std::endl - Push newline to stderr
* radix(arg) - std::cerr << arg - Pushes content to stderr
* radix_warning(arg) - std::cerr << arg << std::endl - Push newline terminated
* content to stderr
* radix_tagged_line(arg) - Same as radixLine, prefixed with
* FILE and LINE
* radix_tagged(arg) - Same as radix, prefixed with FILE and LINE
* radix_tagged_warning(arg) - Same as radixWarning, prefixed with FILE and Line
* radix_block(block) - block - Simply places code block in preprocessor
*/
#include <cstdio>
#include <iostream>
#if RADIX_DEBUG_OUTPUT
#ifndef radix_stream
#define radix_stream std::cerr
#endif
#define radix_define_stream
#define radix_line(arg) radix_stream << arg << std::endl
#define radix_eol() radix_stream << std::endl
#define radix_flush_line(arg) \
radix_stream << arg << std::endl; \
fflush(stderr)
#define radix(arg) radix_stream << arg
#define radix_warning(arg) radix_stream << arg << std::endl
#define radix_tagged_warning(arg) \
radix_stream << __FILE__ << ":" << __LINE__ << ": " << arg << std::endl
#define radix_flush_warning(arg) \
radix_stream << arg << std::endl; \
fflush(stderr)
#define radix_tagged_line(arg) \
radix_stream << __FILE__ << ":" << __LINE__ << ": " << arg << std::endl
#define radix_flush_tagged_line(arg) \
radix_stream << __FILE__ << ":" << __LINE__ << ": " << arg << std::endl; \
fflush(stderr)
#define radix_tagged(arg) \
radix_stream << __FILE__ << ":" << __LINE__ << ": " << arg
#define radix_flush_tagged_warning(arg) \
radix_stream << __FILE__ << ":" << __LINE__ << ": " << arg << std::endl; \
fflush(stderr)
#define radix_tagged_block(block) \
radix_stream << __FILE__ << ":" << __LINE__ << ":" << std::endl; \
block
#define radix_block(block) block
#else
#define radix(arg)
#define radix_line(arg)
#define radix_eol()
#define radix_flush_line(arg)
#define radix_warning(arg)
#define radix_flush_warning(arg)
#define radix_tagged_line(arg)
#define radix_flush_tagged_line(arg)
#define radix_tagged(arg)
#define radix_tagged_warning(arg)
#define radix_flush_tagged_warning(arg)
#define radix_tagged_block(block)
#define radix_block(block)
#endif /* RADIX_DEBUG_OUTPUT */
#include <sstream>
#include <stdexcept>
#ifndef RADIX_DBC
#define RADIX_DBC 0
#endif
//
// Defined all levels of RADIX_DBC
// RADIX_DBC = 1 enables Require
// RADIX_DBC = 2 enables Check
// RADIX_DBC = 4 enables Remember & Ensure
// RADIX_DBC = 7 enables all
// Insist is always enabled
#if RADIX_DBC & 1
#define radix_require(c) \
do \
{ \
if (!(c)) \
{ \
std::ostringstream stream; \
stream << __FILE__ << ":" << __LINE__ << " radix_require(" << #c \
<< ") failed." << std::endl; \
throw std::runtime_error(stream.str()); \
} \
} while (false)
#else
#define radix_require(c)
#endif
#if RADIX_DBC & 2
#define radix_check(c) \
do \
{ \
if (!(c)) \
{ \
std::ostringstream stream; \
stream << __FILE__ << ":" << __LINE__ << " radix_check(" << #c \
<< ") failed." << std::endl; \
throw std::runtime_error(stream.str()); \
} \
} while (false)
#else
#define radix_check(c)
#endif
#if RADIX_DBC & 4
#define radix_ensure(c) \
do \
{ \
if (!(c)) \
{ \
std::ostringstream stream; \
stream << __FILE__ << ":" << __LINE__ << " radix_ensure(" << #c \
<< ") failed." << std::endl; \
throw std::runtime_error(stream.str()); \
} \
} while (false)
#define radix_remember(c) c
#else
#define radix_ensure(c)
#define radix_remember(c)
#endif
#define radix_insist(c, msg) \
do \
{ \
if (!(c)) \
{ \
std::ostringstream stream; \
stream << __FILE__ << ":" << __LINE__ << "radix_insist(" << #c \
<< ") failed with this message:" << std::endl \
<< msg << std::endl; \
throw std::runtime_error(stream.str()); \
} \
} while (false)
#define radix_not_implemented(msg) \
do \
{ \
std::ostringstream stream; \
stream << __FILE__ << ":" << __LINE__ << " : " << msg \
<< " is not implemented. " << std::endl; \
throw std::runtime_error(stream.str()); \