xref: /freebsd/lib/libdevdctl/event_factory.h (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /*-
2  * Copyright (c) 2013 Spectra Logic Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    substantially similar to the "NO WARRANTY" disclaimer below
13  *    ("Disclaimer") and any redistribution must be conditioned upon
14  *    including a substantially similar Disclaimer requirement for further
15  *    binary redistribution.
16  *
17  * NO WARRANTY
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGES.
29  *
30  * Authors: Justin T. Gibbs     (Spectra Logic Corporation)
31  */
32 
33 /**
34  * \file devdctl_event_factory.h
35  */
36 
37 #ifndef _DEVDCTL_EVENT_FACTORY_H_
38 #define	_DEVDCTL_EVENT_FACTORY_H_
39 
40 /*============================ Namespace Control =============================*/
41 namespace DevdCtl
42 {
43 
44 /*============================= Class Definitions ============================*/
45 /*------------------------------- EventFactory -------------------------------*/
46 /**
47  * \brief Container for "event type" => "event object" creation methods.
48  */
49 class EventFactory
50 {
51 public:
52 	/**
53 	 * Event creation handlers are matched by event type and a
54 	 * string representing the system emitting the event.
55 	 */
56 	typedef std::pair<Event::Type, std::string> Key;
57 
58 	/** Map type for Factory method lookups. */
59 	typedef std::map<Key, Event::BuildMethod *> Registry;
60 
61 	/** Table record of factory methods to add to our registry. */
62 	struct Record
63 	{
64 		Event::Type         m_type;
65 		const char         *m_subsystem;
66 		Event::BuildMethod *m_buildMethod;
67 	};
68 
69 	const Registry &GetRegistry()				const;
70 	Event *Build(Event::Type type, NVPairMap &nvpairs,
71 		     const std::string eventString)		const;
72 
73 	EventFactory(Event::BuildMethod *defaultBuildMethod = NULL);
74 
75 	void UpdateRegistry(Record regEntries[], size_t numEntries);
76 
77 
78 protected:
79 	/** Registry of event factory methods providing O(log(n)) lookup. */
80 	Registry	    m_registry;
81 
82 	Event::BuildMethod *m_defaultBuildMethod;
83 };
84 
85 inline const EventFactory::Registry &
86 EventFactory::GetRegistry() const
87 {
88 	return (m_registry);
89 }
90 
91 } // namespace DevdCtl
92 #endif /*_DEVDCTL_EVENT_FACTORY_H_ */
93