Wednesday, November 25, 2009

cpp_trace intro

Development logging is a powerful tool any real time or embedded programmer employs. Do you use development logging from your code?.. If yes, cpp_trace library may help.

I started to write this library since I need it at every my project. And with every project I needed to make some logging component which allows to filter, buffer, route to output device and bla-bla-bla.
Every time I faced the same problems: time consuming filtering, delayed text formatting and filter id managing. These are the reasons to design new effective solution.

Main cpp_trace design decisions are:
1. Use any declared type for filter ids (do you know C++ type lists?..).
This means you are have no limits for ids such as number of bits for a mask or range boundary for enum.
2. Use compile time filtering if possible (partial template specification allows to eliminate unused code).
This means you can use trace instructions at any place with any frequency in code - it has no impact on production performance.
3. Comply with STL streams.
This means you can follow standard at first. Second, you can override operator<< and control every parameter independently (to make buffering of unprocessed parameters and delay text formatting).

I use the library for a year now. It is just an alpha version which has no run-time filtering and delayed text formatting so far. I found it extremely useful for a unit testing since you can identify a filter with class type you are implementing. Then at unit test just list classes you want to debug and get the log from it! From another point, if you like common filter identification by constant values or enumerations: just use BOOST wrappers for constants that converts constant value into a type.

If you are interested, read more at cpp_trace wiki and enjoy.

Tuesday, November 3, 2009

Automatic Mock Object For C++

Two or three years ago when I dive to Java for a half a year I studied Mock objects. At Java (or C#) it build around a reflection. During these years I discussed with my friends and colleagues about C++ Mock object libraries. A sharp corner of the discussions was a possibility to write powerful C++ Mock library without a reflection.
If you see any of existent C++ Mock implementations you find it requires to implement every mock object class by hands. Afterwards you can adjust its behavior at runtime like in Java. My opinion was to use code generation to implement a Mock. I even started to write some code with this concept in mind...
Another alternative is try to use C++ generic programming technique to emulate some reflection features at compile time. But it seemed hard and may not enough. Any case you cannot enumerate interface methods :(
There was some time while I found AMOP - Automatic Mock Object For C++. It is a very young project (first download became available at January'2008). I looked at the AMOP description, decided it very interesting to try but didn't find time to dive inside. I had think it need much time to understand the code and setup it for my project :(
Several days ago I found a hour to try it because I face task to write a really isolated test.
I was surprised how easy it can be. Just single include and library contains three cpp files! No any code generation, just instantiate a template and write a scenario like in Java!

Would you think it is possible to have such a lightweight solution with C++?.. Now I can answer - yes! See yourself at AMOP wiki.

PS: I am not a guy who like C# or even Java! I really like C++. I like it because it allows to do what you want and what you need in many ways. The AMOP example shows the power of C++. C++ may cover solutions you would never have thought.

C++ lamer notes

Hello there!

Some my friends who read my arbitrary blog are dissatisfied by my posts related to a SW development. That's the reason to start the new blog here. It is especially targeted to the SW development topics which are interesting for me.

/lexis