c++

if - else vs if and returns revisited (not asking about multiple returns ok or not)

With regards this example from Code Complete: Comparison Compare(int value1, int value2) { if ( value1 < value2 ) return Comparison_LessThan; else if ( value1 > value2 ) return Comparison_GreaterThan; else return Comparison_Equal; } You could also write this as: Comparison Compare(int value1, int value2) { if ( value1 < value2 ) ...

Windows CE internals, TEB (Thread Environment Block)

Hello everybody, I'm not sure if it's a good place to ask such a question. I'm trying to port some low level library (dbghelp) to wince and I need access to some api that aren't available on wince. I need to access TEB (Thread Environment Block). There are a couple of API's available on PC that do that, but there is no such thing on WinC...

add custom context menu to hosted web browser control

I am hosting a web browser control, and want to provide my own context menu. Ideally, I want to present my own context menu, that contains the original browser's context menu (with all addins etc.) as a sub menu. If that's not possible / to tricky, I'd be ok with e.g. normally showing my context menu, and showing the original one when ...

handle keyboard events / shortcuts in hosted web browser control

I am hosting a web browser control, and I don't know how to fetch hotkeys such as [F1] when the control has focus. My primary need is displaying custom help when the user presses F1, however, generally being able to provide additionla shortcuts would be nice. (additional information is available at my related question - I hope it was t...

How to create a global parameters object

Hi, Here's a common, simple task: Read configuration settings from a configuration file, save the settings (e.g. as a hash) in an object, access this object from various objects that need to access the configuration parameters. I found this implementation for the ConfigFile class implementation and it works. My question is: what is the...

Can one unroll a loop when working with an integer template parameter?

I have the following code: template <int size> inline uint hashfn( const char* pStr ) { uint result = *pStr; switch ( size ) { case 10: result *= 4; result += *pStr; case 9: result *= 4; result += *pStr; ... ... case 2: result *= 4; result += *p...

Inheriting from std::exception, ambiguity in definitions of std::exception.

So I am a little confused, I have been looking around trying to determine an appropriate way of inheriting from std::exception for my own type. Now according to cplusplus.com (and i know this isn't necessarily the standard, thats why I'm asking), std::exception is a base class with no members. However, after looking at my implementation ...

Threadsafe Vector class for C++

Does anyone know a quick and dirty threadsafe vector class for c++? I am multithreading some code, and I believe the problem I have is related to the way the vectors are used. I plan to rewrite the code, but before I go crazy redoing the code, I would like to test it with a threadsafe vector to be sure. I also figure if such a thing i...

What are the best prettyprint options for C++?

Hi All, My partner and I are working on a prettyprinter for C++. The tool parses C++ and prints the resulting AST, so we have quite a bit of flexibility. We've implemented a few options for the user to control the output and now we're looking for opinions about the most important options. If you could take a look at our current (belo...

ifstream seekg beyond end does not return eof in VS 2008 Express?

In VS 2005, I have some code that looks like this: ifs.open("foo"); while (!ifs.eof()) { ifs.read(&bar,sizeof(bar)); loc = ifs.tellg(); loc += bar.dwHeaderSize; // four byte boundary padding if ((loc % 4) != 0) loc += 4 - (loc % 4); ifs.seekg(loc,ios::beg); } ifs.close(); The code worked fine in VS 2005...

C++ Encapsulation Techniques

I'm trying to properly encapsulate a class A, which should only be operated on by class B. However, I want to inherit from class B. Having A friend B doesn't work -- friendship isn't inherited. What's the generally accepted way of accomplish what I want, or am I making a mistake? To give you a bit more color, class A represents a com...

Performance impact of using write() instead of send() when writing to a socket

Hello; I am working on writing a network application in C++ on the Linux platform using the typical sockets API, and I am looking at 2 alternative ways of writing a byte array to a TCP stream: either by calling write(), or by calling send(). I know that, since this is Linux, the socket handle is simply a file descriptor, and therefore ...

"stable_sort()ing" a STL <list> in C++

Hi all! I think the question title is clear enough: is is possible to stable_sort() a std::list in C++? Or do I have to convert it to a std::vector? I'm asking because I tried a simple example and it seems to require RandomAccessIterators, which a linked list doesn't have. So, how do I stable sort a std::list()? EDIT: sample code that ...

Error in std::list::sort with custom comparator (expected primary-expression before ')' token)

Hi all! The title is the main question. The exact scenario (I am 'using namespace std;'): void SubstringMiner::sortByOccurrence(list<Substring *> & substring_list) { list::sort(substring_list.begin(), substring_list.end(), Substring::OccurrenceComparator); } This is the comparator definition: class Substring { // ... class...

iPhone OpenGL ES incorrect alpha blending

I have a problem with incorrect alpha blending results with openGL ES on iPhone. This is my code for creating texture object: glGenTextures(1, &tex_name); glBindTexture(GL_TEXTURE_2D, tex_name); glTextImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width, tex_height, GL_RGBA, GL_UNSIGNED_BYTE, tex_data); 'tex_data' is loaded from raw RGBA8888...

Display 32bit bitmap - Palette

Hello I have an image data in a buffer(type - long) from a scanner which is 32 bit. For example, buffer[0]'s corresponding pixel value is 952 which is [184, 3, 0, 0] <-[R,G,B,A]; I want to display/Paint/draw on to the screen; I am confused when i tried to read about displying bitmaps. I looked at win32 functions, CBitmap class, wi...

writing list of dynamic array to file in binary form>

Hi , I want to write a structure which has a list of integer id. The list can be of varying length. typedef struct ss_iidx_node { int totalFreq; vector < int > docIDList; }s_iidx_node; Now, I wish to write this structure in a file and read it back. How can I do it? Wrting is done: fwrite(&obj,sizeof(s_iidx_node),1,dat_fd...

Is it possible to customize the Visual Studio autoformat?

I'm using Visual Studio to develop a C/C++ library. I would like to know if there is a way to customize the autoformat tool (Ctrl+K,F) so that: It automatically break lines that are bigger than 120 columns Format a function/method parameter the following way: void myFunction(int parameterA, float parameterB, string par...

Good C++ string manipulation library

I'm sorry for flaming std::string and std::wstring. They are quite limited and far from being thread safe. Performance wise, they are not that good too. I miss simple features: Splitting a string into array/vector/list Simple & intuitive case-insensitive find & replace Support for i18n without worrying about string or wstring Conversi...

Opinion Poll on Unnamed Structs

Having a bit of internal debate in my group about the use of unnamed structs\unions in our C++ code. I'd like to get feedback from the community of what you think. Should they be used at all? When is it valuable? What are the pitfalls? etc. Example: union SFlags { struct { unsigned int A : 1; unsigned int B...