Note: Goby version 1 (shown here) is now considered obsolete. Please use version 2 for new projects, and consider upgrading old projects.

Goby Underwater Autonomy Project  Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
Namespaces | Classes | Variables
goby::util Namespace Reference

Utility objects for performing functions such as logging, non-acoustic communication (ethernet / serial), time, scientific, string manipulation, etc. More...

Namespaces

namespace  tcolor
 

Contains functions for adding color to Terminal window streams.


Classes

class  LineBasedInterface
 basic interface class for all the derived serial (and networking mimics) line-based nodes (serial, tcp, udp, etc.) More...
class  SerialClient
 provides a basic client for line by line text based communications over a 8N1 tty (such as an RS-232 serial link) without flow control More...
class  TCPClient
 provides a basic TCP client for line by line text based communications to a remote TCP server More...
class  TCPServer
 provides a basic TCP server for line by line text based communications to a one or more remote TCP clients More...
class  FlexNCurses
 Enables the Verbosity == gui mode of the Goby logger and displays an NCurses gui for the logger content. More...
class  FlexOstream
 Forms the basis of the Goby logger: std::ostream derived class for holding the FlexOStreamBuf. More...
struct  Logger
 Holds static objects of the Goby Logger. More...
class  FlexOStreamBuf
 Class derived from std::stringbuf that allows us to insert things before the stream and control output. This is the string buffer used by goby::util::FlexOstream for the Goby Logger (glogger) More...
struct  Colors
 Represents the eight available terminal colors (and bold variants) More...
class  TermColor
 Converts between string, escape code, and enumeration representations of the terminal colors. More...

Functions

Binary encoding
bool char_array2hex_string (const unsigned char *c, std::string &s, const unsigned int n)
 converts a char (byte) array into a hex string
bool hex_string2char_array (unsigned char *c, const std::string &s, const unsigned int n)
 turns a string of hex chars ABCDEF into a character array reading each byte 0xAB,0xCD, 0xEF, etc.
std::string long2binary_string (unsigned long l, unsigned short bits)
 return a string represented the binary value of `l` for `bits` number of bits which reads MSB -> LSB
std::string binary_string2hex_string (const std::string &bs)
 converts a binary string ("1000101010101010") into a hex string ("8AAA")
std::string dyn_bitset2hex_string (const boost::dynamic_bitset< unsigned char > &bits, unsigned trim_to_bytes_size=0)
 converts a boost::dynamic_bitset (similar to std::bitset but without compile time size requirements) into a hex string
std::string hex_string2binary_string (const std::string &bs)
 converts a hex string ("8AAA") into a binary string ("1000101010101010")
boost::dynamic_bitset
< unsigned char > 
hex_string2dyn_bitset (const std::string &hs, unsigned bits_size=0)
 converts a hex string ("8AAA") into a dynamic_bitset
template<typename T >
bool hex_string2number (const std::string &s, T &t)
 attempts to convert a hex string into a numerical representation (of type T)
template<typename T >
bool number2hex_string (std::string &s, const T &t, unsigned int width=2)
 converts a decimal number of type T into a hex string
template<typename T >
std::string number2hex_string (const T &t, unsigned int width=2)
 converts a decimal number of type T into a hex string assuming success
Logger
FlexOstreamglogger (logger_lock::LockAction lock_action=logger_lock::none)
 Access the Goby logger through this function.
std::ostream & operator<< (FlexOstream &out, char c)
std::ostream & operator<< (FlexOstream &out, signed char c)
std::ostream & operator<< (FlexOstream &out, unsigned char c)
std::ostream & operator<< (FlexOstream &out, const char *s)
std::ostream & operator<< (FlexOstream &out, const signed char *s)
std::ostream & operator<< (FlexOstream &out, const unsigned char *s)
Science
double unbiased_round (double r, double dec)
double mackenzie_soundspeed (double T, double S, double D)
String
template<typename To , typename From >
To as (From from, typename boost::enable_if< boost::is_fundamental< To > >::type *dummy=0)
 non-throwing lexical cast (e.g. assert(as<double>("3.2") == 3.2)). For fundamental types (double, int, etc.)
