summaryrefslogtreecommitdiffstats
path: root/kjsembed/jsopaqueproxy.h
blob: e5a4c1f02874b562daa58729004cb770b788a234 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// -*- c++ -*-

/*
 *  Copyright (C) 2001-2003, Richard J. Moore <rich@kde.org>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT any WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */

#ifndef KJSEMBEDJSOPAQUEPROXY_H
#define KJSEMBEDJSOPAQUEPROXY_H

#include <tqcstring.h>
#include <tqevent.h>

#include <kjs/object.h>
#include <kjsembed/jsproxy.h>

#include <algorithm>
#include <typeinfo>

class QTextStream;

namespace KJSEmbed {

/**
 * Provides a binding to an opaque pointer value. This class stores a pointer
 * without interpreting its type, it also stores the name of the type allowing
 * it to retain type-safety.
 *
 * @author Richard Moore, rich@kde.org
 */
class KJSEMBED_EXPORT JSOpaqueProxy : public JSProxy
{
public:
    /** Creates an opaque proxy. */
    JSOpaqueProxy();

    /** Creates an opaque proxy. */
    template<typename T>
    JSOpaqueProxy( T *nptr, const char *ptype )
    : JSProxy( JSProxy::OpaqueProxy ), ptrtype(ptype), ptr(new Pointer<T>(nptr))
    {

    }

    /** Creates an opaque proxy. */
    JSOpaqueProxy( TQTextStream *ts );

    /** Creates an opaque proxy. */
    JSOpaqueProxy( const TQEvent *ev );

    /** Cleans up. */
    virtual ~JSOpaqueProxy();

    /** Returns the type of the wrapped object.*/
    TQString typeName() const;

    /** Sets the value of the proxy and its type. */
    template<typename T>
    void setValue( T *nptr, const char *ptype ) {
	  if( ptr ) {
	    if(owner() == JavaScript) ptr->cleanup();
	    delete ptr;
	  }
	  ptr = new Pointer<T>(nptr);
	  ptrtype = ptype ? ptype : "void";
    }

    /** Sets the value of the proxy to a TQTextStream. */
    void setValue( TQTextStream *ts );

    /** Sets the value of the proxy to a TQEvent. */
    void setValue( const TQEvent *ev );

    /** Returns the proxy value as a pointer. */
    template<typename T>
    T *toNative() {
      if( !ptr ) return 0L; // empty
      T *value = 0L;
      return (T*)ptr->voidStar(); // Hack for now
      return value;  // return real value
    }

    /** Returns true iff the content of this proxy inherits the specified base-class. */
    bool inherits( const char *clazz );

    /**
     * Returns the TQTextStream stored in the proxy. If proxy does not contain
     * a value of type TQTextStream then 0 is returned.
     */
    TQTextStream *toTextStream();

    const TQEvent *toEvent();

    /** Adds the bindings for the opaque proxy to the specified js object. */
    virtual void addBindings( KJS::ExecState *state, KJS::Object &object );

    /** Reimplemented to return the name and class of the target. */
    virtual KJS::UString toString( KJS::ExecState *exec ) const;

private:

    TQTextStream *textstream;
    const TQEvent *event;
    TQCString ptrtype;

    class JSOpaqueProxyPrivate *d;

    // container objects
    class PointerBase
    {
      public:
	virtual ~PointerBase() {};
	virtual void cleanup() = 0;
	virtual const std::type_info &type() const = 0;
	virtual void *voidStar() = 0;
	//template<typename ValueType>
	//virtual void castTo(ValueType *& val) = 0L;
    };

    template<typename ValueType>
    class Pointer : public PointerBase
    {
      public:
	Pointer( ValueType *value) : ptr(value) {}
	~Pointer( ) {}
	void cleanup()
	{
	  delete ptr;
	  ptr=0L;
	}
	const std::type_info &type() const
	{
	  return typeid(ValueType);
	}
	
	void *voidStar()
	{
	  return (void*)ptr;
	}
	//template<typename type>
	//virtual void castTo(type *& val)
	//{
	//	val = dynamic_cast<type*>(ptr);
	//	kdDebug() << "Type " << val << endl;
	//}
	ValueType *ptr;
    };

    PointerBase *ptr;
};

} // namespace KJSEmbed

#endif // KJSEMBEDJSOPAQUEPROXYIMP_H

// Local Variables:
// c-basic-offset: 4
// End: