Awesomium is a fantastic tool that I’ve been using for the past few months on the Card Kingdom project. I posted a brief description and reason for switching to use Awesomium here, so I won’t go too much into how awesome Awesomium is. It just is, trust me. One thing that is not so awesome about Awesomium is how they handle strings. To fix this, I wrote some utilities to make dealing with the Awesomium strings much easier.
This first utility is a function that converts Awesomium strings, awe_string
, to C++ Standard Strings, std::string
. Awesomium has multiple types of string creation functions for converting unicode, wide and ASCII characters to awe_string
, but no way to convert awe_string
to ASCII. To convert from unicode, or wide characters, to non-wide characters is not as straight forward as one might think. Here is a helper function called awe_string_to_std()
(in the same vein as awe_string_to_...
functions):
std::string& awe_string_to_std( const awe_string* aweString, std::string& outString ) { // get the length of the awe_string int length = awe_string_to_wide( aweString, NULL, 0 ); // create the wide and ascii buffers to hold the data wchar_t* wbuff = new wchar_t[ length + 1 ]; char* buff = new char[ length + 1 ]; // set the data to zero memset( buff, 0, length + 1 ); memset( wbuff, 0, sizeof( wchar_t ) * (length + 1) ); // get the string contents from the awe_string to the wide buffer awe_string_to_wide( aweString, wbuff, length ); // convert the wide char set to ascii char set size_t outCount; wcstombs_s( &outCount, buff, length + 1, wbuff, _TRUNCATE ); // set the now ascii string to the std::string outString = buff; // clean up the buffer delete[] wbuff; delete[] buff; // return the out string return outString; }
This function is incredibly helpful for converting awe_string
s to std::string
s which is quite helpful for debugging purposes, as well as communicating between the application and JavaScript via Awesomium.
Converting from awe_string
to std::string
is all well and good, and converting from std::string
to awe_string
is straight forward, but one thing that needs to done on each awe_string
is to destroy it. Calling awe_string_create_from_ascii()
and awe_string_destroy()
is quite annoying and requires pointers for each string. To encapsulate awe_strings
even further, I wrote a helper class called AweString
that converts std::string
to awe_string
, and visa-versa. This class finally makes strings in Awesomium awesome:
class AweString { public: // Default constructor AweString() : m_aweString( NULL ) {} // Constructor to convert awe_string to std::string AweString( awe_string* str ) : m_aweString( str ) { awe_string_to_std( m_aweString, m_stdString ); } // Constructor to convert std::string to awe_string AweString( const std::string& str ) : m_aweString( NULL ) { m_stdString = str; m_aweString = awe_string_create_from_ascii( m_stdString.c_str(), m_stdString.length() ); } // Deconstructor to destroy the awe_string automatically // and clear the std::string ~AweString() { if( m_aweString ) { awe_string_destroy( m_aweString ); m_aweString = NULL; } m_stdString.clear(); } // Operator to convert this object to get the awe_string // version of this string operator const awe_string*() const { return m_aweString; } // Operator to convert this object to get the std::string // version of this string operator const std::string&() const { return m_stdString; } private: // Private copy and assignment constructor AweString( const AweString& other ) : m_aweString( other.m_aweString ), m_stdString( other.m_stdString ) {} AweString& operator=( const AweString& other ) { return (*this); } // Two representations of the string awe_string* m_aweString; std::string m_stdString; };
All code listed above is free to use or modify. The formatting is kind of weird and not my normal coding standards so it can been shown fully without scroll bars.