/* 
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the
 * License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#ifndef _BASE_NOTIFICATIONS_H_
#define _BASE_NOTIFICATIONS_H_

// Global Notification Center

// This can be easily misused so before using a new notification, discuss with others
// to ensure it's the right way. 

#include <list>
#include <string>
#include <map>
#include <boost/function.hpp>

#include "base/common.h"

namespace base
{
  typedef std::map<std::string, std::string> NotificationInfo;

  struct BASELIBRARY_PUBLIC_FUNC Observer
  {
    virtual void handle_notification(const std::string &name, void *sender, NotificationInfo &info) = 0;
    ~Observer();
  };

  class BASELIBRARY_PUBLIC_FUNC NotificationCenter
  {
  private:
    struct ObserverEntry
    {
      std::string observed_notification;
      Observer *observer;
      //boost::function<void (const std::string &, void*, NotificationInfo &)> callback;
    };
    
    std::list<ObserverEntry> _observers;
    
  public:
    static NotificationCenter *get();
    
    //void add_observer(Observer *observer, boost::function<void (const std::string &, void*, NotificationInfo &)> &callback, const std::string &name = "");
    void add_observer(Observer *observer, const std::string &name = "");
    bool remove_observer(Observer *observer, const std::string &name = "");

    // notification names MUST start with GN (global notification) for easy grepping

    // must be called from main thread only
    void send(const std::string &name, void *sender, NotificationInfo &info);
    void send(const std::string &name, void *sender);
  };

};

#endif
