2 Dimensional Ising Model
Simulate the change in energy and magnetization in a ferro magnet
Loading...
Searching...
No Matches
utils.hpp File Reference

Function prototypes and macros that are useful. More...

#include <armadillo>
#include <iomanip>
#include <sstream>
#include <string>
#include <sys/stat.h>
#include <vector>

Go to the source code of this file.

Macros

#define DEBUG(msg)
 Writes a debug message.
 
#define __METHOD_NAME__   details::methodName(__PRETTY_FUNCTION__)
 Get the name of the current method/function without the return type.
 

Functions

std::string details::methodName (const std::string &pretty_function)
 Takes in the PRETTY_FUNCTION string and removes the return type.
 
std::string utils::scientific_format (double d, int width=20, int prec=10)
 Turns a double into a string written in scientific format.
 
std::string utils::scientific_format (const std::vector< double > &v, int width=20, int prec=10)
 Turns a vector of doubles into a string written in scientific format.
 
bool utils::mkpath (std::string path, int mode=0777)
 Make path given.
 
std::string utils::dirname (const std::string &path)
 Get the directory name of the path.
 
std::string utils::concatpath (const std::string &left, const std::string &right)
 Take 2 strings and concatenate them and make sure there is a directory separator (/) between them.
 

Detailed Description

Function prototypes and macros that are useful.

Author
Cory Alexander Balaton (coryab)
Janita Ovidie Sandtrøen Willumsen (janitaws)
Version
1.0

These utility function are mainly for convenience and aren't directly related to the project. Anything that is in the details namespace should not be used directly, or else it might cause undefined behavior if not used correctly.

Bug:
No known bugs

Definition in file utils.hpp.

Macro Definition Documentation

◆ __METHOD_NAME__

#define __METHOD_NAME__   details::methodName(__PRETTY_FUNCTION__)

Get the name of the current method/function without the return type.

Definition at line 45 of file utils.hpp.

◆ DEBUG

#define DEBUG (   msg)

Writes a debug message.

This macro writes a debug message that includes the filename, line number, and a custom message. The function is wrapped in an ifdef that checks if DBG is defined, so one can choose to display the debug messages by adding the -DDBG flag when compiling.

Definition at line 39 of file utils.hpp.

Function Documentation

◆ concatpath()

std::string utils::concatpath ( const std::string &  left,
const std::string &  right 
)

Take 2 strings and concatenate them and make sure there is a directory separator (/) between them.

This function doesn't care whether or not the values given as parameters are valid path strings. It is the responsibility of the user to make sure that the values given are valid path strings. The function only guarantees that the output string is a valid path string.

Parameters
leftThe left hand side of the result string
rightThe right hand side of the result string
Returns
string

Definition at line 63 of file utils.cpp.

◆ dirname()

std::string utils::dirname ( const std::string &  path)

Get the directory name of the path.

Parameters
pathThe path to use.
Returns
string

Definition at line 58 of file utils.cpp.

◆ methodName()

std::string details::methodName ( const std::string &  pretty_function)
inline

Takes in the PRETTY_FUNCTION string and removes the return type.

This function should only be used for the METHOD_NAME macro, since it takes the output from PRETTY_FUNCTION and strips the return type.

Parameters
pretty_functionThe string from PRETTY_FUNCTION
Returns
std::string

Definition at line 58 of file utils.hpp.

◆ mkpath()

bool utils::mkpath ( std::string  path,
int  mode = 0777 
)

Make path given.

This tries to be the equivalent to "mkdir -p" and creates a new directory whenever it needs to.

Parameters
pathThe path to be created
modeThe mode/permissions for all the new directories
Returns
bool Success/Fail

Definition at line 32 of file utils.cpp.

◆ scientific_format() [1/2]

std::string utils::scientific_format ( const std::vector< double > &  v,
int  width = 20,
int  prec = 10 
)

Turns a vector of doubles into a string written in scientific format.

The code is stolen from https://github.com/anderkve/FYS3150.

Parameters
vThe vector to stringify
widthThe reserved width of the string
precThe precision of the stringified number
Returns
std::string

Definition at line 23 of file utils.cpp.

◆ scientific_format() [2/2]

std::string utils::scientific_format ( double  d,
int  width = 20,
int  prec = 10 
)

Turns a double into a string written in scientific format.

The code is stolen from https://github.com/anderkve/FYS3150.

Parameters
dThe number to stringify
widthThe reserved width of the string
precThe precision of the stringified number
Returns
std::string

Definition at line 16 of file utils.cpp.