I have run into a small design predicament. It’s not really a “problem” per se, it’s just bothering me that I cannot find a cleaner solution 🙂 and I was hoping some of you might have some good ideas or alternative solutions.
Say I have some pieces of data that can arrive on a communications channel (serial/IP, e.t.c) out of sync and at any time. The individual pieces of data are small, say ints or floats. They relate to some state of the machine sending the data. I wan’t to design a simple “property” system, where I am alerted when a single data-item is received, or, when MULTIPLE data items have been received. For example, a single alert would be given to me, when I successfully collected two ints and a float.
Lets say the remote system can send me this:
DATA1 : float
DATA2 : int
DATA3 : int
DATA4 : double
DATA5 : short
Now, in pseudo-code, what I want to do is something like this (using macros or whatever)
PROPERTY(DATA1)
PROPERTY(DATA2, DATA3)
PROPERTY(DATA4)
that is, each property monitors a variable amount of data chunks, and sends me a SINGLE alert, when all of the pieces being monitored has changed/been filled.
At the moment I am using sigc++ which I think is a brilliant callback library. I have a working implementation, but it’s cumbersome to code, because C++ does not really support variable argument lists, in the way that I want, so what I have done so far, is to have a single class, PROPERTY, which has a number of overloaded constructors, supporting 1 to X number of arguments, which is not really “dynamic”. If I need more variables monitored, I need to make sure there is a constructor to handle the number of arguments.
Maybe it’s just me trying to over-design 🙂
I’d write a
Propertyclass that stores a collection of data chunks. Each chunk is either a Boost.Variant or some type of union, or a pointer to an an object that implements a data chunk interface.The class could be initialized like
or like
if a C++0x initializer list can be used, where
IntValetc. are wrappers that convert a more primitive data chunk type to the variant type used in thePropertycollection.