How to fix the Unbound module Graphics in an ocaml project

Image
From ~/pr/gitl/ocaml-gol In a constant effort to learn new programming languages, I'm currently trying to use ocaml , a free and open-source general-purpose, multi-paradigm programming language maintained at the Inria . It's basically an extension of Caml with object-oriented features. I'm mostly interested by its functionnal and pattern matching features but the module part of the language can be a bit difficult to understand for someone with little to none ML (Meta Language) background.   The error When trying to use the graphics module to create a graphical window and go just a little further than the simplest helloworld program, here is the result : If the project uses dune : (executable (name ocaml_project) (libraries lwt.unix graphics) ) with this code : let () = Printf.printf "Hello, world!\n";; Lwt_io.printf "Hello, world!\n";; Graphics.open_graph " 800x600";; The first times I built this project running the du

How to handle loading progress using PubSub design pattern

In the RainbruRPG's LocalTest class, we show the map generation progress from libRLGL using a LoadingBar. The loading informations simply come from a Publisher/Subscriber design pattern from the LoadingBarListener override.

Following examples are obviously implemented in C++. It should correctly build on every compiler with, at least, C++11 support.

The PubSub design pattern

This design pattern involves at least three classes. Here is an example UML diagram done with umbrello, a free and open source UML modeler licensed under GPL 2 or later :

A publisher subscriber design pattern UML diagram


In this example, the Publisher class will be able to send message to an arbitrary list of classes that inherits the Listsner abstract type.

From the Publisher point of view

The publisher side needs at least a base class to be overridden by the subscriber and a list of subscribers :

#include <vector>

class Listener
{
public:
  virtual void message1()=0;
  virtual void message2()=0;
  virtual void message3()=0;
};

class Publisher
{
public:
  void subscribe(Listener*);
  void unsubscribe(Listener*);

private:
  std::vector<Listener> mList;

};

void
Publisher::subscribe(Listener* l)
{
  mList.push_back(l);
}

void
Publisher::unsubscribe(Listener* l)
{
  mList.remove(l);
}

The subscriber subclass

Each subscriber must inherits the listener base class and call Publisher::subscribe() :

class Subscriber : public Listener
{
public:
  virtual void message1(){ };
  virtual void message2(){ };
  virtual void message3(){ };
};

Call to action

Now we need to notify all listeners about the loading progress :

void
Publisher::notifyMessage1()
{
  for(auto& i : mList)
    i->message1();
}

For this last part, you will need at least a C++11 compliant compiler to be able to use the range-based for loop. If you can't, use a traditionnal for loop with a vector iterator :

std::vector<Listener>::iterator iter;
for (iter=mList.begin(); iter != mList.end(); ++iter)
  (*iter)->lmessage1();

Conclusion

The pubsub design pattern is very useful when a publisher need to notify one or more unknown subscribers.

Comments

Popular posts from this blog

How to make a map of variant in C++