Nicolás Brailovsky


A modern blog

Throwing destructors

author Posted by: nico on date Sep 20th, 2011 | filed Filed under: C++

We already know what happens when you throw from a constructor. Ending up with a half built object is not good, but suppose we do manage to build one correctly. What happens if we throw in a destructor instead? The results are usually much worse, with a very real possibility of having your program terminated. Read on for a brief explanation on the perils of throwing constructors.

So, according to RAII pattern, resource deallocation should occur during the destructor, yet resource freeing is not exempt of possible errors. How would you notify of an error condition?

    • First error handling choice, you notify /dev/null of the error condition. Best case, you may log the error somewhere, but you can’t do anything about it, you end up ignoring it. Not good, usually you’ll want to do something about the error condition, even more if it’s transient.
    • Second choice, throw. The user (of the class) will know something has gone horribly wrong. This option seems better, yet it has some disadvantages too (just as it happened with throwing destructors; when is an object completely deleted? is it ever deleted if an exception is thrown whilst running?)

Yet the worst part is not resource leaking through half destroyed objects, the worst part is having your program call std::abort.

Think of it this way: when an exception is active, the stack is unwind, i.e. the call stack is traversed backwards until a function which can handle the exception is found. And you just can’t unwind the stack while unwinding the stack (you’d need a stack of stacks) so the reasonable thing to do is call std::abort.

So, what can you do about it? Go to your favorite jobs posting site and start searching for a PHP position, you’ll sleep better at nights.

Zero padding for Bash scripts

author Posted by: nico on date Sep 15th, 2011 | filed Filed under: Console

Lately I found myself trying to generate a video from a series of images generated by a program. Doesn’t sound difficult, until you start running into a stupid issue: your 1000th frame will come before your 2nd frame!

Luckily there’s a very easy fix for this problem, just add zero padding in a bash script. How?

  1. for i in `seq 1 10`; do echo $i; done

That will print all the numbers between 1 and 10. This one will do the same, with zero padding:

  1. for i in `seq 1 10`; do printf "%02d\n" $i; done

200th post!

author Posted by: nico on date Sep 14th, 2011 | filed Filed under: Meta-post

Yes, this is the post number 200 on this blog. Considering we are a month away of starting the fourth year, I guess that gives me quite a lousy periodicity, but I am still surprised I post somewhat regularly here after such a long time (hey, in programmer years that’s like a whole life!).

Since the beginning this blog has mutated from being  my public notepad to being a place where I research new topics, or write about things that are generally interesting to me. I lost many readers for posting crazy metaprogramming stuff and for constantly babbling about Vim and Linux, but hey, I’m proud of if. Let’s see what the next 200 posts bring here.

Automagic document conversion for your makefiles

author Posted by: nico on date Sep 13th, 2011 | filed Filed under: LaTeX, Programming

So, now you have a common makefile, ready to be used for a TDD project and for code coverage report automagic generation. Not only that, but it even speaks to endlessly annoy your team. What else can we add to this makefile? Well, automatic documentation generation, clearly.

You want to batch convert .doc to .pdf using the command line on a server without a GUI? Or you need automated .ppt to .swf conversion through cron, a sysvinit service, or a remote web server? Online conversion services such as Zamzar.com and Media-convert.com not working for you? Whichever formats you need to batch convert, PyODConverter is a simple Python script for just this purpose.

http://www.oooninja.com/2008/02/batch-command-line-file-conversion-with.html

 


Activating tildes and accents for a USA keyboard layout in Ubuntu

author Posted by: nico on date Sep 8th, 2011 | filed Filed under: Gnome

Wow. This time the title of the post may actually be longer than its contents. How do you enable accents and tildes in Ubuntu? You need it to type cool characters like á, Ó or ñ (hey, my name has one of these!).

If you are on Windows I think you have to install a new map, and then guess where the key would be. Or use an alt+something magic spell. In Ubuntu, it works by default you just need to add a compose key, Go to System > Preferences > Keyboard > Options > compose key position, select right alt (or whatever you fancy), there you go, now it works. Try it by typing alt + ‘ + a.

A talking makefile

author Posted by: nico on date Sep 6th, 2011 | filed Filed under: Makefiles
So, after learning how to use makefiles, then how to use makefiles for TDD and for code coverage report, now you need to annoy your whole team with a talking makefile. What could be better to notify everyone on your team when a test fails than a synthesized voice commanding you to fix your program?
  1. test: $(TEST_SRCS)
  2.         @for TEST in $(TEST_BINS); do \
  3.                 make "$$TEST"; \
  4.                 echo "Execute $(TEST)"; \
  5.                 if ! ./$$TEST; then \
  6.                         echo "Oh noes! I detected a failed test from $$TEST. Go and fix your program!" | festival –tts ; \
  7.         done

Try it. You’ll love it.

Bonus chatter: when Valgrind detects over $MUCHOS errors it’ll print “Too many errors detected. Go and fix your program”, then it won’t print so much detail in the next backtraces.

Edit pdf files in Ubuntu

author Posted by: nico on date Sep 1st, 2011 | filed Filed under: LaTeX, Linux

