WvStreams
uniunwrapgen.cc
00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 2002 Net Integration Technologies, Inc.
00004  * 
00005  * A totally evil UniConfGen that "unwraps" a UniConf object by turning it
00006  * back into a UniConfGen.  See uniunwrapgen.h.
00007  */
00008 #include "uniconfroot.h"
00009 #include "uniunwrapgen.h"
00010 #include "wvlinkerhack.h"
00011 
00012 WV_LINK(UniUnwrapGen);
00013 
00014 
00015 UniUnwrapGen::UniUnwrapGen(const UniConf &inner)
00016 {
00017     refreshing = committing = false;
00018     setinner(inner);
00019 }
00020 
00021 
00022 UniUnwrapGen::~UniUnwrapGen()
00023 {
00024     UniConfRoot *root = xinner.rootobj();
00025     if (root)
00026         root->mounts.del_callback(this);
00027 }
00028 
00029 
00030 void UniUnwrapGen::setinner(const UniConf &inner)
00031 {
00032     UniConfRoot *root = xinner.rootobj();
00033     if (root)
00034         root->mounts.del_callback(this);
00035 
00036     xinner = inner;
00037     xfullkey = xinner.fullkey();
00038 
00039     root = xinner.rootobj();
00040     if (root)
00041         root->mounts.add_callback(this, wv::bind(&UniUnwrapGen::gencallback,
00042                                                  this, _1, _2));
00043 }
00044 
00045 
00046 UniConf UniUnwrapGen::_sub(const UniConfKey &key)
00047 {
00048     if (key.isempty())
00049         return xinner;
00050     else
00051         return xinner[key];
00052 }
00053 
00054 
00055 void UniUnwrapGen::commit()
00056 {
00057     if (!committing)
00058     {
00059         committing = true;
00060         xinner.commit();
00061         committing = false;
00062     }
00063 }
00064 
00065 
00066 bool UniUnwrapGen::refresh()
00067 {
00068     if (!refreshing)
00069     {
00070         refreshing = true;
00071         bool ret = xinner.refresh();
00072         refreshing = false;
00073         return ret;
00074     }
00075     return true;
00076 }
00077 
00078 
00079 void UniUnwrapGen::prefetch(const UniConfKey &key, bool recursive)
00080 {
00081     _sub(key).prefetch(recursive);
00082 }
00083 
00084 
00085 WvString UniUnwrapGen::get(const UniConfKey &key)
00086 {
00087     return _sub(key).getme();
00088 }
00089 
00090 
00091 void UniUnwrapGen::set(const UniConfKey &key, WvStringParm value)
00092 {
00093     _sub(key).setme(value);
00094 }
00095 
00096 
00097 void UniUnwrapGen::setv(const UniConfPairList &pairs)
00098 {
00099     // Extremely evil.  This pokes directly into UniMountGen, because we
00100     // don't want to expose setv to users.
00101     xinner.rootobj()->mounts.setv(pairs);
00102 }
00103 
00104 
00105 bool UniUnwrapGen::exists(const UniConfKey &key)
00106 {
00107     return _sub(key).exists();
00108 }
00109 
00110 
00111 bool UniUnwrapGen::haschildren(const UniConfKey &key)
00112 {
00113     return _sub(key).haschildren();
00114 }
00115 
00116 
00117 bool UniUnwrapGen::isok()
00118 {
00119     IUniConfGen *gen = xinner.whichmount();
00120     return gen ? gen->isok() : false;
00121 }
00122 
00123 
00124 class UniUnwrapGen::Iter : public UniConfGen::Iter
00125 {
00126     UniConf::Iter i;
00127     
00128 public:
00129     Iter(const UniConf &cfg)
00130         : i(cfg)
00131         { }
00132     virtual ~Iter()
00133         { }
00134     
00135     /***** Overridden members *****/
00136     virtual void rewind() { i.rewind(); }
00137     virtual bool next() { return i.next(); }
00138     virtual UniConfKey key() const { return i->key(); }
00139     virtual WvString value() const { return i->getme(); }
00140 };
00141 
00142 
00143 class UniUnwrapGen::RecursiveIter : public UniConfGen::Iter
00144 {
00145     UniConf::RecursiveIter i;
00146     
00147 public:
00148     RecursiveIter(const UniConf &cfg)
00149         : i(cfg)
00150         { }
00151     virtual ~RecursiveIter()
00152         { }
00153     
00154     /***** Overridden members *****/
00155     virtual void rewind() { i.rewind(); }
00156     virtual bool next() { return i.next(); }
00157     virtual UniConfKey key() const { return i->key(); }
00158     virtual WvString value() const { return i->getme(); }
00159 };
00160 
00161 
00162 UniConfGen::Iter *UniUnwrapGen::iterator(const UniConfKey &key)
00163 {
00164     return new Iter(_sub(key));
00165 }
00166 
00167 
00168 UniConfGen::Iter *UniUnwrapGen::recursiveiterator(const UniConfKey &key)
00169 {
00170     return new RecursiveIter(_sub(key));
00171 }
00172 
00173 
00174 void UniUnwrapGen::gencallback(const UniConfKey &key, WvStringParm value)
00175 {
00176     UniConfKey subkey;
00177     if (xfullkey.suborsame(key, subkey))
00178         delta(subkey, value);
00179 }