template<typename To , typename From >
To as (From from, typename boost::disable_if< boost::is_fundamental< To > >::type *dummy=0)
 non-throwing lexical cast (e.g. assert(as<double>("3.2") == 3.2)) for non-fundamental types (MyClass(), etc.)
template<>
bool as< bool, std::string > (std::string from, void *dummy)
 specialization of as() for string -> bool
template<>
std::string as< std::string, bool > (bool from, void *dummy)
 specialization of as() for bool -> string
void stripblanks (std::string &s)
 remove all blanks from string s
bool val_from_string (std::string &out, const std::string &str, const std::string &key)
template<typename T >
bool val_from_string (T &out, const std::string &str, const std::string &key)
bool val_from_string (bool &out, const std::string &str, const std::string &key)
 specialization of val_from_string for boolean `out`
Time
int64_t microtime ()
boost::posix_time::ptime goby_time ()
 Always use for current time within the Goby project.
std::string goby_time_as_string (const boost::posix_time::ptime &t=goby_time())
 Simple string representation of goby_time()
std::string goby_file_timestamp ()
 ISO string representation of goby_time()
double ptime2unix_double (boost::posix_time::ptime given_time)
 convert from boost date_time ptime to the number of seconds (including fractional) since 1/1/1970 0:00 UTC ("UNIX Time")
boost::posix_time::ptime unix_double2ptime (double given_time)
 convert to boost date_time ptime from the number of seconds (including fractional) since 1/1/1970 0:00 UTC ("UNIX Time"): good to the microsecond
boost::posix_time::ptime time_t2ptime (std::time_t t)
 convert to ptime from time_t from time.h (whole seconds since UNIX)
std::time_t ptime2time_t (boost::posix_time::ptime t)
 convert from ptime to time_t from time.h (whole seconds since UNIX)
double time_duration2double (boost::posix_time::time_duration time_of_day)
 time duration to double number of seconds: good to the microsecond

Variables

const std::string esc_red = "\33[31m"
const std::string esc_lt_red = "\33[91m"
const std::string esc_green = "\33[32m"
const std::string esc_lt_green = "\33[92m"
const std::string esc_yellow = "\33[33m"
const std::string esc_lt_yellow = "\33[93m"
const std::string esc_blue = "\33[34m"
const std::string esc_lt_blue = "\33[94m"
const std::string esc_magenta = "\33[35m"
const std::string esc_lt_magenta = "\33[95m"
const std::string esc_cyan = "\33[36m"
const std::string esc_lt_cyan = "\33[96m"
const std::string esc_white = "\33[37m"
const std::string esc_lt_white = "\33[97m"
const std::string esc_nocolor = "\33[0m"

Detailed Description

Utility objects for performing functions such as logging, non-acoustic communication (ethernet / serial), time, scientific, string manipulation, etc.


Function Documentation

template<typename To , typename From >
To goby::util::as ( From  from,
typename boost::enable_if< boost::is_fundamental< To > >::type *  dummy = 0 
)

non-throwing lexical cast (e.g. assert(as<double>("3.2") == 3.2)). For fundamental types (double, int, etc.)

Parameters:
fromvalue to cast from
Returns:
to value to cast to
Exceptions:
none
Examples:
acomms/chat/chat.cpp.

Definition at line 47 of file string.h.

template<typename To , typename From >
To goby::util::as ( From  from,
typename boost::disable_if< boost::is_fundamental< To > >::type *  dummy = 0 
)

non-throwing lexical cast (e.g. assert(as<double>("3.2") == 3.2)) for non-fundamental types (MyClass(), etc.)

Parameters:
fromvalue to cast from
Returns:
to value to cast to
Exceptions:
none

Definition at line 64 of file string.h.

bool goby::util::char_array2hex_string ( const unsigned char *  c,
std::string &  s,
const unsigned int  n 
) [inline]

converts a char (byte) array into a hex string

Parameters:
cpointer to array of char
sreference to string to put char into as hex
nlength of c the first two hex chars in s are the 0 index in c

