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