OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
safearray.h
Go to the documentation of this file.
1#if !defined(_SAFEARRAY_H)
2#define _SAFEARRAY_H
3
4#if !defined(_OLE2_H_)
5# include <ocf/autodefs.h>
6#endif
7
8//
9// Class VarType
10// ~~~~~ ~~~~~~~
11// VarType provides OLE interpretations to C++ basic types.
12//
13template<class T>
14class VarType {
15 public:
16 virtual VARTYPE OLEType() = 0;
17};
18
19template<>
20class VarType<bool> {
21 public:
22 VARTYPE OLEType()
23 {
24 return VT_BOOL;
25 }
26};
27
28template<>
29class VarType<char> {
30 public:
31 VARTYPE OLEType()
32 {
33 return VT_I1;
34 }
35};
36
37template<>
38class VarType<double> {
39 public:
40 VARTYPE OLEType()
41 {
42 return VT_R8;
43 }
44};
45
46template<>
47class VarType<float> {
48 public:
49 VARTYPE OLEType()
50 {
51 return VT_R4;
52 }
53};
54
55template<>
56class VarType<int> {
57 public:
58 VARTYPE OLEType()
59 {
60 return VT_INT;
61 }
62};
63
64template<>
65class VarType<long> {
66 public:
67 VARTYPE OLEType()
68 {
69 return VT_I4;
70 }
71};
72
73template<>
74class VarType<short> {
75 public:
76 VARTYPE OLEType()
77 {
78 return VT_I2;
79 }
80};
81
82template<>
83class VarType<unsigned char> {
84 public:
85 VARTYPE OLEType()
86 {
87 return VT_UI1;
88 }
89};
90
91template<>
92class VarType<unsigned int> {
93 public:
94 VARTYPE OLEType()
95 {
96 return VT_UINT;
97 }
98};
99
100template<>
101class VarType<unsigned long> {
102 public:
103 VARTYPE OLEType()
104 {
105 return VT_UI4;
106 }
107};
108
109template<>
110class VarType<unsigned short> {
111 public:
112 VARTYPE OLEType()
113 {
114 return VT_UI2;
115 }
116};
117
118//
119// Class TSafeArray
120// ~~~~~ ~~~~~~~~~~
121// TSafeArray encapsulates the properties of OLE's SAFEARRAY and
122// provides easy access to individual members of it.
123//
124template<class T>
125class TSafeArray : public TAutoVal {
126 public:
127 TSafeArray(int n);
128 ~TSafeArray();
129
130 // Indexing operator
131 //
132 T& operator[](int index);
133};
134
135// TSafeArray inline functions
136//
137template<class T>
138inline
140:
141 TAutoVal()
142{
143 SAFEARRAYBOUND bound;
144 bound.lLbound = 0;
145 bound.cElements = n;
146
147 VARTYPE varType = VarType<T>().OLEType();
148
149 ((VARIANT*)this)->vt = (VARTYPE)(varType | VT_ARRAY);
150 ((VARIANT*)this)->parray = ::SafeArrayCreate(varType, 1, &bound);
151}
152
153template<class T>
154inline
156{
157 // TAutoVal::~Clear() takes care of freeing the array!
158}
159
160template<class T>
161T&
163{
164 T* tmp = (T*)(GetArray()->pvData);
165 return tmp[index];
166}
167
168#endif // _SAFEARRAY_H
OLE Automation Class Definitions.
TSafeArray(int n)
Definition safearray.h:139
T & operator[](int index)
Definition safearray.h:162
VARTYPE OLEType()
Definition safearray.h:22
VARTYPE OLEType()
Definition safearray.h:31
VARTYPE OLEType()
Definition safearray.h:40
VARTYPE OLEType()
Definition safearray.h:49
VARTYPE OLEType()
Definition safearray.h:58
VARTYPE OLEType()
Definition safearray.h:67
VARTYPE OLEType()
Definition safearray.h:76
virtual VARTYPE OLEType()=0