Add macro for validator cache implementation

Most of the validators use a cache. This cache looks the same in
all of the validator classes. In preparation of reduce these code
duplications a macro gets added to the validator base class which
can be used by the derived validator classes.

Change-Id: I5fe8a897f9b36e2a620e657bdd37ae0ea8b62c4c

git-svn-id: https://svn.apache.org/repos/asf/etch/trunk@1679581 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/binding-cpp/runtime/include/serialization/EtchValidator.h b/binding-cpp/runtime/include/serialization/EtchValidator.h
index 0552fa5..f9a6016 100644
--- a/binding-cpp/runtime/include/serialization/EtchValidator.h
+++ b/binding-cpp/runtime/include/serialization/EtchValidator.h
@@ -19,6 +19,31 @@
 #ifndef __ETCHVALIDATOR_H__
 #define __ETCHVALIDATOR_H__
 
+    #define VALIDATOR_CACHE_DEF()                                                               \
+    static capu::SmartPointer<EtchValidator>* Validators(EtchRuntime* runtime);
+
+#define VALIDATOR_CACHE_IMPL(validatorClassName)                                                \
+    static EtchValidatorCaches SValidatorCache##validatorClassName;                             \
+                                                                                                \
+class EtchValidatorStringRuntimeListener : public EtchRuntimeListener {                         \
+    status_t onRuntimeChanged(EtchRuntime* runtime) {                                           \
+    if (runtime == NULL) {                                                                      \
+        return ETCH_ERROR;                                                                      \
+    }                                                                                           \
+    if (runtime->isClosed()) {                                                                  \
+        SValidatorCache##validatorClassName.clearCache();                                       \
+        runtime->unregisterListener(this);                                                      \
+    }                                                                                           \
+    return ETCH_OK;                                                                             \
+    }                                                                                           \
+};                                                                                              \
+                                                                                                \
+    static EtchValidatorStringRuntimeListener SRuntimeChangedListener;                          \
+                                                                                                \
+    capu::SmartPointer<EtchValidator>* validatorClassName::Validators(EtchRuntime* runtime) { \
+    return SValidatorCache##validatorClassName.get(runtime);                                    \
+}
+
 #include "common/EtchObject.h"
 #include "support/EtchRuntime.h"
 
@@ -102,19 +127,23 @@
    * Destructor
    */
   virtual ~EtchValidatorCaches() {
-    capu::List<ValidatorCache*>::Iterator iter = mValidatorsCache.begin();
-    while(iter != mValidatorsCache.end()) {
-      ValidatorCache* entry = *iter;
-      iter++;
-      delete entry;
-    }
-    mValidatorsCache.clear();
+      clearCache();
   }
 
   status_t onRuntimeChanged(EtchRuntime* runtime) {
     return ETCH_OK;
   }
 
+  void clearCache() {
+      capu::List<ValidatorCache*>::Iterator iter = mValidatorsCache.begin();
+      while(iter != mValidatorsCache.end()) {
+          ValidatorCache* entry = *iter;
+          iter++;
+          delete entry;
+      }
+      mValidatorsCache.clear();
+  }
+
   capu::SmartPointer<EtchValidator>* get(EtchRuntime* runtime) {
     capu::List<ValidatorCache*>::Iterator iter = mValidatorsCache.begin();
     while(iter != mValidatorsCache.end()) {