C1: deny drops into black holes
[openc2e.git] / Scriptorium.cpp
blob391cc9e910331b6a6586a707b0c4b6e3aa99ea4d
1 /*
2 * Scriptorium.cpp
3 * openc2e
5 * Created by Alyssa Milburn on Wed 10 Nov 2004.
6 * Copyright (c) 2004-2006 Alyssa Milburn. All rights reserved.
7 * Copyright (c) 2005 Bryan Donlan. All rights reserved.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
21 #include "Scriptorium.h"
22 #include <iostream>
24 inline unsigned int Scriptorium::calculateValue(unsigned char family, unsigned char genus, unsigned short species) {
25 return (family + (genus << 8) + (species << 16));
28 void Scriptorium::addScript(unsigned char family, unsigned char genus, unsigned short species, unsigned short event, shared_ptr<script> s) {
29 std::map<unsigned short, shared_ptr<script> > &m = getScripts(calculateValue(family, genus, species));
30 m[event] = s;
33 void Scriptorium::delScript(unsigned char family, unsigned char genus, unsigned short species, unsigned short event) {
34 // Retrieve the list of scripts for the classifier, return if there aren't any.
35 std::map<unsigned int, std::map<unsigned short, shared_ptr<script> > >::iterator x = scripts.find(calculateValue(family, genus, species));
36 if (x == scripts.end())
37 return;
39 // Retrieve the script, return if it doesn't exist.
40 std::map<unsigned short, shared_ptr<script> >::iterator j = x->second.find(event);
41 if (j == x->second.end())
42 return;
44 // Erase the script.
45 x->second.erase(j);
47 // If there are no scripts left, erase the whole list of scripts for this classifier.
48 if (x->second.size() == 0)
49 scripts.erase(x);
52 shared_ptr<script> Scriptorium::getScript(unsigned char family, unsigned char genus, unsigned short species, unsigned short event) {
53 std::map<unsigned short, shared_ptr<script> > &x = getScripts(calculateValue(family, genus, species));
54 if (x.find(event) == x.end()) {
55 std::map<unsigned short, shared_ptr<script> > &x = getScripts(calculateValue(family, genus, 0));
56 if (x.find(event) == x.end()) {
57 std::map<unsigned short, shared_ptr<script> > &x = getScripts(calculateValue(family, 0, 0));
58 if (x.find(event) == x.end())
59 return shared_ptr<script>();
60 else
61 return x[event];
62 } else return x[event];
63 } else return x[event];
66 /* vim: set noet: */