Some Quick Snippets of Code


Fixed Point


This is a Fixed Point math class for c++. It supports all combinations which add up to a native data types (8.8/16.16/24.8/etc). The template parameters are the number of bits to use as the base type for both the integer and fractional portions, invalid combinations will yield a compiler error, the current implementation makes use of c++-11 static assert to make this more readable. It should be a nice drop in replacement for native float types. Here's an example usage:

      typedef numeric::Fixed<16, 16> fixed; fixed f;
    

This will declare a 16.16 fixed point number. Operators are provided though the use of boost::operators. multiplication and division are implemented in free functions named numeric::multiply and numeric::divide which use std::enable_if to choose the best option. If a larger type is available, it will use the accurate and fast scaled math version. If here is not a larger type, then it will fall back on the slower multiply and emulated divide (which unfortunately has less precision). This system allows the user to specialize the multiplication and division as needed.

With this design, on usual x86/amd64 systems, fixed types as large as numeric::Fixed<32, 32> are supported (though the 64-bit fixed types have a lower precision divide)

Option Parser


Here is a c++ Option Parser implementing using using standard c++ so it should be easily portable. It supports GNU style "double dash" options which may have 0 or 1 operand. Any option may or may not be required. It is fairly easy to use and pretty robust overall. It also has nice --help support.

Rotation Operators


A coworker of mine complained that c++ doesn't define any ROL or ROR operators, namely rotate left and rotate right. So here are two quick and dirty template functions which do a little bit twiddling to get it done for anyone who is interested. Because it is implemented with templates, it should work for all integer types, signed or unsigned.

Binary Grep


I found myself wanting to be able to quickly search a large binary file for a certain byte pattern the other day, only the be dissapointed and find nothing. So bgrep was born. It's usage is simple, you specify one or more bytes on the command line and it will search stdin for that byte pattern reporting the results as it goes. For example:

      ./bgrep 01 02 03 04 < my_file
    

This will search my_file for the byte pattern 01 02 03 04 (which of course is 04030201 when viewed as a little-endian 32-bit value). You may search for patterns of any length which is 1 byte or more.

Properties


The concept of properties is something that c++ lacks, yet other languages have. In general, they are not needed, but sometimes can be useful in making code more clear. So I developed a solution to implementing properties with the least amount of intrusive code in the class it's being applied to. I've also provided an example of its usage.

uint128


Because I wanted to support larger fixed point values, I decided to make a uint128 class which acts like a normal unsigned integer type in pretty much every way. All the basic mathematical operations are there and can be output to a std::ostream as well.

Matrix


This Matrix class is the basis for the maps in my RPG engine. It is copyable, resizable, and uses the c-like [] operators which are properly bound checked and may throw std::out_of_range. For example:

      Matrix<int> m(2, 3); m[1][2] = 10;
    

will create a matrix object with a width of 2 and a height of 3 and assign 10 the last valid element. The current version is row major only, but a future version will support column major as well.