Apache log4cxx  Version 0.12.1
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_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 
91 namespace log4cxx
92 {
93 class AppenderSkeleton;
94 class Logger;
95 
96 namespace helpers
97 {
98 class Pool;
99 
101 class 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 
118 template<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>
122 std::shared_ptr<Ret> cast(const std::shared_ptr<Type>& incoming)
123 {
124  Ret* casted = reinterpret_cast<Ret*>(const_cast<void*>(incoming->cast(Ret::getStaticClass())));
125 
126  if ( casted )
127  {
128  return std::shared_ptr<Ret>( incoming, casted );
129  }
130 
131  return std::shared_ptr<Ret>();
132 }
133 
134 }
135 
136 #define BEGIN_LOG4CXX_CAST_MAP()\
137  const void * cast(const helpers::Class& clazz) const\
138  {\
139  const void * object = 0;\
140  if (&clazz == &helpers::Object::getStaticClass()) return (const helpers::Object *)this;
141 
142 #define END_LOG4CXX_CAST_MAP()\
143  return object;\
144  }\
145  bool instanceof(const helpers::Class& clazz) const\
146  { return cast(clazz) != 0; }
147 
148 #define LOG4CXX_CAST_ENTRY(Interface)\
149  if (&clazz == &Interface::getStaticClass()) return (const Interface *)this;
150 
151 #define LOG4CXX_CAST_ENTRY2(Interface, interface2)\
152  if (&clazz == &Interface::getStaticClass()) return (Interface *)(interface2 *)this;
153 
154 #define LOG4CXX_CAST_ENTRY_CHAIN(Interface)\
155  object = Interface::cast(clazz);\
156  if (object != 0) return object;
157 
158 #endif //_LOG4CXX_HELPERS_OBJECT_H
#define DECLARE_ABSTRACT_LOG4CXX_OBJECT(object)
Definition: object.h:26
LOG4CXX_PTR_DEF(AppenderAttachableImpl)
base class for java-like objects.
Definition: object.h:101
Definition: class.h:37
Definition: appender.h:32
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