xref: /haiku/src/apps/cortex/Persistence/XMLElementMapping.h (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
1 // XMLElementMapping.h
2 // * PURPOSE
3 //   A simple class (template implementing a non-template
4 //   base interface) to encapsulate the operation:
5 //   "make an object for this XML element."
6 //
7 // * HISTORY
8 //   e.moon		04oct99		Begun.
9 
10 #ifndef __XMLElementMapping_H__
11 #define __XMLElementMapping_H__
12 
13 #include <functional>
14 #include <String.h>
15 
16 #include "cortex_defs.h"
17 __BEGIN_CORTEX_NAMESPACE
18 
19 class IPersistent;
20 
21 // The base class:
22 
23 class XMLElementMapping {
24 public:													// *** data
25 	const BString									element;
26 
27 public:													// *** interface
28 	virtual ~XMLElementMapping() {}
29 	XMLElementMapping(
30 		const char*									_element) :
31 		element(_element) {}
32 
33 	virtual IPersistent* create() const =0;
34 };
35 
36 // The template:
37 
38 template <class T>
39 class Mapping :
40 	public	XMLElementMapping {
41 public:
42 	virtual ~Mapping() {}
43 	Mapping(
44 		const char*									element) :
45 		XMLElementMapping(element) {}
46 
47 	IPersistent* create() const {
48 		return new T();
49 	}
50 };
51 
52 // compare pointers to Mappings by element name
53 struct _mapping_ptr_less : public std::binary_function<XMLElementMapping*,XMLElementMapping*,bool> {
54 public:
55 	bool operator()(const XMLElementMapping* a, const XMLElementMapping* b) const {
56 		return a->element < b->element;
57 	}
58 };
59 
60 
61 __END_CORTEX_NAMESPACE
62 #endif /*__XMLElementMapping_H__*/
63