Separatista
automagic.h
1 /***************************************************************************
2 * Copyright (C) 2017 by Okkel Klaver *
3 * info@vanhetland.nl *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 
21 #include <string>
22 
23 #include "separatista/separatista.h"
24 #include "separatista/xerces_types.h"
25 #include "separatista/element.h"
26 #include "separatista/separatistadocument.h"
27 
28 #ifndef SEPARATISTA_AUTOMAGIC_H
29 #define SEPARATISTA_AUTOMAGIC_H
30 
31 namespace Separatista
32 {
33  // Forward decl
34  class AutoMagic;
35 
40  {
41  public:
42  PathWatcher(AutoMagic *pAutoMagic, Element *pElement, const wchar_t *pPath);
43 
44  virtual void elementValueChanged(Element *pElement, const wchar_t *pNewValue);
45  virtual void elementCreated(Element *pParent, Element *pChild);
46  virtual void elementDeleted(Element *pElement);
47  protected:
48  const wchar_t *m_pPath;
49  AutoMagic *m_pAutoMagic;
50  };
51 
55  class AutoMagic : public ElementListener
56  {
57  public:
59  AutoMagic();
60 
62  AutoMagic(Element *pBaseElement);
66  AutoMagic(Element *pBaseElement, const wchar_t *pWatchPath, const wchar_t *pValuePath);
67 
68  virtual void elementValueChanged(Element *pElement, const wchar_t *pNewValue);
69  virtual void elementCreated(Element *pParent, Element *pChild);
70  virtual void elementDeleted(Element *pElement);
71 
73  virtual void doValueMagic(Element *pElement, const wchar_t *pNewValue) = 0;
75  virtual void doCreatedMagic(Element *pParent, Element *pChild) = 0;
77  virtual void doDeletedMagic(Element *pElemenet) = 0;
78 
82  static void installAutoMagic(SeparatistaDocument *pDocument);
83 
84  protected:
89  Element* getValueElement();
90 
91  private:
92  Element *createValueElement(Element *pBaseElement, const wchar_t *pValuePath, const wchar_t *pDefaultValue = TEXT("0"));
93  Element *m_pValueElement;
94  Element *m_pBaseElement;
95  };
96 
97  template <class T>
98  class AutoMagicFactory : public AutoMagic
99  {
100  public:
101  AutoMagicFactory(SeparatistaDocument *pDocument, const wchar_t *pBasePath, const wchar_t *pWatchPath, const wchar_t *pValuePath);
102 
103  static void Create(SeparatistaDocument *pDocument, const wchar_t *pBasePath, const wchar_t *pWatchPath, const wchar_t *pValuePath)
104  {
105  DEBUG_STATIC_METHOD;
106 
107  new AutoMagicFactory<T>(pDocument, pBasePath, pWatchPath, pValuePath);
108  };
109 
110  void doValueMagic(Element *pElement, const wchar_t *pNewValue);
111  void doCreatedMagic(Element *pParent, Element *pChild);
112  void doDeletedMagic(Element *pElemenet);
113 
114  private:
115  const wchar_t *m_pWatchPath;
116  const wchar_t *m_pValuePath;
117  };
118 
119  class CountAutoMagic : public AutoMagic
120  {
121  public:
122  CountAutoMagic(Element *pBaseElement, const wchar_t *pWatchPath, const wchar_t *pValuePath);
123 
124  void doValueMagic(Element *pElement, const wchar_t *pNewValue);
125  void doCreatedMagic(Element *pParent, Element *pChild);
126  void doDeletedMagic(Element *pElemenet);
127  private:
128  size_t m_nCount;
129  };
130 
131  class SumAutoMagic : public AutoMagic
132  {
133  public:
134  SumAutoMagic(Element *pBaseElement, const wchar_t *pWatchPath, const wchar_t *pValuePath);
135 
136  void doValueMagic(Element *pElement, const wchar_t *pNewValue);
137  void doCreatedMagic(Element *pParent, Element *pChild);
138  void doDeletedMagic(Element *pElemenet);
139  protected:
140  void sum();
141 
142  static unsigned int atoi(const wchar_t c);
143  static wchar_t itoa(unsigned int i);
145  static size_t findCh(const wchar_t *pValue, const wchar_t ch);
146 
147  private:
148  std::list<Element*> m_elements;
149  };
150 
151 }
152 
153 #endif // !defined SEPARATISTA_AUTOMAGIC_H
Definition: automagic.h:55
Definition: separatistadocument.h:35
Definition: automagic.h:119
Definition: element.h:46
Definition: automagic.h:39
Definition: mt940s.h:39
Definition: automagic.h:98
Definition: element.h:55
Definition: automagic.h:131