Bumped copyright dates for 2013
[barry.git] / desktop / src / StringSync.h
blob20d249e06a46579065ed4747eaf4ba04de7a1017
1 ///
2 /// \file StringSync.h
3 /// Class to easily manage the wxString / std::string boundary
4 ///
6 /*
7 Copyright (C) 2011-2013, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #ifndef __BARRYDESKTOP_STRING_SYNC_H__
23 #define __BARRYDESKTOP_STRING_SYNC_H__
25 #include <wx/wx.h>
26 #include <list>
29 // StringSync
31 /// Provides a wxString or std::string while maintaining a link to
32 /// a corresponding external string of the opposite kind. When
33 /// Sync() is called, the external linked string is updated with the
34 /// current contents of the returned string.
35 ///
36 /// This class contains two lists of strings: one that maps external
37 /// std::strings to internal wxStrings, and another that maps external
38 /// wxStrings to internal std::strings. These lists never mix.
39 /// Add or Refresh transfers data from external to internal, and Sync
40 /// transfers data from internal to external.
41 ///
42 /// Normally, you would use Add and Refresh when setting up a dialog,
43 /// and once setup, never use them again. Use Sync when extracting
44 /// data from the dialog, back into the original variables again.
45 ///
46 /// Note that you must call Sync explicitly, as it is not called
47 /// in the destructor, since it is impossible to know when the
48 /// external strings are freed, and they may have been freed already
49 /// by the time ~StringSync() is called.
50 ///
51 class StringSync
53 public:
54 typedef std::pair<wxString, std::string*> WxIsCopyType;
55 typedef std::pair<std::string, wxString*> StdIsCopyType;
56 typedef std::list<WxIsCopyType> WxIsCopyList;
57 typedef std::list<StdIsCopyType> StdIsCopyList;
59 private:
60 WxIsCopyList m_wx;
61 StdIsCopyList m_std;
63 public:
64 /// On destruction, calls Sync()
65 ~StringSync();
67 /// Creates an internal wxString, copies the contents of source
68 /// into it, and returns its reference.
69 wxString* Add(std::string &source);
71 /// Does the opposite
72 std::string* Add(wxString &source);
74 /// Copies all internal wxString contents into the corresponding
75 /// external std::strings.
76 void SyncToStd();
78 /// Copies all internal std::string contents into the corresponding
79 /// external wxStrings.
80 void SyncToWx();
82 /// Calls both SyncToStd() and SyncToWx()
83 void Sync();
85 /// Copies the contents of all the external std::strings into
86 /// their internal wxStrings, basically doing an Add() for each one
87 /// without re-creating the wxStrings.
88 void RefreshWx();
90 /// Copies the contents of all the external wxStrings into their
91 /// internal std::strings, basically doing an Add() for each one
92 /// without re-creating the std::strings.
93 void RefreshStd();
95 /// Calls both RefreshStd() and RefreshWx()
96 void Refresh();
99 #endif