Definition at line 47 of file binary.h.

goby::util::FlexOstream & goby::util::glogger ( logger_lock::LockAction  lock_action = logger_lock::none)

Access the Goby logger through this function.

For normal (non thread safe use), do not pass any parameters: glogger() << "some text" << std::endl;

To group messages, pass the group(group_name) manipulator, where group_name is a previously defined group (by call to glogger().add_group(Group)). For example: glogger() << group("incoming") << "received message foo" << std::endl;

For thread safe use, use glogger(lock) and then insert the "unlock" manipulator when relinquishing the lock. The "unlock" manipulator MUST be inserted before the next call to glogger(lock). Nothing must throw exceptions between glogger(lock) and unlock. For example: glogger(lock) << "my thread is the best" << std::endl << unlock;

Parameters:
lock_actionlogger_lock::lock to lock access to the logger (for thread safety) or logger_lock::none for no mutex action (typical)
Returns:
reference to Goby logger (std::ostream derived class FlexOstream)

Definition at line 24 of file flex_ostream.cpp.

std::string goby::util::hex_string2binary_string ( const std::string &  bs) [inline]

converts a hex string ("8AAA") into a binary string ("1000101010101010")

only works on whole byte string (even number of nibbles)

Definition at line 118 of file binary.h.

template<typename T >
bool goby::util::hex_string2number ( const std::string &  s,
T &  t 
)

attempts to convert a hex string into a numerical representation (of type T)

Returns:
true if conversion succeeds, false otherwise

Definition at line 149 of file binary.h.

double goby::util::mackenzie_soundspeed ( double  T,
double  S,
double  D 
) [inline]

K.V. Mackenzie, Nine-term equation for the sound speed in the oceans (1981) J. Acoust. Soc. Am. 70(3), pp 807-812 http://scitation.aip.org/getabs/servlet/GetabsServlet?prog=normal&id=JASMAN000070000003000807000001&idtype=cvips&gifs=yes

Parameters:
Ttemperature in degrees Celcius (see paper for applicable ranges)
Ssalinity (unitless, calculated using Practical Salinity Scale) (see paper for applicable ranges)
Ddepth in meters (see paper for applicable ranges)
Returns:
speed of sound in meters per second

Definition at line 60 of file sci.h.

template<typename T >
bool goby::util::number2hex_string ( std::string &  s,
const T &  t,
unsigned int  width = 2 
)

converts a decimal number of type T into a hex string

Parameters:
sstring reference to store result in
tdecimal number to convert
widthdesired width (in characters) of return string. Width should be twice the number of bytes
Returns:
true if successful, false otherwise

Definition at line 164 of file binary.h.

template<typename T >
std::string goby::util::number2hex_string ( const T &  t,
unsigned int  width = 2 
)

converts a decimal number of type T into a hex string assuming success

Parameters:
tdecimal number to convert
widthdesired width (in characters) of return string. Width should be twice the number of bytes
Returns:
hex string

Definition at line 177 of file binary.h.

double goby::util::unbiased_round ( double  r,
double  dec 
) [inline]

round 'r' to 'dec' number of decimal places we want no upward bias so round 5 up if odd next to it, down if even

Parameters:
rvalue to round
decnumber of places past the decimal to round (e.g. dec=1 rounds to tenths)
Returns:
r rounded

Definition at line 41 of file sci.h.

bool goby::util::val_from_string ( std::string &  out,
const std::string &  str,
const std::string &  key 
) [inline]

find `key` in `str` and if successful put it in out and return true deal with these basic forms: str = foo=1,bar=2,pig=3 str = foo=1,bar={2,3,4,5},pig=3

Parameters:
outstring to return value in
strhaystack to search
keyneedle to find
Returns:
true if key is in str, false otherwise

Definition at line 112 of file string.h.

template<typename T >
bool goby::util::val_from_string ( T &  out,
const std::string &  str,
const std::string &  key 
) [inline]

variation of val_from_string() for arbitrary return type

Returns:
false if `key` not in `str` OR if `out` is not of proper type T

Definition at line 174 of file string.h.

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends