void setup( const const_string& stream_name ) { if(stream_name.empty()) return; if( stream_name == "stderr" ) m_stream = &std::cerr; else if( stream_name == "stdout" ) m_stream = &std::cout; else { m_file = boost::make_shared<std::ofstream>(); m_file->open( std::string(stream_name.begin(), stream_name.end()).c_str() ); m_stream = m_file.get(); } }
IntType to_int(const const_string & input) { const char * m_firstc = input.begin(); const char * m_lastc = input.end() - 1; //TODO replace with a pow metafunction, if that's possible static IntType max_placeval = static_cast<IntType>(pow(10,std::numeric_limits<IntType>::digits10)); static IntType highest_digit = std::numeric_limits<IntType>::max() / max_placeval; IntType result = 0; IntType placeval = 1; bool signed_ = std::numeric_limits<IntType>::is_signed; bool minus = signed_ && *m_firstc == '-'; const_string::const_iterator firstc = m_firstc + minus; for(const_string::const_iterator i = m_lastc; i >= firstc; --i) { char c = i[0]; bool last = i == firstc; if(c < '0' || c > '9') throw std::bad_cast(); IntType digit = c - '0'; if(placeval == max_placeval && digit > highest_digit) throw std::bad_cast(); IntType tmp = placeval * digit; if( tmp > std::numeric_limits<IntType>::max() - result ) throw std::bad_cast(); result += tmp; if(placeval == max_placeval && !last) throw std::bad_cast(); placeval *= 10; } if(minus) result = -result; return result; }
Player(const_string n, PlayerID i, std::unique_ptr<Controller> c) : name(n.begin(), n.end()), id(i), controller(std::move(c)) {}