1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef INCLUDED_REF_AND_POINTER_HXX
25 #define INCLUDED_REF_AND_POINTER_HXX
26 
27 #include <iostream>
28 #include <com/sun/star/lang/XUnoTunnel.hpp>
29 
30 namespace writerfilter {
31 namespace ooxml
32 {
33 using namespace ::com::sun::star;
34 using namespace ::std;
35 
36 template <class Interface, class ChildClass>
37 class RefAndPointer
38 {
39     mutable ChildClass * mpHandler;
40     mutable uno::Reference<Interface> mRef;
41 
42 public:
RefAndPointer()43     RefAndPointer()
44     : mpHandler(NULL)
45     {
46 #ifdef DEBUG_MEMORY
47         clog << "MEMORY:" << mpHandler->getInstanceNumber() << ":RefAndPointer"
48              << endl;
49 #endif
50     }
51 
RefAndPointer(ChildClass * pHandler)52     RefAndPointer(ChildClass * pHandler)
53     : mpHandler(pHandler), mRef(pHandler)
54     {
55 #ifdef DEBUG_MEMORY
56         clog << "MEMORY:" << mpHandler->getInstanceNumber() << ":RefAndPointer"
57              << endl;
58 #endif
59     }
60 
RefAndPointer(uno::Reference<Interface> xRef)61     RefAndPointer(uno::Reference<Interface> xRef)
62     : mRef(xRef)
63     {
64 #if 0
65         uno::Reference<lang::XUnoTunnel> xTunnel( xRef, uno::UNO_QUERY);
66 
67         if (xTunnel.is())
68             mpHandler = reinterpret_cast<ChildClass *>(xTunnel->getSomething(ChildClass::getUnoTunnelId()));
69 #else
70         mpHandler = dynamic_cast<ChildClass *>(xRef.get());
71 #endif
72         if (mpHandler != NULL)
73             clog << "MEMORY:" << mpHandler->getInstanceNumber()
74                  << ":RefAndPointer" << endl;
75     }
76 
~RefAndPointer()77     virtual ~RefAndPointer()
78     {
79 #ifdef DEBUG_MEMORY
80         if (mpHandler != NULL)
81             clog << "MEMORY:" << mpHandler->getInstanceNumber()
82                  << ":~RefAndPointer" << endl;
83 #endif
84     }
85 
set(ChildClass * pHandler)86     void set(ChildClass * pHandler)
87     {
88         mpHandler = pHandler;
89         mRef = pHandler;
90     }
91 
set(uno::Reference<Interface> xHandler)92     void set(uno::Reference<Interface> xHandler)
93     {
94         mpHandler = dynamic_cast<ChildClass*>(xHandler.get());
95         mRef = xHandler;
96     }
97 
getPointer() const98     ChildClass * getPointer() const { return mpHandler; }
getRef() const99     const uno::Reference<Interface> getRef() const { return mRef; }
100 
operator =(const RefAndPointer & rSrc)101     RefAndPointer & operator=
102     (const RefAndPointer & rSrc)
103     {
104         set(rSrc.getHandler());
105 
106         return *this;
107     }
108 
is()109     bool is() { return getRef().is(); }
110 
operator ChildClass*()111     operator ChildClass* () { return getPointer(); }
operator uno::Reference<Interface>()112     operator uno::Reference<Interface> () { return getRef(); }
113 };
114 }}
115 #endif // INCLUDED_REF_AND_POINTER_HXX
116