Author: Anupam Srivastava
What is this book? Here is a brief by another reader of this gem. I got this book when my father bought its hardcover at a mela, back when USSR had just broken,...
Kantara is getting a lot of well deserved praise. It is rare that movies with such good writing get made in India. In that, I wanted to note down some points: The...
Let me start with an example. Imagine if we wanted to pick a machine among a list of machines that is best suited to run a CPU-heavy process. It is actually a...
Vivek Agnihotri is known for making movies like Hate Story and Zid that rely on titillation to keep you engaged. So, I had very little hope of technical strengths in the new...
The Beginning Imagine you are an average guy who is interested in coding and building something great that solves someone’s problems. Imagine you are not really writing an interactive desktop program, but...
Multiple companies are selling RO (Reverse Osmosis) based water purifiers these days, and all of them will have some confusing diagram to accompany their product so that you are forced to call...
So you got yourself access to a Debian server (or virtual private server) and have logged into it by one way or the other. In this tutorial, we will learn how to...
A generic factory is a factory that can register any class type. It is very easy to write because we are allowed to use void pointers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <functional> class Factory { public: Factory() {} ~Factory() {} void Register(const std::string _key, const std::function<void *()> _creator) { m_creators[_key] = _creator; } template<typename ClassType> ClassType* Get(const std::string _key) const { return static_cast<ClassType *>(m_creators.at(_key)()); } private: std::map<std::string, std::function<void *()> > m_creators; }; |
Usage:
|
int main() { Factory f; f.Register("Derived1", &BaseClass<Derived1>::CreateInterface); f.Register("Derived2", &BaseClass<Derived2>::CreateInterface); Derived1 *d1 = f.Get<Derived1>("Derived1"); Derived2 *d2 = f.Get<Derived2>("Derived2"); Derived1 *d2 = f.Get<Derived1>("Derived2"); // This also compiles!!! } |
As we are...
Problem statement: Find the largest sum that is possible in an array. I.e., find the maximum sub-array. Solution: From the very first this problem looks complicated because of how it is framed....
Ticking away the moments that make up a dull day Fritter and waste the hours in an offhand way. Kicking around on a piece of ground in your home town Waiting for...