26 #include <boost/foreach.hpp> 28 #include <boost/lexical_cast.hpp> 29 #include <boost/numeric/conversion/cast.hpp> 31 #include "goby/util/as.h" 32 #include "goby/util/sci.h" 34 #include "goby/acomms/dccl.h" 35 #include "message_val.h" 39 void goby::transitional::DCCLMessageVal::init()
45 precision_ = MAX_DBL_PRECISION;
104 throw(goby::acomms::DCCLException(
105 "vector cast to DCCLMessageVal failed: vector is not size 1"));
119 precision_ = precision;
134 std::stringstream ss;
140 if ((log10(abs(dval_)) + precision_) <= MAX_DBL_PRECISION)
141 ss << std::fixed << std::setprecision(precision_) << dval_;
143 ss << std::setprecision(precision_) << dval_;
148 case cpp_long: s = as<std::string>(lval_);
return true;
150 case cpp_bool: s = (bval_) ?
"true" :
"false";
return true;
152 default:
return false;
161 if (boost::iequals(sval_,
"true") || boost::iequals(sval_,
"1"))
163 else if (boost::iequals(sval_,
"false") || boost::iequals(sval_,
"0"))
172 b = boost::numeric_cast<
bool>(dval_);
183 b = boost::numeric_cast<
bool>(lval_);
191 case cpp_bool: b = bval_;
return true;
193 default:
return false;
204 double d = boost::lexical_cast<
double>(sval_);
205 t = boost::numeric_cast<
long>(util::unbiased_round(d, 0));
209 if (boost::iequals(sval_,
"true"))
211 else if (boost::iequals(sval_,
"false"))
221 t = boost::numeric_cast<
long>(util::unbiased_round(dval_, 0));
229 case cpp_long: t = lval_;
return true;
231 case cpp_bool: t = (bval_) ? 1 : 0;
return true;
233 default:
return false;
244 d = boost::lexical_cast<
double>(sval_);
246 catch (boost::bad_lexical_cast&)
248 if (boost::iequals(sval_,
"true"))
250 else if (boost::iequals(sval_,
"false"))
262 d = boost::numeric_cast<
double>(lval_);
270 case cpp_bool: d = (bval_) ? 1 : 0;
return true;
272 default:
return false;
276 goby::transitional::DCCLMessageVal::operator
double()
const 282 return std::numeric_limits<double>::quiet_NaN();
285 goby::transitional::DCCLMessageVal::operator
bool()
const 294 goby::transitional::DCCLMessageVal::operator std::string()
const 303 goby::transitional::DCCLMessageVal::operator long()
const 312 goby::transitional::DCCLMessageVal::operator
int()
const {
return long(*
this); }
314 goby::transitional::DCCLMessageVal::operator unsigned()
const {
return long(*
this); }
316 goby::transitional::DCCLMessageVal::operator float()
const {
return double(*
this); }
318 goby::transitional::DCCLMessageVal::operator std::vector<goby::transitional::DCCLMessageVal>()
const 320 return std::vector<DCCLMessageVal>(1, *
this);
323 bool goby::transitional::DCCLMessageVal::operator==(
const DCCLMessageVal& mv)
const 331 default:
return false;
335 bool goby::transitional::DCCLMessageVal::operator==(
const std::string& s)
const 338 return get(us) && us == s;
341 bool goby::transitional::DCCLMessageVal::operator==(
double d)
const 344 return get(us) && us == d;
347 bool goby::transitional::DCCLMessageVal::operator==(
long l)
const 350 return get(us) && us == l;
353 bool goby::transitional::DCCLMessageVal::operator==(
bool b)
const 356 return get(us) && us == b;
364 case cpp_string:
return os <<
"std::string: " << mv.sval_;
366 return os <<
"double: " << std::fixed << std::setprecision(mv.precision_) << mv.dval_;
367 case cpp_long:
return os <<
"long: " << mv.lval_;
368 case cpp_bool:
return os <<
"bool: " << std::boolalpha << mv.bval_;
369 default:
return os <<
"{empty}";
373 std::ostream& goby::transitional::
374 operator<<(std::ostream& os, const std::vector<goby::transitional::DCCLMessageVal>& vm)
void set(std::string sval)
set the value with a string (overwrites previous value regardless of type)
bool get(std::string &s) const
extract as std::string (all reasonable casts are done)
std::ostream & operator<<(std::ostream &out, const std::map< std::string, Value > &m)
use this for displaying a human readable version