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     }
47 
RefAndPointer(ChildClass * pHandler)48     RefAndPointer(ChildClass * pHandler)
49     : mpHandler(pHandler), mRef(pHandler)
50     {
51     }
52 
RefAndPointer(uno::Reference<Interface> xRef)53     RefAndPointer(uno::Reference<Interface> xRef)
54     : mRef(xRef)
55     {
56         mpHandler = dynamic_cast<ChildClass *>(xRef.get());
57     }
58 
~RefAndPointer()59     virtual ~RefAndPointer()
60     {
61     }
62 
set(ChildClass * pHandler)63     void set(ChildClass * pHandler)
64     {
65         mpHandler = pHandler;
66         mRef = pHandler;
67     }
68 
set(uno::Reference<Interface> xHandler)69     void set(uno::Reference<Interface> xHandler)
70     {
71         mpHandler = dynamic_cast<ChildClass*>(xHandler.get());
72         mRef = xHandler;
73     }
74 
getPointer() const75     ChildClass * getPointer() const { return mpHandler; }
getRef() const76     const uno::Reference<Interface> getRef() const { return mRef; }
77 
operator =(const RefAndPointer & rSrc)78     RefAndPointer & operator=
79     (const RefAndPointer & rSrc)
80     {
81         set(rSrc.getHandler());
82 
83         return *this;
84     }
85 
is()86     bool is() { return getRef().is(); }
87 
operator ChildClass*()88     operator ChildClass* () { return getPointer(); }
operator uno::Reference<Interface>()89     operator uno::Reference<Interface> () { return getRef(); }
90 };
91 }}
92 #endif // INCLUDED_REF_AND_POINTER_HXX
93