For more details, here and here are some references I found on the subject.
You might ask yourself “Why does this guy needs a ramdisk?… What a geek!”, and wouldn’t mind. It was kind of a epiphany, when I remembered to use a ramdisk to run a “‘Lack of space’ Test Case” on the application I’m building right now. After running the application, I just had to copy a mp3 album to occupy the remaining disk space… et voilá, welcome the expected “error message”. No, not a SIGSEGV, a real error dialog!…
redirects the stdout from ‘command’ to a file called ‘filename’. The file shall be created if not present, or otherwise overwritten.
> filename : > filename
are a special case of redirection, creating a zero-lenght file. The ‘:’ is a dummy placeholder that generates no content - usually used when the first form is not allowed by the shell.
command >> filename
also redirects the stdout to file, but the content is appended to file in the file already exists.
note: pre-pending 1, 2 or & will redirect stdout, stderr or both , according to the redirection ‘operator’ (>, >>).
Andrew Hunt is becoming one of my favorite authors. The first book I read from Andy was “The Pragmatic Programmer“, which is a book full of insights on the life philosophy to become an expert programmer. Now I got my hands in “Pragmatic Thinking & Learning - Refactor Your Wetware” and it is another excellent book. This book is not only for software developers, it is intended for a wider audience and provides lots of tips to improve the way you think - it certainly made me think about the way I put by “mind” into the job.
I strongly recommend it…
My goal was to read an XML file containing a list of class names, and then instanciate the classes according to that list by caling a simple function inside a for loop.
The Abstract Factory is a very interesting design pattern that provides an encapsulation mechanism to the creation of families of objects. Combining this with the Factory Method it is possible build a factory that, after ‘generator method’ being registered, enables a simple mapping between a string object and an object.
The example shows an abstract Product which implements a given interface. There are two concrete products that derive from the base definition: a BigProduct and a SmallProduct. Then we have the Factory<T> that will be the production line for objects of type T. In the example, with T = Product, we have Factory<Product> that allows to call registration and creation methods, respectivelly:
The trick is that during registration you must provide the Generator function (also known as Factory Method) that will be used as building method by the factory. In this implementation, the Generator
is defined by the Factory itself but it could be defined anywhere as long as it returns a pointer to an abstract product. The implemented generation is based on default construction:
template <class P> template <class DP> // where P is Product and DP any kind of Derived Product P* Factory<P>::generate() { return new DP(); }
In practice, using the factory is quite simple…
int main (void) { typedef Factory<Product> GenericFactory; // Simplifying the factory type… GenericFactory factory; // Register products and ‘generation’ methods… factory.registerProduct(”BigProduct”, GenericFactory::generate<BigProduct>); factory.registerProduct(”SmallProduct”, GenericFactory::generate<SmallProduct>); // Calling for product creation // NOTE: The use of auto pointer ensures that newly allocated object // is automatically destroyed as the end of scope std::auto_ptr<Product> bp(factory.newProduct(”BigProduct”)); std::auto_ptr<Product> sp(factory.newProduct(”SmallProduct”)); bp->doFeature(); sp->doFeature();
return 0; }
There is one very interesting evolution of this design that I’ve done, to enable runtime class registration. I’ll post on that the next time.