Tag Archive: “code sample”


There are a few tools that I love for C++ development, and one of them is CxxTest. One of my other favorite tools is the Make Project Creator or MPC. It allows you to generate makefiles or visual studio projects from some simple configuration files.

MPC lets you create templates for various projects that can be used again and again.

Here is my template for CxxTest projects:

project {
  Header_Files {
    *.h
    *.hpp
    ^Test*.hpp
    CxxTest {
      ../cxxtest/cxxtest/*.h
    }
  }
  Define_Custom(CxxTest) {
    automatic = 1
    command = $(PERL_BIN)/perl ../cxxtest/cxxtestgen.pl
    output_option = -o
    inputext = .hpp
    source_outputext = .cpp
    commandflags = --abort-on-fail --have-eh --have-std --part
  }
  CxxTest_Files {
    Test*.hpp
  }
  includes += ../cxxtest ..
}

Then in the folder where your test cases live:

project : CxxTest {
  exename=*
  Source_Files {
    main.cpp
  }
  specific(vc71,vc8) {
    postbuild = $(TargetPath) $(CXX_RUNNER_ARGS)
  }
}

You will have to create the main.cpp file manually, normally it is created with perl ../cxxtest/cxxtestgen.pl --root.

If you use boost::regex, don’t do what I just did.

  1. Calling boost::regex_match(std::string&,match_results&,basic_regex&)
  2. Changing the std::string that was passed to regex_match.
  3. Trying to look at the sub-matches in the match_results object.

I didn’t realize that match_results kept references to the string, and so you can’t edit the string.

Perhaps that will save someone from hitting an assert().

I don’t know why this is so hard to find, but it is. I ended up following the suggestion of a google answers question and converting the javascript formula at http://www.csgnetwork.com/csgcolorsel4.html to C++. (Ok, it is just plain C).

// RGB are from 0..1, H is from 0..360, SV from 0..1
double maxC = b;
if (maxC < g) maxC = g;
if (maxC < r) maxC = r;
double minC = b;
if (minC > g) minC = g;
if (minC > r) minC = r;

double delta = maxC - minC;

double V = maxC;
double S = 0;
double H = 0;

if (delta == 0)
{
	H = 0;
	S = 0;
}
else
{
	S = delta / maxC;
	double dR = 60*(maxC - r)/delta + 180;
	double dG = 60*(maxC - g)/delta + 180;
	double dB = 60*(maxC - b)/delta + 180;
	if (r == maxC)
		H = dB - dG;
	else if (g == maxC)
		H = 120 + dR - dB;
	else
		H = 240 + dG - dR;
}

if (H<0)
	H+=360;
if (H>=360)
	H-=360;