Well, for some reason my LaTeX py-pygments stopped compiling. Thanks for breaking backwards compatibility, you pig-ments.

I had two options, either spend hours trying to fix this by altering the preamble, or just edit the pdf file. Yeah, I know, editing the pdf sounds ugly as hell, but hey at 2 am in the morning I’ll take anything. And pdfedit was there to save the day (night). Just apt-get install pdfedit, it’s in the repo.

A Makefile for code coverage report with C++

author Posted by: nico on date Aug 30th, 2011 | filed Filed under: Makefiles

So far you should know how to use makefiles and you should have a nice testable project. Then you have everything ready to get a coverage report. Yeah, using makefiles, you guessed!

This time we’ll depend on two tools, gcov and gtest. These are in Ubuntu’s repositories, so you should have no problem getting them. I won’t even bother to explain this makefile (not because it’s obvious but because I don’t really remember how it works. I wrote this over a year ago).

  1. .PHONY: clean coverage_report
  2. coverage_report:
  3.         # Reset code coverage counters and clean up previous reports
  4.         rm -rf coverage_report
  5.         lcov –zerocounters –directory .
  6.         $(MAKE) COMPILE_TYPE=code_coverage &&\
  7.         $(MAKE) COMPILE_TYPE=code_coverage test
  8.         lcov –capture –directory $(BIN_DIR)/$(OBJ_DIR)/code_coverage –base-directory . -o salida.out &&\
  9.         lcov –remove salida.out "*usr/include*" -o salida.out &&\
  10.         genhtml -o coverage_report salida.out
  11.         rm salida.out

Bonus makefile target: make your code pretty:

  1. .PHONY: pretty
  2. pretty:
  3.         find -L|egrep \.(cpp|h|hh)$$’|egrep -v ‘svn|_Test.cpp$$’ | xargs astyle –options=none

Remember to change your astyle options as needed.

Bonus II: Example project using gcov and gtest: gcov_gtest_sample.tar. The irony? It doesn’t use my common makefile, it predates it.

Link: ASCII graphs, 2.0 style

author Posted by: nico on date Aug 25th, 2011 | filed Filed under: Misc

Every once in a while you need to draw a graph to quickly convey some information, and you don’t want all the hassle of opening paint, drawing whatever you want, exporting it as png, and all that stuff. Sometimes it’s just easier to do it as ASCII art, only you don’t want to spend hours carefully aligning pipes and dashes. For these times Asciiflow exists.

Give it a try, it’s a great way to quickly generate a diagram. Just remember to use monospace fonts.

A Makefile for TDD with C++

author Posted by: nico on date Aug 22nd, 2011 | filed Filed under: Makefiles, Programming

So, after reading my post about makefiles you decided that you like them but would like to add some TDD to be buzzword compliant? No problem, that’s easy to do.

Assuming you use a naming convention such as this one:

  1. path/to/src/Object.h
  2. path/to/src/Object.cpp
  3. path/to/src/Object_Test.cpp

then it’s easy to auto detect which tests should be built:

  1. TEST_SRCS := $(patsubst ./%, %, $(shell find -L|grep -v svn|egrep "_Test\.cpp$$" ) )
  2. TEST_BINS := $(addprefix ./$(BIN_DIR)/, $(patsubst %.cpp, %, $(TEST_SRCS)) )

Then we have to define a special rule with pattern matching to compile the tests:

  1. $(BIN_DIR)/%_Test: $(patsubst $(BIN_DIR)/%, %, %_Test.cpp ) %.cpp %.h
  2.         @echo "Making $@"
  3.         @mkdir -p $(shell dirname $@)
  4.         g++ $(CXXFLAGS) -g3 -O0 $< -o $@ -lpthread -lgtest_main -lgmock $(OBJECTS) $(LDFLAGS)

and some magic to auto execute every test when we “make test”:

  1. test: $(TEST_SRCS)
  2.         @for TEST in $(TEST_BINS); do \
  3.                 make "$$TEST"; \
  4.                 echo "Execute $(TEST)"; \
  5.                 ./$$TEST; \
  6.         done

Everything nice and tidy for a copy & paste session:

  1. TEST_SRCS := $(patsubst ./%, %, $(shell find -L|grep -v svn|egrep "_Test\.cpp$$" ) )
  2. TEST_BINS := $(addprefix ./$(BIN_DIR)/, $(patsubst %.cpp, %, $(TEST_SRCS)) )
  3.  
  4. $(BIN_DIR)/%_Test: $(patsubst $(BIN_DIR)/%, %, %_Test.cpp ) %.cpp %.h
  5.         @echo "Making $@"
  6.         @mkdir -p $(shell dirname $@)
  7.         g++ $(CXXFLAGS) -g3 -O0 $< -o $@ -lpthread -lgtest_main -lgmock $(OBJECTS) $(LDFLAGS)
  8.  
  9. .PHONY: test
  10. test: $(TEST_SRCS)
  11.         @for TEST in $(TEST_BINS); do \
  12.                 make "$$TEST"; \
  13.                 echo "Execute $(TEST)"; \
  14.                 ./$$TEST; \
  15.         done

Now you just need to run make test. Remember to add the proper Vim’s mapping.