WvStreams
unicallbackgen.h
1 /* -*- Mode: C++ -*-
2  * Worldvisions Weaver Software:
3  * Copyright (C) 2002 Net Integration Technologies, Inc.
4  *
5  * A UniConf generator that executes callbacks to generate the value of keys
6  */
7 #ifndef __UNICALLBACKGEN_H
8 #define __UNICALLBACKGEN_H
9 
10 #include <map>
11 
12 #include "unitempgen.h"
13 #include "wvstream.h"
14 #include "wvtr1.h"
15 
16 typedef wv::function<WvString(const UniConfKey&)>
17  UniCallbackGenGetCallback;
18 typedef wv::function<void(const UniConfKey&, WvStringParm)>
19  UniCallbackGenSetCallback;
20 
29 {
30  typedef std::map<UniConfKey, UniCallbackGenGetCallback> GetCallbackMap;
31  GetCallbackMap get_callbacks;
32  typedef std::map<UniConfKey, UniCallbackGenSetCallback> SetCallbackMap;
33  SetCallbackMap set_callbacks;
34 
35 public:
36 
37  bool update_before_get;
38  bool update_after_set;
39 
41  update_before_get(false),
42  update_after_set(true) {}
43  virtual ~UniCallbackGen() {}
44 
45  virtual void setgetcallback(const UniConfKey &key,
46  UniCallbackGenGetCallback get_callback)
47  {
48  if (get_callback)
49  get_callbacks[key] = get_callback;
50  else
51  get_callbacks.erase(key);
52  }
53  virtual void setsetcallback(const UniConfKey &key,
54  UniCallbackGenSetCallback set_callback)
55  {
56  if (set_callback)
57  set_callbacks[key] = set_callback;
58  else
59  set_callbacks.erase(key);
60  }
61 
62  virtual void update(const UniConfKey &key,
63  WvStringParm value = WvString::null)
64  {
65  GetCallbackMap::iterator it = get_callbacks.find(key);
66  if (it != get_callbacks.end())
67  UniTempGen::set(key, it->second(key));
68  else
69  UniTempGen::set(key, value);
70  }
71 
72  /***** Overridden members *****/
73  virtual WvString get(const UniConfKey &key)
74  {
75  if (update_before_get) update(key);
76 
77  return UniTempGen::get(key);
78  }
79  virtual void set(const UniConfKey &key, WvStringParm value)
80  {
81  SetCallbackMap::iterator it = set_callbacks.find(key);
82  if (it != set_callbacks.end())
83  it->second(key, value);
84 
85  if (update_after_set)
86  update(key, value);
87  }
88 };
89 
90 
91 #endif // __UNICALLBACKGEN_H
UniTempGen::get
virtual WvString get(const UniConfKey &key)
Fetches a string value for a key from the registry.
Definition: unitempgen.cc:38
UniCallbackGen::set
virtual void set(const UniConfKey &key, WvStringParm value)
Stores a string value for a key into the registry.
Definition: unicallbackgen.h:79
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
UniCallbackGen::get
virtual WvString get(const UniConfKey &key)
Fetches a string value for a key from the registry.
Definition: unicallbackgen.h:73
UniConfKey
Represents a UniConf key which is a path in a hierarchy structured much like the traditional Unix fil...
Definition: uniconfkey.h:38
UniTempGen::set
virtual void set(const UniConfKey &key, WvStringParm value)
Stores a string value for a key into the registry.
Definition: unitempgen.cc:57
UniCallbackGen
A UniConf generator that executes callbacks to generate the value of keys.
Definition: unicallbackgen.h:28
UniTempGen
A UniConf generator that stores keys in memory.
Definition: unitempgen.h:20