Sign in to follow this  
  • entries
    455
  • comments
    639
  • views
    422594

GTL Part 7 : The auto-filter reg system

Sign in to follow this  

103 views

So, a few days ago I decided to work on the easy inclusion of new filters system.

To start with filter selection was based on a hardcoded if..then..else structure, with the file extensions just hardcoded into the source.

So, with easy of use in mind I started restructuring the code so that things could be registered automagically on start up. The key to this was a registration class, which holds a static pointer to an std::map. When the object is constructed it enters into the map an extension string and a creation function which will be used later to construct an object of that type.

A macro is then defined to create a unique named static instance of that at class, so at startup the creation function is registered.

Then, each filter source file needs to define a function to create the correct type of object and use the macro to register it.

However, this involved making pointers to objects and the iostreams filters only take references to objects. This required a couple more changed to be made to the source.

Firstly, a FilterBase class is created which all the filters have to inherit from and includes a virtual 'read' function.

A FilterProxy class is also needed, this takes a shared_ptr to a FilterBase object and when its read function is called it passes it onto the stored object. This class allows for a tempory object to be constructed and passed into the Filtered stream which controls the lifetime of the real filtering object.

This FilterProxy class does currently have a small issue. The read() function is templated in it, however the read() function for FilterBase is virtual with a fixed signature. So, after some investigation and poking for now I've settled with performing a reinterrupt cast from the Source type to a known type pointer and passing that to the filter objects.

Now, this does work, but it feels a bit like a dirty hack. However, it should also only be around for a short time, as I plan to write my own Device for the filter stream to use, this should allow me to fix the type passed and not rely on templates.

The alternative, is to see if I can redesign it so that I can dispose to the cast, however right now I dont see how I can achieve this.

So, after all this hacking it does indeed work, huzzah!
Its not perfect, but it works and allows for adhock registering of filter objects.

The next step is to allow for loading from streams directly, not just from files. However, firstly I need to invesitage how the libraries which allow you todo this work.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now