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