~ubuntu-branches/ubuntu/trusty/grantlee/trusty

« back to all changes in this revision

Viewing changes to corelib/pluginpointer_p.h

  • Committer: Bazaar Package Importer
  • Author(s): Harald Sitter
  • Date: 2010-06-11 23:41:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100611234145-oas7rhdrbwy8j55c
Tags: upstream-0.1.1
ImportĀ upstreamĀ versionĀ 0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  This file is part of the Grantlee template system.
 
3
 
 
4
  Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
 
5
 
 
6
  This library is free software; you can redistribute it and/or
 
7
  modify it under the terms of the GNU Lesser General Public
 
8
  License as published by the Free Software Foundation; either version
 
9
  2 of the Licence, or (at your option) any later version.
 
10
 
 
11
  This library is distributed in the hope that it will be useful,
 
12
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
  Library General Public License for more details.
 
15
 
 
16
  You should have received a copy of the GNU Lesser General Public
 
17
  License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
*/
 
20
 
 
21
#ifndef GRANTLEE_PLUGINPOINTER_H
 
22
#define GRANTLEE_PLUGINPOINTER_H
 
23
 
 
24
#include <QtCore/QSharedPointer>
 
25
#include <QtCore/QPluginLoader>
 
26
 
 
27
namespace Grantlee
 
28
{
 
29
 
 
30
void _deleter( QPluginLoader *loader )
 
31
{
 
32
  loader->unload();
 
33
  delete loader;
 
34
}
 
35
 
 
36
/**
 
37
  @brief A smart pointer for handling plugins.
 
38
 
 
39
  @author Stephen Kelly <steveire@gmail.com>
 
40
*/
 
41
template <typename PluginType>
 
42
class PluginPointer
 
43
{
 
44
  class _Dummy;
 
45
public:
 
46
  PluginPointer()
 
47
    : m_plugin( 0 )
 
48
  {
 
49
 
 
50
  }
 
51
 
 
52
  // This allows returning 0 from a function returning a PluginType*
 
53
  PluginPointer( _Dummy* )
 
54
    : m_plugin( 0 )
 
55
  {
 
56
 
 
57
  }
 
58
 
 
59
  PluginPointer( const QString &fileName )
 
60
    : m_object( 0 ), m_plugin( 0 )
 
61
  {
 
62
    m_pluginLoader = QSharedPointer<QPluginLoader>( new QPluginLoader( fileName ), _deleter );
 
63
 
 
64
    m_object = m_pluginLoader->instance();
 
65
    if ( !m_object )
 
66
      return;
 
67
 
 
68
    m_plugin = qobject_cast<PluginType*>( m_object );
 
69
  }
 
70
 
 
71
  QString errorString() {
 
72
    return m_pluginLoader->errorString();
 
73
  }
 
74
 
 
75
  QObject* object() {
 
76
    return m_object;
 
77
  }
 
78
 
 
79
  PluginType* operator->() {
 
80
    return m_plugin;
 
81
  }
 
82
 
 
83
  operator bool() {
 
84
    return m_plugin ? true : false;
 
85
  }
 
86
 
 
87
  PluginType* data() const {
 
88
    return m_plugin;
 
89
  }
 
90
 
 
91
private:
 
92
  QObject *m_object;
 
93
  PluginType *m_plugin;
 
94
  QSharedPointer<QPluginLoader> m_pluginLoader;
 
95
};
 
96
 
 
97
}
 
98
 
 
99
#endif