My Project
UDK 3.2.7 C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FreeReference.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org. If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 
29 #ifndef INCLUDED_cppu_FreeReference_hxx
30 #define INCLUDED_cppu_FreeReference_hxx
31 
32 #include "uno/environment.hxx"
33 #include "cppu/Map.hxx"
35 
36 
37 namespace cssuno = com::sun::star::uno;
38 
39 
40 namespace cppu
41 {
47  template< class T >
49  {
50  cssuno::Environment m_env;
51  T * m_pObject;
52 
53  public:
54  FreeReference() : m_pObject(NULL) {}
55 
57  : m_env(cssuno::Environment::getCurrent()),
58  m_pObject(pObject)
59  {
60  }
61 
62  FreeReference(T * pObject)
63  : m_env(cssuno::Environment::getCurrent()),
64  m_pObject(pObject)
65  {
66  if (m_pObject)
67  m_env.get()->pExtEnv->acquireInterface(m_env.get()->pExtEnv, m_pObject);
68  }
69 
70  explicit FreeReference(cssuno::Reference<T> const & xRef)
71  : m_env(cssuno::Environment::getCurrent()),
72  m_pObject(xRef.get())
73  {
74  if (m_pObject)
75  m_env.get()->pExtEnv->acquireInterface(m_env.get()->pExtEnv, m_pObject);
76  }
77 
79  : m_env (rOther.m_env),
80  m_pObject(rOther.m_pObject)
81  {
82  if (m_pObject)
83  m_env.get()->pExtEnv->acquireInterface(m_env.get()->pExtEnv, m_pObject);
84  }
85 
86 
88  {
89  clear();
90  }
91 
92  cssuno::Environment getEnv() const throw (cssuno::RuntimeException)
93  {
94  return m_env;
95  }
96 
97  cssuno::Reference<T> get() const throw (cssuno::RuntimeException)
98  {
99  return cssuno::Reference<T>(cppu::mapIn(m_pObject, m_env), SAL_NO_ACQUIRE);
100  }
101 
102  operator cssuno::Reference<T> () const throw (cssuno::RuntimeException)
103  {
104  return get();
105  }
106 
107  cssuno::Reference<T> operator -> () const throw (cssuno::RuntimeException)
108  {
109  return get();
110  }
111 
112  bool is() const throw (cssuno::RuntimeException)
113  {
114  return m_pObject != NULL;
115  }
116 
117  void clear()
118  {
119  if (m_pObject)
120  {
121 
122  m_env.get()->pExtEnv->releaseInterface(m_env.get()->pExtEnv, m_pObject);
123  m_pObject = NULL;
124  m_env.clear();
125  }
126  }
127 
129  {
130  clear();
131 
132  m_pObject = rOther.m_pObject;
133  if (m_pObject)
134  {
135  m_env = rOther.m_env;
136  m_env.get()->pExtEnv->acquireInterface(m_env.get()->pExtEnv, m_pObject);
137  }
138 
139  return *this;
140  }
141 
142  void set(cssuno::Reference<T> const & xRef)
143  {
144  clear();
145 
146  m_pObject = xRef.get();
147  if (m_pObject)
148  {
150 
151  m_env.get()->pExtEnv->acquireInterface(m_env.get()->pExtEnv, m_pObject);
152  }
153  }
154 
155  bool operator == (FreeReference const & rOther) const
156  {
157  return get() == rOther.get();
158  }
159 
160  bool operator != (FreeReference const & rOther) const
161  {
162  return !operator==(rOther);
163  }
164  };
165 }
166 
167 #endif
168 
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */