Skip to main content

Going up step by step

Have you ever wanted to be able to iterate through your own class as if it was a cool STL container?
Do something like this:

Then it there is a way to integrate your class into STL and get powerful support. In order to be able to provide interface to for each loop you need to support in your custom classes set of operations such as:

After giving it a good time to fix all the bugs and understand them all as single piece you will notice what really for each loop needs to be able to iterate through a custom object.

Here a code example. Simple one.

If you have a big class on your hands that stores all the data and you want to implement a more secure and encapsulated class you can implement a set of Wrappers and Iterators to iterate all of those edges of your class.

This code sample is a bit crazy. But it shows how you can separate iteration routine and the rest of class logic. Controlling complexity is a goal of software architecture. Patterns, SOLID and other instruments are there only cause of some bright minds wanted to show their jobs on that topic for other people to use it.

Iterators are neat concept separating STL algorithms and containers. Watching on Iterator pattern from another angle reveals different way to look at it's place in STL. Since Iterators usually implemented inside of containers they as glue connects two islands of the STL archipelago.

It's better to think about this relations as if there is a strictly established policy of data translation. Such high level of abstraction model let us see what the idea behind design of STL, as well as start to see other libraries on such level.

Iterate and prosper.

Popular posts from this blog

Maturing, relocation, getting to the point

So it's been a long time I've wrote something for the blog.
Since the last post few things have changed. For example - I've moved in a foreign country. I went into Ukraine which is not a big deal you would say, but for me it's a pretty significant change in my life.

I've worked a two years in solo on a video related projects, one of which was related to trans-coding video on fly using a gstreamer and the other one was about storing and providing access to a video content by special tags, this one was a nightmare.

The gstreamer is a neat open source solution in video processing libraries. The people designed it are crazy ones - such a over-complicated implementation to achieve flexibility and effectiveness in processing video on various platforms and hardware. The most important about this library for me was it's non-existing support. Or should I say community driven support? I don't know even for now. At the time I've had a couple questions, so basical…

Tuples: are they useful?

In some languages there is such a thing as tuple which makes real to write such code (this time it's in python):

def f(x):
 # do stuff return (True, modified_string)

success, modified_string = f(something) 
C++ hadn't such feature until C++11. That was one of the reasons to pass variable as referenced argument, which ended up creating huge code bases that have unused arguments in it's functions. That happened in order to save revers compatibility and allow clients to use stable interfaces to access libraries without checking the version of dynamically attached library in order to use correct functions.