OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
transferbuffer.h
Go to the documentation of this file.
1//
2/// \file transferbuffer.h
3/// Safe transfer buffers
4//
5// Copyright © 2010 Vidar Hasfjord
6// Distributed under the OWLNext License (see http://owlnext.sourceforge.net).
7//
8
9#ifndef OWL_TRANSFERBUFFER_H
10#define OWL_TRANSFERBUFFER_H
11
12#include <owl/window.h>
13#include <owl/static.h>
14#include <owl/edit.h>
15#include <owl/listbox.h>
16#include <owl/combobox.h>
17#include <owl/combobex.h>
18#include <owl/checkbox.h>
19#include <owl/radiobut.h>
20#include <owl/scrollba.h>
21#include <owl/slider.h>
22#include <owl/checklst.h>
23#include <owl/datetime.h>
24#include <owl/hotkey.h>
25#include <owl/ipaddres.h>
26#include <owl/memcbox.h>
27#include <owl/monthcal.h>
28
29namespace owl {
30
31//
32/// Serves as the base class for safe transfer buffer windows.
33/// The derived class is responsible for upholding a number of invariants.
34/// See member AssignField for more information.
35//
37 : virtual public TWindow
38{
39public:
40
41 //
42 /// TWindow override
43 /// Initiates the transfer of the children data to or from the transfer buffer.
44 //
45 virtual void TransferData(TTransferDirection direction);
46
47 //
48 /// TWindow override
49 /// Dispatches to all the children.
50 /// Checks that all participating children are bound to a field; throws if not.
51 /// See TransferChild.
52 //
53 virtual uint Transfer(void* buffer, TTransferDirection direction);
54
55 //
56 /// Thrown if a a control tries to associate with more than one field.
57 //
60
61 //
62 /// Thrown if a more than one control tries to associate with the same field.
63 //
65 {public: TXFieldConflict(TWindow& c);};
66
67 //
68 /// Thrown if a transfer is attempted with an unbound control.
69 //
71 {public: TXUnboundControl(TWindow& c);};
72
73 //
74 /// Thrown if a validator attempts to meddle with the transfer.
75 //
78
79protected:
80
81 //
82 /// Initializes the private implementation.
83 /// This constructor is protected to not allow stand-alone use of the class.
84 //
86
87 //
88 /// Required for proper clean-up.
89 //
91
92 //
93 /// Stores the field details and enables transfer for the given control.
94 /// Used by derived class templates to create field associations.
95 /// The window (control) and field types should be checked for consistency
96 /// before calling this function. It is a precondition that the passed window
97 /// can transfer data of the right type to the buffer field at the given offset.
98 /// Consequently, it is a precondition that the window's transfer buffer is of a
99 /// type that has such a field at the given offset. These invariants must be
100 /// upheld by the derived class.
101 //
102 void AssignField(TWindow&, size_t offset, size_t size);
103
104private:
105
106 class TImpl; ///< Private implementation class
107 TImpl* pimpl; ///< Private implementation pointer
108
110 TTransferBufferWindowBase& operator=(const TTransferBufferWindowBase&); ///< Disabled
111
112};
113
114//
115/// Provides a customization hook for binding and creating controls.
116/// Every control type should have a specialization of this template,
117/// containing two function templates;
118///
119/// \code
120/// template <class TBuffer>
121/// void Restrict(TFieldType TBuffer::*, TControlType&);
122///
123/// template <class TBuffer>
124/// TControlType& Create(TFieldType TBuffer::*, TWindow*, int id, ...);
125/// \endcode
126///
127/// These signatures restrict the binding of the control to a field in,
128/// and only in, the transfer buffer specified for the window.
129/// See TTransferBufferBinderImplementation for a generic implementation.
130//
131template <class TControlType>
134
135//
136/// Provides the type-safe interface needed to set up a safe transfer buffer.
137/// This class is responsible for assigning controls to fields in the transfer buffer
138/// in a type-safe manner. This means upholding the invariants required by the base
139/// class. See TTransferBufferWindowBase::AssignField for more information.
140//
141template <class TBuffer>
144{
145public:
146
147 typedef TBuffer TTransferBuffer; ///< The template argument type
148
149 //
150 /// Hides the unsafe version in TWindow.
151 //
152 void SetTransferBuffer(TBuffer* b)
153 {
154 TWindow::SetTransferBuffer(b, sizeof(TBuffer));
155 }
156
157 //
158 /// Maps the given control to the given field.
159 //
160 template <class TControlType, class TFieldType>
162 {
164
165 // Calculate the field offset.
166 //
167 // We cannot use the `offsetof` macro here, since it requires a name for it's second argument,
168 // and we only have the member pointer to the field. We therefore calculate the offset in a
169 // similar way to the common implementation of the `offsetof` macro. Note however that this
170 // implementation has undefined behaviour since we dereference a null-pointer. In addition, it
171 // may fail if the '&' address-operator is overloaded for TFieldType.
172 //
173 TBuffer* const p = 0; // dummy buffer pointer
174 const size_t offset = reinterpret_cast<char*>(&(p->*field)) - reinterpret_cast<char*>(p);
175
176 AssignField(c, offset, sizeof(TFieldType));
177 return c;
178 }
179
180 //
181 /// Creates a new control of the specified type by passing along the given arguments,
182 /// then maps the given control to the given field.
183 //
184 // TODO: Replace by variadic template (C++0x).
185 //
186 template <class TControlType, class TFieldType>
188 {
190 }
191
192 //
193 /// Overload; forwards extra constructor arguments to Create.
194 //
195 template <class TControlType, class TFieldType, class TArg1>
200
201 //
202 /// Overload; forwards extra constructor arguments to Create.
203 //
204 template <class TControlType, class TFieldType, class TArg1, class TArg2>
209
210 //
211 /// Overload; forwards extra constructor arguments to Create.
212 //
213 template <class TControlType, class TFieldType, class TArg1, class TArg2, class TArg3>
218
219 //
220 /// Overload; forwards extra constructor arguments to Create.
221 //
222 template <class TControlType, class TFieldType, class TArg1, class TArg2, class TArg3, class TArg4>
227
228 //
229 /// Overload; forwards extra constructor arguments to Create.
230 //
231 template <class TControlType, class TFieldType, class TArg1, class TArg2, class TArg3, class TArg4, class TArg5>
236
237 //
238 /// Overload; forwards extra constructor arguments to Create.
239 //
240 template <class TControlType, class TFieldType, class TArg1, class TArg2, class TArg3, class TArg4, class TArg5, class TArg6>
245
246 //
247 /// Overload; forwards extra constructor arguments to Create.
248 //
249 template <class TControlType, class TFieldType, class TArg1, class TArg2, class TArg3, class TArg4, class TArg5, class TArg6, class TArg7>
254
255};
256
257//
258/// Helper; returns the type of a window as an tstring.
259/// TODO: Move to a more suitable home where it is more convenient for reuse.
260//
261template <class T>
267
268//
269/// Helper for char buffers; restricts the field type to a char array type of the right size.
270/// Also configures the control with the right field type setting.
271//
272template <class TControlType, class TBuffer, uint N>
274{
275 WARN(c.GetTextLimit() > 0 && c.GetTextLimit() != N,
276 _T("Corrected text limit and field size mismatch for control #")
277 << c.GetId() << _T(" (") << GetFullTypeName(&c) << _T(") ")
278 << _T("in window ") << GetFullTypeName(c.GetParent()) << _T("."));
279
280 WARN(true, _T("Character array fields are deprecated for use with transfer buffers. ")
281 << _T("Use a standard string class instead for control #")
282 << c.GetId() << _T(" (") << GetFullTypeName(&c) << _T(") ")
283 << _T("in window ") << GetFullTypeName(c.GetParent()) << _T("."));
284
285 c.SetTextLimit(N);
286 c.SetTransferBufferFieldType(TControlType::tbftCharArray);
287 return c;
288}
289
290//
291/// Generic binder implementation; restricts the field type for the given control type.
292//
293template <class TControlType, class TFieldType>
295{
296public:
297
298 template <class TBuffer>
299 static void Restrict(TFieldType TBuffer::*, TControlType&)
300 {}
301
302 //
303 // TODO: Replace by variadic template (C++0x).
304 //
305 template <class TBuffer>
306 static TControlType& Create(TFieldType TBuffer::*, TWindow* p, int id)
307 {
308 return *new TControlType(p, id);
309 }
310
311 template <class TBuffer, class TArg1>
312 static TControlType& Create(TFieldType TBuffer::*, TWindow* p, int id,
313 TArg1 a1)
314 {
315 return *new TControlType(p, id, a1);
316 }
317
318 template <class TBuffer, class TArg1, class TArg2>
319 static TControlType& Create(TFieldType TBuffer::*, TWindow* p, int id,
320 TArg1 a1, TArg2 a2)
321 {
322 return *new TControlType(p, id, a1, a2);
323 }
324
325 template <class TBuffer, class TArg1, class TArg2, class TArg3>
326 static TControlType& Create(TFieldType TBuffer::*, TWindow* p, int id,
328 {
329 return *new TControlType(p, id, a1, a2, a3);
330 }
331
332 template <class TBuffer, class TArg1, class TArg2, class TArg3, class TArg4>
333 static TControlType& Create(TFieldType TBuffer::*, TWindow* p, int id,
335 {
336 return *new TControlType(p, id, a1, a2, a3, a4);
337 }
338
339 template <class TBuffer, class TArg1, class TArg2, class TArg3, class TArg4, class TArg5>
340 static TControlType& Create(TFieldType TBuffer::*, TWindow* p, int id,
342 {
343 return *new TControlType(p, id, a1, a2, a3, a4, a5);
344 }
345
346 template <class TBuffer, class TArg1, class TArg2, class TArg3, class TArg4, class TArg5, class TArg6>
347 static TControlType& Create(TFieldType TBuffer::*, TWindow* p, int id,
349 {
350 return *new TControlType(p, id, a1, a2, a3, a4, a5, a6);
351 }
352
353 template <class TBuffer, class TArg1, class TArg2, class TArg3, class TArg4, class TArg5, class TArg6, class TArg7>
354 static TControlType& Create(TFieldType TBuffer::*, TWindow* p, int id,
356 {
357 return *new TControlType(p, id, a1, a2, a3, a4, a5, a6, a7);
358 }
359
360};
361
362//
363/// Binder for TStatic; restricts the possible field types to character array or tstring.
364//
365template <>
367{
368public:
369
370 template <class TBuffer, uint N>
371 static void Restrict(tchar (TBuffer::* field)[N], TStatic& c)
372 {
374 }
375
376 template <class TBuffer>
377 static void Restrict(tstring TBuffer::* field, TStatic& c)
378 {
379 c.SetTransferBufferFieldType(TStatic::tbftString);
380 }
381
382 template <class TBuffer, uint N>
383 static TStatic& Create(tchar (TBuffer::*)[N], TWindow* p, int id,
384 uint textLimit = 0, TModule* m = 0)
385 {
386 return *new TStatic(p, id, textLimit, m);
387 }
388
389 template <class TBuffer>
390 static TStatic& Create(tstring TBuffer::*, TWindow* p, int id,
391 uint textLimit = 0, TModule* m = 0)
392 {
393 return *new TStatic(p, id, textLimit, m);
394 }
395
396};
397
398//
399/// Binder for TEdit; restricts the possible field types to character array or tstring.
400//
401template <>
403{
404public:
405
406 template <class TBuffer, uint N>
407 static void Restrict(tchar (TBuffer::* field)[N], TEdit& c)
408 {
410 }
411
412 template <class TBuffer>
413 static void Restrict(tstring TBuffer::* field, TEdit& c)
414 {
415 c.SetTransferBufferFieldType(TStatic::tbftString);
416 }
417
418 template <class TBuffer, uint N>
419 static TEdit& Create(tchar (TBuffer::*)[N], TWindow* p, int id,
420 uint textLimit = 0, TModule* m = 0)
421 {
422 return *new TEdit(p, id, textLimit, m);
423 }
424
425 template <class TBuffer>
426 static TEdit& Create(tstring TBuffer::*, TWindow* p, int id,
427 uint textLimit = 0, TModule* m = 0)
428 {
429 return *new TEdit(p, id, textLimit, m);
430 }
431
432};
433
434//
435/// Binder for TListBox; restricts the field type to TListBoxData.
436//
437template <>
439 : public TTransferBufferBinderImplementation<TListBox, TListBoxData>
440{};
441
442//
443/// Binder for TComboBox; restricts the field type to TComboBoxData.
444//
445template <>
447 : public TTransferBufferBinderImplementation<TComboBox, TComboBoxData>
448{};
449
450//
451/// Binder for TComboBoxEx; restricts the field type to TComboBoxExData.
452//
453template <>
455 : public TTransferBufferBinderImplementation<TComboBoxEx, TComboBoxExData>
456{};
457
458//
459/// Binder for TCheckBox; restricts the field type to WORD (uint16).
460//
461template <>
463 : public TTransferBufferBinderImplementation<TCheckBox, WORD>
464{};
465
466//
467/// Binder for TRadioButton; restricts the field type to WORD (uint16).
468//
469template <>
471 : public TTransferBufferBinderImplementation<TRadioButton, WORD>
472{};
473
474//
475/// Binder for TScrollBar; restricts the field type to TScrollBarData.
476//
477template <>
479 : public TTransferBufferBinderImplementation<TScrollBar, TScrollBarData>
480{};
481
482//
483/// Binder for TSlider; restricts the field type to TScrollBarData.
484//
485template <>
487 : public TTransferBufferBinderImplementation<TSlider, TScrollBarData>
488{};
489
490//
491/// Binder for TCheckList; restricts the field type to TCheckListData.
492//
493template <>
495 : public TTransferBufferBinderImplementation<TCheckList, TCheckListData>
496{};
497
498//
499/// Binder for TDateTimePicker; restricts the field type to TDateTimePickerData.
500//
501template <>
503 : public TTransferBufferBinderImplementation<TDateTimePicker, TDateTimePickerData>
504{};
505
506//
507/// Binder for THotKey; restricts the field type to uint16.
508//
509template <>
511 : public TTransferBufferBinderImplementation<THotKey, uint16>
512{};
513
514//
515/// Binder for TIPAddress; restricts the field type to TIPAddressBits.
516//
517template <>
519 : public TTransferBufferBinderImplementation<TIPAddress, TIPAddressBits>
520{};
521
522//
523/// Binder for TMemComboBox; restricts the possible field types to character array, tstring or TComboBoxData.
524//
525template <>
527{
528public:
529
530 template <class TBuffer, uint N>
531 static void Restrict(tchar (TBuffer::* field)[N], TMemComboBox& c)
532 {
534 }
535
536 template <class TBuffer>
537 static void Restrict(tstring TBuffer::* field, TMemComboBox& c)
538 {
539 c.SetTransferBufferFieldType(TMemComboBox::tbftString);
540 }
541
542 template <class TBuffer>
543 static void Restrict(TComboBoxData TBuffer::* field, TMemComboBox& c)
544 {
545 c.SetTransferBufferFieldType(TMemComboBox::tbftComboBoxData);
546 }
547
548 template <class TBuffer, uint N>
549 static TMemComboBox& Create(tchar (TBuffer::*)[N], TWindow* p, int id,
550 const tstring& name, uint textLimit = 255, uint itemLimit = 25, TModule* m = 0)
551 {
552 return *new TMemComboBox(p, id, name, textLimit, itemLimit, m);
553 }
554
555 template <class TBuffer>
556 static TMemComboBox& Create(tstring TBuffer::*, TWindow* p, int id,
557 const tstring& name, uint textLimit = 255, uint itemLimit = 25, TModule* m = 0)
558 {
559 return *new TMemComboBox(p, id, name, textLimit, itemLimit, m);
560 }
561
562 template <class TBuffer>
563 static TMemComboBox& Create(TComboBoxData TBuffer::*, TWindow* p, int id,
564 const tstring& name, uint textLimit = 255, uint itemLimit = 25, TModule* m = 0)
565 {
566 return *new TMemComboBox(p, id, name, textLimit, itemLimit, m);
567 }
568
569};
570
571//
572/// Binder for TMonthCalendar; restricts the field type to TMonthCalendarData.
573//
574template <>
576 : public TTransferBufferBinderImplementation<TMonthCalendar, TMonthCalendarData>
577{};
578
579} // OWL namespace
580
581#endif
Definition of class TCheckBox.
Definition of class TCheckList, an ownerdrawn listbox to select multiple items.
#define WARN(condition, message)
Definition checks.h:273
TCheckBox is a streamable interface class that represents a check box control.
Definition checkbox.h:53
TCheckList is an owner-drawn list box to select multiple items.
Definition checklst.h:114
An interface object that represents a transfer buffer for a TComboBox.
Definition combobox.h:182
ComboBoxEx controls are combo box controls that provide native support for item images.
Definition combobex.h:89
You can use TComboBox to create a combo box or a combo box control in a parent TWindow,...
Definition combobox.h:47
A TEdit is an interface object that represents an edit control interface element.
Definition edit.h:34
THotKey encapsulates the hot-key control, a window that allows the user to enter a combination of key...
Definition hotkey.h:33
An interface object that represents a corresponding list box element.
Definition listbox.h:43
ObjectWindows dynamic-link libraries (DLLs) construct an instance of TModule, which acts as an object...
Definition module.h:75
Defines an interface object that represents a corresponding radio button element in Windows.
Definition radiobut.h:42
TScrollBar objects represent standalone vertical and horizontal scroll bar controls.
Definition scrollba.h:41
An abstract base class derived from TScrollBar, TSlider defines the basic behavior of sliders (contro...
Definition slider.h:55
An interface object that represents a static text interface element.
Definition static.h:36
static TEdit & Create(tchar(TBuffer::*)[N], TWindow *p, int id, uint textLimit=0, TModule *m=0)
static TEdit & Create(tstring TBuffer::*, TWindow *p, int id, uint textLimit=0, TModule *m=0)
static void Restrict(tstring TBuffer::*field, TEdit &c)
static void Restrict(tchar(TBuffer::*field)[N], TEdit &c)
static void Restrict(tstring TBuffer::*field, TMemComboBox &c)
static TMemComboBox & Create(TComboBoxData TBuffer::*, TWindow *p, int id, const tstring &name, uint textLimit=255, uint itemLimit=25, TModule *m=0)
static TMemComboBox & Create(tstring TBuffer::*, TWindow *p, int id, const tstring &name, uint textLimit=255, uint itemLimit=25, TModule *m=0)
static TMemComboBox & Create(tchar(TBuffer::*)[N], TWindow *p, int id, const tstring &name, uint textLimit=255, uint itemLimit=25, TModule *m=0)
static void Restrict(tchar(TBuffer::*field)[N], TMemComboBox &c)
static void Restrict(TComboBoxData TBuffer::*field, TMemComboBox &c)
static TStatic & Create(tchar(TBuffer::*)[N], TWindow *p, int id, uint textLimit=0, TModule *m=0)
static void Restrict(tstring TBuffer::*field, TStatic &c)
static void Restrict(tchar(TBuffer::*field)[N], TStatic &c)
static TStatic & Create(tstring TBuffer::*, TWindow *p, int id, uint textLimit=0, TModule *m=0)
Provides a customization hook for binding and creating controls.
Generic binder implementation; restricts the field type for the given control type.
static TControlType & Create(TFieldType TBuffer::*, TWindow *p, int id, TArg1 a1, TArg2 a2, TArg3 a3, TArg4 a4, TArg5 a5, TArg6 a6, TArg7 a7)
static TControlType & Create(TFieldType TBuffer::*, TWindow *p, int id, TArg1 a1, TArg2 a2, TArg3 a3, TArg4 a4, TArg5 a5)
static TControlType & Create(TFieldType TBuffer::*, TWindow *p, int id)
static TControlType & Create(TFieldType TBuffer::*, TWindow *p, int id, TArg1 a1)
static TControlType & Create(TFieldType TBuffer::*, TWindow *p, int id, TArg1 a1, TArg2 a2, TArg3 a3, TArg4 a4, TArg5 a5, TArg6 a6)
static TControlType & Create(TFieldType TBuffer::*, TWindow *p, int id, TArg1 a1, TArg2 a2)
static void Restrict(TFieldType TBuffer::*, TControlType &)
static TControlType & Create(TFieldType TBuffer::*, TWindow *p, int id, TArg1 a1, TArg2 a2, TArg3 a3)
static TControlType & Create(TFieldType TBuffer::*, TWindow *p, int id, TArg1 a1, TArg2 a2, TArg3 a3, TArg4 a4)
Thrown if a more than one control tries to associate with the same field.
Thrown if a validator attempts to meddle with the transfer.
Thrown if a a control tries to associate with more than one field.
Thrown if a transfer is attempted with an unbound control.
Serves as the base class for safe transfer buffer windows.
void AssignField(TWindow &, size_t offset, size_t size)
Stores the field details and enables transfer for the given control.
Provides the type-safe interface needed to set up a safe transfer buffer.
TControlType & Bind(TFieldType TBuffer::*field, int id, TArg1 a1)
Overload; forwards extra constructor arguments to Create.
TControlType & Bind(TFieldType TBuffer::*field, int id, TArg1 a1, TArg2 a2, TArg3 a3)
Overload; forwards extra constructor arguments to Create.
TControlType & Bind(TFieldType TBuffer::*field, TControlType &c)
Maps the given control to the given field.
TBuffer TTransferBuffer
The template argument type.
TControlType & Bind(TFieldType TBuffer::*field, int id, TArg1 a1, TArg2 a2)
Overload; forwards extra constructor arguments to Create.
TControlType & Bind(TFieldType TBuffer::*field, int id, TArg1 a1, TArg2 a2, TArg3 a3, TArg4 a4)
Overload; forwards extra constructor arguments to Create.
TControlType & Bind(TFieldType TBuffer::*field, int id)
Creates a new control of the specified type by passing along the given arguments, then maps the given...
TControlType & Bind(TFieldType TBuffer::*field, int id, TArg1 a1, TArg2 a2, TArg3 a3, TArg4 a4, TArg5 a5, TArg6 a6, TArg7 a7)
Overload; forwards extra constructor arguments to Create.
TControlType & Bind(TFieldType TBuffer::*field, int id, TArg1 a1, TArg2 a2, TArg3 a3, TArg4 a4, TArg5 a5)
Overload; forwards extra constructor arguments to Create.
TControlType & Bind(TFieldType TBuffer::*field, int id, TArg1 a1, TArg2 a2, TArg3 a3, TArg4 a4, TArg5 a5, TArg6 a6)
Overload; forwards extra constructor arguments to Create.
void SetTransferBuffer(TBuffer *b)
Hides the unsafe version in TWindow.
TWindow, derived from TEventHandler and TStreamableBase, provides window-specific behavior and encaps...
Definition window.h:414
void SetTransferBuffer(void *transferBuffer, uint size)
Sets TransferBuffer and TransferBufferSize.
Definition window.h:1971
TXOwl is root class of the ObjectWindows exception hierarchy.
Definition except.h:38
Definition of class TComboBoxEx.
Definition of class TComboBox and TComboBoxData the base class for all combobox controls.
#define _T(x)
Definition cygwin.h:51
Definition of class TDateTimePicker.
Definition of class TEdit.
#define _OBJ_FULLTYPENAME(obj)
Definition objstrm.h:78
TTransferDirection
The TTransferDirection enum describes the constants that the transfer function uses to determine how ...
Definition window.h:92
Definition of class THotKey.
Definition of class TListBox and TlistBoxData.
Class definition for TMemComboBox (TComboBox).
#define _A2W(lpw)
Definition memory.h:220
#define _USES_CONVERSION
Definition memory.h:217
Definition of class TMonthCalendar.
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
tstring GetFullTypeName(T *w)
Helper; returns the type of a window as an tstring.
char tchar
Definition defs.h:77
std::string tstring
Definition defs.h:79
TControlType & RestrictCharArray(tchar(TBuffer::*)[N], TControlType &c)
Helper for char buffers; restricts the field type to a char array type of the right size.
#define _OWLCLASS
Definition defs.h:338
Definition of class TRadioButton.
Definition of class TScrollBar.
Definition of classes TSlider & TVSlider.
Definition of class TStatic, the class for static controls and base for any control that manages simp...
Base window class TWindow definition, including HWND encapsulation.