Apache log4cxx Version 0.13.0
Loading...
Searching...
No Matches
object.h
Go to the documentation of this file.
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>
24
25
26#define DECLARE_ABSTRACT_LOG4CXX_OBJECT(object)\
27 public:\
28 class Clazz##object : public helpers::Class\
29 {\
30 public:\
31 Clazz##object() : helpers::Class() {}\
32 virtual ~Clazz##object() {}\
33 virtual log4cxx::LogString getName() const { return LOG4CXX_STR(#object); } \
34 };\
35 virtual const helpers::Class& getClass() const;\
36 static const helpers::Class& getStaticClass(); \
37 static const log4cxx::helpers::ClassRegistration& registerClass();
38
39#define DECLARE_LOG4CXX_OBJECT(object)\
40 public:\
41 class Clazz##object : public helpers::Class\
42 {\
43 public:\
44 Clazz##object() : helpers::Class() {}\
45 virtual ~Clazz##object() {}\
46 virtual log4cxx::LogString getName() const { return LOG4CXX_STR(#object); } \
47 virtual object* newInstance() const\
48 {\
49 return new object();\
50 }\
51 };\
52 virtual const helpers::Class& getClass() const;\
53 static const helpers::Class& getStaticClass(); \
54 static const log4cxx::helpers::ClassRegistration& registerClass();
55
56#define DECLARE_LOG4CXX_OBJECT_WITH_CUSTOM_CLASS(object, class)\
57 public:\
58 virtual const helpers::Class& getClass() const;\
59 static const helpers::Class& getStaticClass();\
60 static const log4cxx::helpers::ClassRegistration& registerClass();
61
62#define IMPLEMENT_LOG4CXX_OBJECT(object)\
63 const ::log4cxx::helpers::Class& object::getClass() const { return getStaticClass(); }\
64 const ::log4cxx::helpers::Class& object::getStaticClass() { \
65 static Clazz##object theClass; \
66 return theClass; \
67 } \
68 const log4cxx::helpers::ClassRegistration& object::registerClass() { \
69 static log4cxx::helpers::ClassRegistration classReg(object::getStaticClass); \
70 return classReg; \
71 }\
72 namespace log4cxx { namespace classes { \
73 const ::log4cxx::helpers::ClassRegistration& object##Registration = object::registerClass(); \
74 } }
75
76
77#define IMPLEMENT_LOG4CXX_OBJECT_WITH_CUSTOM_CLASS(object, class)\
78 const log4cxx::helpers::Class& object::getClass() const { return getStaticClass(); }\
79 const log4cxx::helpers::Class& object::getStaticClass() { \
80 static class theClass; \
81 return theClass; \
82 } \
83 const log4cxx::helpers::ClassRegistration& object::registerClass() { \
84 static log4cxx::helpers::ClassRegistration classReg(object::getStaticClass); \
85 return classReg; \
86 }\
87 namespace log4cxx { namespace classes { \
88 const log4cxx::helpers::ClassRegistration& object##Registration = object::registerClass(); \
89 } }
90
91namespace log4cxx
92{
93class AppenderSkeleton;
94class Logger;
95
96namespace helpers
97{
98class Pool;
99
101class LOG4CXX_EXPORT Object
102{
103 public:
105 virtual ~Object() {}
106 virtual bool instanceof(const Class& clazz) const = 0;
107 virtual const void* cast(const Class& clazz) const = 0;
108};
110}
111
118template<typename Ret,
119 typename Type,
120 bool = std::is_base_of<Ret, helpers::Object>::value,
121 bool = std::is_base_of<Type, helpers::Object>::value>
122std::shared_ptr<Ret> cast(const std::shared_ptr<Type>& incoming)
123{
124 if(!incoming)
125 {
126 return std::shared_ptr<Ret>();
127 }
128
129 Ret* casted = reinterpret_cast<Ret*>(const_cast<void*>(incoming->cast(Ret::getStaticClass())));
130
131 if ( casted )
132 {
133 return std::shared_ptr<Ret>( incoming, casted );
134 }
135
136 return std::shared_ptr<Ret>();
137}
138
139}
140
141#define BEGIN_LOG4CXX_CAST_MAP()\
142 const void * cast(const helpers::Class& clazz) const\
143 {\
144 const void * object = 0;\
145 if (&clazz == &helpers::Object::getStaticClass()) return (const helpers::Object *)this;
146
147#define END_LOG4CXX_CAST_MAP()\
148 return object;\
149 }\
150 bool instanceof(const helpers::Class& clazz) const\
151 { return cast(clazz) != 0; }
152
153#define LOG4CXX_CAST_ENTRY(Interface)\
154 if (&clazz == &Interface::getStaticClass()) return (const Interface *)this;
155
156#define LOG4CXX_CAST_ENTRY2(Interface, interface2)\
157 if (&clazz == &Interface::getStaticClass()) return (Interface *)(interface2 *)this;
158
159#define LOG4CXX_CAST_ENTRY_CHAIN(Interface)\
160 object = Interface::cast(clazz);\
161 if (object != 0) return object;
162
163#endif //_LOG4CXX_HELPERS_OBJECT_H
Definition: class.h:38
base class for java-like objects.
Definition: object.h:102
virtual const void * cast(const Class &clazz) const =0
virtual bool instanceof(const Class &clazz) const =0
LOG4CXX_PTR_DEF(AppenderAttachableImpl)
Definition: messagehandler.h:23
std::shared_ptr< Ret > cast(const std::shared_ptr< Type > &incoming)
Attempt to cast one Object to another kind of Object.
Definition: object.h:122
#define DECLARE_ABSTRACT_LOG4CXX_OBJECT(object)
Definition: object.h:26