]> granicus.if.org Git - icinga2/blob - lib/config/objectrule.cpp
Refactor the AST
[icinga2] / lib / config / objectrule.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "config/objectrule.hpp"
21 #include <boost/foreach.hpp>
22 #include <set>
23
24 using namespace icinga;
25
26 ObjectRule::RuleMap ObjectRule::m_Rules;
27 ObjectRule::CallbackMap ObjectRule::m_Callbacks;
28
29 ObjectRule::ObjectRule(const String& name, const boost::shared_ptr<Expression>& filter,
30     const DebugInfo& di, const Object::Ptr& scope)
31         : m_Name(name), m_Filter(filter), m_DebugInfo(di), m_Scope(scope)
32 { }
33
34 String ObjectRule::GetName(void) const
35 {
36         return m_Name;
37 }
38
39 boost::shared_ptr<Expression> ObjectRule::GetFilter(void) const
40 {
41         return m_Filter;
42 }
43
44 DebugInfo ObjectRule::GetDebugInfo(void) const
45 {
46         return m_DebugInfo;
47 }
48
49 Object::Ptr ObjectRule::GetScope(void) const
50 {
51         return m_Scope;
52 }
53
54 void ObjectRule::AddRule(const String& sourceType, const String& name,
55     const boost::shared_ptr<Expression>& filter, const DebugInfo& di, const Object::Ptr& scope)
56 {
57         m_Rules[sourceType].push_back(ObjectRule(name, filter, di, scope));
58 }
59
60 bool ObjectRule::EvaluateFilter(const Object::Ptr& scope) const
61 {
62         return m_Filter->Evaluate(scope);
63 }
64
65 void ObjectRule::EvaluateRules(bool clear)
66 {
67         std::pair<String, Callback> kv;
68         BOOST_FOREACH(kv, m_Callbacks) {
69                 const Callback& callback = kv.second;
70
71                 RuleMap::const_iterator it = m_Rules.find(kv.first);
72
73                 if (it == m_Rules.end())
74                         continue;
75
76                 callback(it->second);
77         }
78
79         if (clear)
80                 m_Rules.clear();
81 }
82
83 void ObjectRule::RegisterType(const String& sourceType, const ObjectRule::Callback& callback)
84 {
85         m_Callbacks[sourceType] = callback;
86 }
87
88 bool ObjectRule::IsValidSourceType(const String& sourceType)
89 {
90         return m_Callbacks.find(sourceType) != m_Callbacks.end();
91 }
92