Uint64 ConstNamedValueRef::AsUint64() const { switch ( m_entry->type ) { case NAMED_VALUE_BOOL: case NAMED_VALUE_INT: case NAMED_VALUE_UINT: case NAMED_VALUE_INT64: case NAMED_VALUE_UINT64: return m_entry->GetUint64(); case NAMED_VALUE_STRING: { Lexical::Integer< Uint64 > xuint; if ( ! xuint.TryParse( m_entry->GetString() )) { CARAMEL_THROW( "Can't convert string value to uint64, name: %s", m_name ); } return xuint; } case NAMED_VALUE_DOUBLE: CARAMEL_THROW( "Can't convert double value to uint64, name: %s", m_name ); default: CARAMEL_NOT_REACHED(); } }
std::string WideString::ToNarrow( TextEncoding encoding ) const { CARAMEL_ASSERT( TEXT_ENCODING_UTF16_LE != encoding ); if ( m_s.empty() ) { return std::string(); } if ( TEXT_ENCODING_WINDOWS_ACP == encoding ) { encoding = static_cast< TextEncoding >( ::GetACP() ); } // // 1. Computer the required buffer size. // 2. Try to encode the string. // 3. If the buffer is insufficient, double the buffer and try again. // const Int requiredSize = ::WideCharToMultiByte( encoding, // code page 0, m_s.c_str(), m_s.length() + 1, // including the terminating '\0' NULL, 0, NULL, NULL ); if ( 0 == requiredSize ) { CARAMEL_THROW( "Compute required size failed" ); } std::string result; DWORD errorCode = WideString_Encode( result, m_s, encoding, requiredSize ); if ( ERROR_INSUFFICIENT_BUFFER == errorCode ) { // Double the buffer and try again. errorCode = WideString_Encode( result, m_s, encoding, requiredSize * 2 ); } if ( S_OK != errorCode ) { // TODO: Trace some error CARAMEL_THROW( "Encode string failed" ); } return result; }
Double ConstNamedValueRef::AsDouble() const { switch ( m_entry->type ) { case NAMED_VALUE_BOOL: case NAMED_VALUE_INT: return static_cast< Double >( m_entry->GetInt() ); case NAMED_VALUE_UINT: return static_cast< Double >( m_entry->GetUint() ); case NAMED_VALUE_INT64: return static_cast< Double >( m_entry->GetInt64() ); case NAMED_VALUE_UINT64: return static_cast< Double >( m_entry->GetUint64() ); case NAMED_VALUE_DOUBLE: return m_entry->GetDouble(); case NAMED_VALUE_STRING: { Lexical::Floating< Double > xdouble; if ( ! xdouble.TryParse( m_entry->GetString() )) { CARAMEL_THROW( "Can't convert string value to double, name: %s", m_name ); } return xdouble; } default: CARAMEL_NOT_REACHED(); } }
Bool ConstNamedValueRef::AsBool() const { switch ( m_entry->type ) { case NAMED_VALUE_BOOL: return m_entry->GetBool(); case NAMED_VALUE_INT: case NAMED_VALUE_UINT: return ( 0 != m_entry->GetInt() ); case NAMED_VALUE_INT64: case NAMED_VALUE_UINT64: return ( 0 != m_entry->GetInt64() ); case NAMED_VALUE_DOUBLE: return ( 0.0 != m_entry->GetDouble() ); case NAMED_VALUE_STRING: { Lexical::Boolean xbool; if ( ! xbool.TryParse( m_entry->GetString() )) { CARAMEL_THROW( "Can't convert string value to bool, name: %s", m_name ); } return xbool; } default: CARAMEL_NOT_REACHED(); } }
HttpClientImpl::HttpClientImpl( const std::string& baseUrl ) : m_baseUrl( baseUrl ) , m_curl( curl_easy_init() ) { if ( ! m_curl ) { CARAMEL_THROW( "Init curl failed" ); } m_errorBuffer.resize( CURL_ERROR_SIZE, 0 ); /// Configure Curl /// this->SetOption( CURLOPT_ERRORBUFFER, &m_errorBuffer[0] ); this->SetOption( CURLOPT_TIMEOUT, 10 ); this->SetOption( CURLOPT_CONNECTTIMEOUT, 10 ); this->SetOption( CURLOPT_WRITEFUNCTION, &HttpClientImpl::WriteCallback ); // Prevent network thread to raise a signal this->SetOption( CURLOPT_NOSIGNAL, 1 ); // SSL Settings this->SetOption( CURLOPT_SSL_VERIFYPEER, 0 ); this->SetOption( CURLOPT_SSL_VERIFYHOST, 0 ); }
WideString::WideString( const std::string& input, TextEncoding encoding ) { if ( ! this->TryParse( input, encoding )) { CARAMEL_THROW( "Convert to UTF-16 failed, input: %s, encoding: %u", input, encoding ); } }
void DirectoryInfo::Delete() { const Bool ok = boost::filesystem::remove( *m_path ); if ( ! ok ) { CARAMEL_THROW( "Delete directory \"%s\" failed", m_path->ToString() ); } }
std::string ProgramOptionsManager::GetStringOption( const std::string& longName ) const { if ( ! this->Contains( longName )) { CARAMEL_THROW( "Option not found, longName: {0}", longName ); } return m_variablesMap[ longName ].as< std::string >(); }
void AnyInteger_GetFloating( T& value, U intValue ) { if ( ! NumberConverter< T, U >::CanExactConvert( intValue )) { CARAMEL_THROW( "Can't exactly convert %s to %s, value: %s", ToStringT< U >(), ToStringT< T >(), ToString( intValue )); } value = static_cast< T >( intValue ); }
Detail::ConstNamedValueRef NamedValues::operator[]( const std::string& name ) const { auto ientry = m_impl->m_valueEntries.find( name ); if ( m_impl->m_valueEntries.end() == ientry ) { CARAMEL_THROW( "Value not found, name: %s", name ); } return Detail::ConstNamedValueRef( name, &( ientry->second )); }
Path FileInfo::GetExactPath() const { const std::wstring wpath = this->GetPath().ToWstring(); /// Step 1 : Convert original path to short path. const DWORD shortSize = ::GetShortPathNameW( wpath.c_str(), NULL, 0 ); if ( 0 == shortSize ) { CARAMEL_THROW( "GetShortPathName() get buffer size failed" ); } std::vector< Wchar > shortBuffer( shortSize + 1, 0 ); const DWORD shortConverted = ::GetShortPathNameW( wpath.c_str(), &shortBuffer[0], shortBuffer.size() ); if ( ! shortConverted ) { CARAMEL_THROW( "GetShortPathName() convert failed" ); } const std::wstring shortPath( &shortBuffer[0] ); /// Step 2 : Convert short path to long path. const DWORD longSize = ::GetLongPathNameW( shortPath.c_str(), NULL, 0 ); if ( 0 == longSize ) { CARAMEL_THROW( "GetLongPathName() get buffer size failed" ); } std::vector< Wchar > longBuffer( longSize + 1, 0 ); const DWORD longConverted = ::GetLongPathNameW( shortPath.c_str(), &longBuffer[0], longBuffer.size() ); if ( ! longConverted ) { CARAMEL_THROW( "GetLongPathName() convert failed" ); } const std::wstring longPath( &longBuffer[0] ); return Path( longPath ); }
void DirectoryInfo::Create() { // Do nothing if the directory already exists. if ( this->Exists() ) { return; } const Bool ok = boost::filesystem::create_directory( *m_path ); if ( ! ok ) { CARAMEL_THROW( "Create directory \"%s\" failed", m_path->ToString() ); } }
void ProgramOptionsManager::AddPositionalOptions( const std::vector< ProgramOptionValue >& options ) { if ( m_positionalAdded ) { CARAMEL_THROW( "Positional options can only be added once" ); } for ( const auto& option : options ) { m_positionalDesc.add( option.GetLongName().c_str(), 1 ); } m_positionalAdded = true; }
void HttpClientImpl::Perform( const std::string& pathQuery, HttpResponseImpl* response ) { const std::string url = EndsWith( m_baseUrl, '/' ) ? m_baseUrl + pathQuery : m_baseUrl + "/" + pathQuery; this->SetOption( CURLOPT_URL, url.c_str() ); this->SetOption( CURLOPT_WRITEDATA, reinterpret_cast< Void* >( response )); const CURLcode code = curl_easy_perform( m_curl ); if ( CURLE_OK != code ) { CARAMEL_THROW( "Curl perform failed, code: {0}, url: \"{1}\"", code, url ); } CARAMEL_VERIFY( CURLE_OK == curl_easy_getinfo( m_curl, CURLINFO_RESPONSE_CODE, &response->m_responseCode )); }
void SimpleApp::InitPlatform() { /// Redirect Caramel Trace to Win32 Debugger /// auto debugger = new Windows::DebuggerTraceAdapter; debugger->BindBuiltinChannels( Trace::LEVEL_DEBUG ); Trace::Listeners::AddManaged( debugger ); /// Create GLView with Size and Title /// const DesktopSettings& ds = m_settings.desktop; auto glview = GLViewImpl::createWithRect( ds.frameTitle, Rect( 0, 0, ds.frameWidth, ds.frameHeight )); auto director = Director::getInstance(); CARAMEL_ASSERT( nullptr == director->getOpenGLView() ); director->setOpenGLView( glview ); // // Specify Assets path in Win32 // - 1. Relateive path from working directory to assets. // 2. Add an additional assets.win32 . // if ( ds.assetsPath.empty() ) { CARAMEL_THROW( "Assets path not set" ); } auto fileUtils = FileUtils::getInstance(); fileUtils->addSearchPath( ds.assetsPath ); fileUtils->addSearchPath( ds.assetsPath + ".win32" ); }