1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef _LOG4CXX_HELPERS_OBJECT_H
19 #define _LOG4CXX_HELPERS_OBJECT_H
20 
21 #include <log4cxx/logstring.h>
22 #include <log4cxx/helpers/class.h>
23 #include <log4cxx/helpers/objectptr.h>
24 #include <log4cxx/helpers/classregistration.h>
25 
26 
27 #define DECLARE_ABSTRACT_LOG4CXX_OBJECT(object)\
28 	public:\
29 	class Clazz##object : public helpers::Class\
30 	{\
31 		public:\
32 			Clazz##object() : helpers::Class() {}\
33 			virtual ~Clazz##object() {}\
34 			virtual log4cxx::LogString getName() const { return LOG4CXX_STR(#object); } \
35 	};\
36 	virtual const helpers::Class& getClass() const;\
37 	static const helpers::Class& getStaticClass(); \
38 	static const log4cxx::helpers::ClassRegistration& registerClass();
39 
40 #define DECLARE_LOG4CXX_OBJECT(object)\
41 	public:\
42 	class Clazz##object : public helpers::Class\
43 	{\
44 		public:\
45 			Clazz##object() : helpers::Class() {}\
46 			virtual ~Clazz##object() {}\
47 			virtual log4cxx::LogString getName() const { return LOG4CXX_STR(#object); } \
48 			virtual helpers::ObjectPtr newInstance() const\
49 			{\
50 				return new object();\
51 			}\
52 	};\
53 	virtual const helpers::Class& getClass() const;\
54 	static const helpers::Class& getStaticClass(); \
55 	static const log4cxx::helpers::ClassRegistration& registerClass();
56 
57 #define DECLARE_LOG4CXX_OBJECT_WITH_CUSTOM_CLASS(object, class)\
58 	public:\
59 	virtual const helpers::Class& getClass() const;\
60 	static const helpers::Class& getStaticClass();\
61 	static const log4cxx::helpers::ClassRegistration&  registerClass();
62 
63 #define IMPLEMENT_LOG4CXX_OBJECT(object)\
64 	const ::log4cxx::helpers::Class& object::getClass() const { return getStaticClass(); }\
65 	const ::log4cxx::helpers::Class& object::getStaticClass() { \
66 		static Clazz##object theClass;                         \
67 		return theClass;                                       \
68 	}                                                                      \
69 	const log4cxx::helpers::ClassRegistration& object::registerClass() {   \
70 		static log4cxx::helpers::ClassRegistration classReg(object::getStaticClass); \
71 		return classReg; \
72 	}\
73 	namespace log4cxx { namespace classes { \
74 	const ::log4cxx::helpers::ClassRegistration& object##Registration = object::registerClass(); \
75 	} }
76 
77 
78 #define IMPLEMENT_LOG4CXX_OBJECT_WITH_CUSTOM_CLASS(object, class)\
79 	const log4cxx::helpers::Class& object::getClass() const { return getStaticClass(); }\
80 	const log4cxx::helpers::Class& object::getStaticClass() { \
81 		static class theClass;                                 \
82 		return theClass;                                       \
83 	}                                                         \
84 	const log4cxx::helpers::ClassRegistration& object::registerClass() {   \
85 		static log4cxx::helpers::ClassRegistration classReg(object::getStaticClass); \
86 		return classReg; \
87 	}\
88 	namespace log4cxx { namespace classes { \
89 	const log4cxx::helpers::ClassRegistration& object##Registration = object::registerClass(); \
90 	} }
91 
92 namespace log4cxx
93 {
94 class AppenderSkeleton;
95 class Logger;
96 
97 namespace helpers
98 {
99 class Pool;
100 
101 /** base class for java-like objects.*/
102 class LOG4CXX_EXPORT Object
103 {
104 	public:
DECLARE_ABSTRACT_LOG4CXX_OBJECT(Object)105 		DECLARE_ABSTRACT_LOG4CXX_OBJECT(Object)
106 		virtual ~Object() {}
107 		virtual void addRef() const = 0;
108 		virtual void releaseRef() const = 0;
109 		virtual bool instanceof(const Class& clazz) const = 0;
110 		virtual const void* cast(const Class& clazz) const = 0;
111 };
112 LOG4CXX_PTR_DEF(Object);
113 }
114 }
115 
116 #define BEGIN_LOG4CXX_CAST_MAP()\
117 	const void * cast(const helpers::Class& clazz) const\
118 	{\
119 		const void * object = 0;\
120 		if (&clazz == &helpers::Object::getStaticClass()) return (const helpers::Object *)this;
121 
122 #define END_LOG4CXX_CAST_MAP()\
123 	return object;\
124 	}\
125 	bool instanceof(const helpers::Class& clazz) const\
126 	{ return cast(clazz) != 0; }
127 
128 #define LOG4CXX_CAST_ENTRY(Interface)\
129 	if (&clazz == &Interface::getStaticClass()) return (const Interface *)this;
130 
131 #define LOG4CXX_CAST_ENTRY2(Interface, interface2)\
132 	if (&clazz == &Interface::getStaticClass()) return (Interface *)(interface2 *)this;
133 
134 #define LOG4CXX_CAST_ENTRY_CHAIN(Interface)\
135 	object = Interface::cast(clazz);\
136 	if (object != 0) return object;
137 
138 #endif //_LOG4CXX_HELPERS_OBJECT_H
139