1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_COMMON_CREATE_H
6 #define BALL_COMMON_CREATE_H
7 
8 //@{
9 
10 /**	Virtual construction macro.
11 		This macro is used to define the virtual <b>create</b> method.
12 		On inclusion of this macro in the public interface of a class,
13 		the virtual creation method becomes available. The create method's signature is as follows:
14 		<tt>virtual void* <b>create</b>(bool deep = true, bool empty = false) const</tt> \par
15 		The create method either creates an empty default object of the class (<tt>empty == <b>true</b></tt>)
16 		or a copy of the object. The copy is either deep (<tt>deep == <b>true</b></tt>) or shallow (<tt>deep == <b>false</b></tt>).
17 		By default, the create methods returns a pointer to a deep copy of the object.
18 		The use of the create method requires a (public) default constructor (when creating an empty copy)
19 		or a copy constructor. \par
20 		The macro also implements a static method <tt>createDefault</tt> that returns a void pointer to
21 		a new instance of <tt>name</tt>.
22 
23 		@param	name the class name
24   \ingroup Common
25 */
26 #define BALL_CREATE_DEEP(name)\
27 \
28 	virtual void* create(bool deep = true, bool empty = false) const\
29 	{\
30 		void* ptr;\
31 		if (empty == true)\
32 		{\
33 			ptr = (void*)new name;\
34 		}\
35 		else\
36 		{\
37 			ptr = (void*)new name(*this, deep);\
38 		}\
39 		\
40 		return ptr;\
41 	}\
42 	\
43 	static void* createDefault()\
44 	{\
45 		return static_cast<void*>(new name);\
46 	}
47 
48 /**	Virtual construction macro.
49 		This macro is used to define the virtual <b>create</b> method for classes that do
50 		not define a copy constructor taking a second argument (boolean, deep or shallow copy).
51 		On inclusion of this macro in the public interface of a class,
52 		the virtual creation method becomes available. The create method's signature is as follows:
53 		<tt>virtual void* <b>create</b>(bool deep = true, bool empty = false) const</tt> \par
54 		The create method either creates an empty default object of the class (<tt>empty == <b>true</b></tt>)
55 		or a copy of the object.
56 		The use of the create method requires a (public) default constructor (when creating an empty copy)
57 		and a copy constructor taking a reference to an object.
58 		The macro also implements a static method <tt>createDefault</tt> that returns a void pointer to
59 		a new instance of <tt>name</tt>.
60 		@param	name the class name
61 */
62 #define BALL_CREATE(name)\
63 \
64 	virtual void* create(bool /* deep */ = true, bool empty = false) const\
65 	{\
66 		void* ptr;\
67 		if (empty == true)\
68 		{\
69 			ptr = (void*)new name;\
70 		}\
71 		else\
72 		{\
73 			ptr = (void*)new name(*this);\
74 		}\
75 		\
76 		return ptr;\
77 	}\
78 	\
79 	static void* createDefault()\
80 	{\
81 		return static_cast<void*>(new name);\
82 	}
83 
84 /**	Virtual cloning method definition macro.
85 		If the create method has to be implemented by the user, this macro just defines
86 		the create method and the createDefault method.
87 		The function signatures are:
88 		\verbatim
89 			virtual void* create(bool deep = true, bool empty = false) const;
90 			static void* createDefault();
91 		\endverbatim
92 */
93 #define BALL_DEFINE_CREATE(name)\
94 \
95 	virtual void* create(bool deep = true, bool empty = false) const;\
96 	static void* createDefault();
97 
98 //@}
99 
100 #endif // BALL_COMMON_CREATE_H
101