File: notifications.cpp

package info (click to toggle)
mysql-workbench 5.2.40%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 53,880 kB
  • sloc: cpp: 419,850; yacc: 74,784; xml: 54,510; python: 31,455; sh: 9,423; ansic: 4,736; makefile: 2,442; php: 529; java: 237
file content (107 lines) | stat: -rw-r--r-- 3,103 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* 
 * 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
 */

//#include "stdafx.h"
#include "base/notifications.h"
#include "base/log.h"
#include <stdexcept>

DEFAULT_LOG_DOMAIN("base");

using namespace base;

NotificationCenter *NotificationCenter::get()
{
  static NotificationCenter *nc = 0;
  if (!nc)
    nc = new NotificationCenter();
  return nc;
}


Observer::~Observer()
{
  NotificationCenter *nc = NotificationCenter::get();
  if (nc->remove_observer(this))
    log_error("INTERNAL ERROR: Observer %p was deleted while still listening for notifications\n", this);
}


void NotificationCenter::add_observer(Observer *observer, const std::string &name)
{
  ObserverEntry entry;
  entry.observer = observer;
  entry.observed_notification = name;
  _observers.push_back(entry);
}

/*
void NotificationCenter::add_observer(Observer *observer, boost::function<void (const std::string &, void*, NotificationInfo &)> &callback, const std::string &name)
{
  ObserverEntry entry;
  entry.observer = observer;
  entry.callback = callback;
  entry.observed_notification = name;
  _observers.push_back(entry);
}*/


bool NotificationCenter::remove_observer(Observer *observer, const std::string &name)
{
  bool found = false;
  for (std::list<ObserverEntry>::iterator next, iter = _observers.begin(); iter != _observers.end();)
  {
    next = iter;
    ++next;
    if (iter->observer == observer && (name.empty() || name == iter->observed_notification))
    {
      found = true;
      _observers.erase(iter);
    }
    iter = next;
  }
  return found;
}


void NotificationCenter::send(const std::string &name, void *sender, NotificationInfo &info)
{
  if (name.substr(0, 2) != "GN")
    throw std::invalid_argument("Attempt to send notification with a name that doesn't start with GNot");
  
  // act on a copy of the observer list, because one of them could remove stuff from the list
  std::list<ObserverEntry> copy(_observers);
  for (std::list<ObserverEntry>::iterator iter = copy.begin(); iter != copy.end(); ++iter)
  {
    if (iter->observed_notification.empty() || iter->observed_notification == name)
    {
      //if (iter->callback)
      //  iter->callback(name, sender, info);
      //else
      iter->observer->handle_notification(name, sender, info);
    }
  }
}


void NotificationCenter::send(const std::string &name, void *sender)
{
  NotificationInfo info;
  send(name, sender, info);
}