Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ library API. Using converter classes instead of plain C api
    primarykey
    data
    text
    <p>This question is about C++ &lt;-> C++ interoperability.</p> <p>As is well known implementation of standard library classes/functions may differ across different vendors. Moreover implementation may differ even within same library vendor, when using different compiler keys, configuration (Debug/Release), etc.</p> <p>Due to that reason, many library developers shifts to old plain C-style API. Which leads to uglish error-prone interfaces.</p> <p>For instance, in order to get string from some function, interfaces like Win GetCurrentDirectory function are used:</p> <pre><code>DWORD WINAPI GetCurrentDirectory( __in DWORD nBufferLength, __out LPTSTR lpBuffer ); </code></pre> <p>three parameters + some boilerplate code on both sides(checking if buffer size was enough, etc) just to get simple string.</p> <p>I am thinking to use some auxiliary adapter/proxy class, which will do all conversions automaticly, and can be simply reused.</p> <p>Something like:</p> <pre><code>#include &lt;string&gt; #include &lt;algorithm&gt; #include &lt;iostream&gt; #include &lt;ostream&gt; class StringConverter { char *str; // TODO: use smart pointer with right deleter public: StringConverter(const std::string &amp;user_string) // Will be defined only at user side { str=new char[user_string.length()+1]; (*(std::copy(user_string.begin(),user_string.end(),str)))=0; } operator std::string() // Will be defined only at library side { return std::string(str); } ~StringConverter() { delete [] str; } }; StringConverter foo() { return std::string("asd"); } int main(int argc,char *argv[]) { std::cout &lt;&lt; std::string(foo()) &lt;&lt; std::endl; return 0; } </code></pre> <p><a href="http://ideone.com/EfcKv" rel="nofollow">http://ideone.com/EfcKv</a></p> <p>Note, I plan to have defenition of conversion from user string to StringConverter only at user side, and defenition of conversion from StringConverter to library string only inside library. Also, right deleter should be used (from right heap).</p> <p>What do you think about such approach?</p> <p>Are there some major pitfalls?</p> <p>Are there some superior alternatives?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload