Changeset 67:7fe4085c903f in rrlib_si_units


Ignore:
Timestamp:
11.01.2019 17:51:07 (6 months ago)
Author:
Max Reichardt <max.reichardt@…>
Branch:
14.08
Phase:
public
Message:

Adds std::istream operator for tQuantity

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • tQuantity.h

    r65 r67  
    331331{ 
    332332  stream << quantity.Value() << " " << TUnit(); 
     333  return stream; 
     334} 
     335 
     336template <typename TUnit, typename TValue> 
     337std::istream &operator >> (std::istream &stream, tQuantity<TUnit, TValue>& quantity) 
     338{ 
     339  TValue value = 0; 
     340  stream >> value; 
     341  std::ostringstream unit_string; 
     342  unit_string << TUnit(); 
     343  internal::ReadSIUnit(stream, unit_string); 
     344  quantity = value; 
    333345  return stream; 
    334346} 
  • tSIUnit.cpp

    r43 r67  
    169169} 
    170170 
     171namespace internal 
     172{ 
     173 
     174void ReadSIUnit(std::istream &stream, std::ostringstream &unit_string) 
     175{ 
     176  std::string unit = unit_string.str(); 
     177  if (!unit.length()) 
     178  { 
     179    return; 
     180  } 
     181 
     182  std::istream::sentry stream_ok(stream, true); 
     183  if (!stream_ok || stream.peek() == std::char_traits<char>::eof()) 
     184  { 
     185    stream.setstate(std::ios_base::failbit); 
     186    return; 
     187  } 
     188 
     189  stream >> std::ws; 
     190  for (size_t i = 0; i < unit.length(); i++) 
     191  { 
     192    auto peeked = stream.peek(); 
     193    if (peeked != unit[i]) 
     194    { 
     195      stream.setstate(std::ios_base::failbit); 
     196      char buffer[2] = { static_cast<char>(peeked == std::char_traits<char>::eof() ? 0 : peeked), 0 }; 
     197      RRLIB_LOG_PRINT(ERROR, "Could not read expected unit string '", unit, "'! Read '", (unit.substr(0, i) + buffer), "'."); 
     198      return; 
     199    } 
     200    stream.get(); 
     201  } 
     202} 
     203 
     204} 
     205 
    171206//---------------------------------------------------------------------- 
    172207// DetermineSymbolComponentsFromExponentList 
  • tSIUnit.h

    r43 r67  
    5959namespace si_units 
    6060{ 
     61namespace internal 
     62{ 
    6163 
    6264//---------------------------------------------------------------------- 
    6365// Forward declarations / typedefs / enums 
    6466//---------------------------------------------------------------------- 
     67 
     68void ReadSIUnit(std::istream &stream, std::ostringstream &unit_string); 
     69} 
     70 
    6571const unsigned int cNUMBER_OF_BASIC_DIMENSIONS = 7; 
    6672 
Note: See TracChangeset for help on using the changeset viewer.