include/corelib/env_reg.hpp

Go to the documentation of this file.
00001 #ifndef CORELIB___ENV_REG__HPP
00002 #define CORELIB___ENV_REG__HPP
00003 
00004 /*  $Id: env_reg.hpp 165891 2009-07-15 14:39:29Z ucko $
00005  * ===========================================================================
00006  *
00007  *                            PUBLIC DOMAIN NOTICE
00008  *               National Center for Biotechnology Information
00009  *
00010  *  This software/database is a "United States Government Work" under the
00011  *  terms of the United States Copyright Act.  It was written as part of
00012  *  the author's official duties as a United States Government employee and
00013  *  thus cannot be copyrighted.  This software/database is freely available
00014  *  to the public for use. The National Library of Medicine and the U.S.
00015  *  Government have not placed any restriction on its use or reproduction.
00016  *
00017  *  Although all reasonable efforts have been taken to ensure the accuracy
00018  *  and reliability of the software and data, the NLM and the U.S.
00019  *  Government do not and cannot warrant the performance or results that
00020  *  may be obtained by using this software or data. The NLM and the U.S.
00021  *  Government disclaim all warranties, express or implied, including
00022  *  warranties of performance, merchantability or fitness for any particular
00023  *  purpose.
00024  *
00025  *  Please cite the author in any work or product based on this material.
00026  *
00027  * ===========================================================================
00028  *
00029  * Authors:  Aaron Ucko
00030  *
00031  */
00032 
00033 /// @file env_reg.hpp
00034 /// Classes to support using environment variables as a backend for
00035 /// the registry framework.
00036 
00037 #include <corelib/ncbienv.hpp>
00038 #include <corelib/ncbireg.hpp>
00039 
00040 
00041 /** @addtogroup Registry
00042  *
00043  * @{
00044  */
00045 
00046 
00047 BEGIN_NCBI_SCOPE
00048 
00049 /////////////////////////////////////////////////////////////////////////////
00050 ///
00051 /// IEnvRegMapper --
00052 ///
00053 /// Abstract policy class mediating conversions between environment
00054 /// variable names and registry entry names.
00055 
00056 class  IEnvRegMapper : public CObject
00057 {
00058 public:
00059     /// Returns empty strings for unsupported (section, name) pairs.
00060     virtual string RegToEnv(const string& section, const string& name) const
00061         = 0;
00062 
00063     /// The return value indicates whether the environment variable was
00064     /// appropriately formatted.
00065     virtual bool   EnvToReg(const string& env, string& section, string& name)
00066         const = 0;
00067 
00068     /// Can be overriden to speed enumeration.
00069     virtual string GetPrefix(void) const { return kEmptyStr; }
00070 };
00071 
00072 
00073 /////////////////////////////////////////////////////////////////////////////
00074 ///
00075 /// CEnvironmentRegistry --
00076 ///
00077 /// Adapts CNcbiEnvironment to act like a registry.  Leaves all
00078 /// caching of values to CNcbiEnvironment, and does not support
00079 /// comments.  Always transient, though making it the persistent side
00080 /// of a CTwoLayerRegistry can mask that.
00081 ///
00082 /// Uses customizable mappers between environment variable names and
00083 /// registry section/name pairs; see below for details.
00084 
00085 
00086 class  CEnvironmentRegistry : public IRWRegistry
00087 {
00088 public:
00089     /// Constructors.
00090     CEnvironmentRegistry(TFlags flags = 0);
00091     CEnvironmentRegistry(CNcbiEnvironment& env, EOwnership own = eNoOwnership,
00092                          TFlags flags = 0);
00093 
00094     /// Destructor.
00095     ~CEnvironmentRegistry();
00096 
00097     enum EPriority {
00098         ePriority_Min     = kMin_Int,
00099         ePriority_Default = 0,
00100         ePriority_Max     = kMax_Int
00101     };
00102     typedef int TPriority; ///< Not restricted to ePriority_*.
00103 
00104     void AddMapper(const IEnvRegMapper& mapper,
00105                    TPriority            prio = ePriority_Default);
00106     void RemoveMapper(const IEnvRegMapper& mapper);
00107 
00108 protected:
00109     bool x_Empty(TFlags flags) const;
00110     bool x_Modified(TFlags flags) const;
00111     void x_SetModifiedFlag(bool modified, TFlags flags);
00112     const string& x_Get(const string& section, const string& name,
00113                         TFlags flags) const;
00114     bool x_HasEntry(const string& section, const string& name,
00115                     TFlags flags) const;
00116     const string& x_GetComment(const string& section, const string& name,
00117                                TFlags flags) const;
00118     void x_Enumerate(const string& section, list<string>& entries,
00119                      TFlags flags) const;
00120     void x_ChildLockAction(FLockAction action);
00121 
00122     void x_Clear(TFlags flags);
00123     bool x_Set(const string& section, const string& name,
00124                const string& value, TFlags flags,
00125                const string& comment);
00126     bool x_SetComment(const string& comment, const string& section,
00127                       const string& name, TFlags flags);
00128 
00129 private:
00130     /// Copying prohibited.
00131     CEnvironmentRegistry(const CEnvironmentRegistry&) {}
00132 
00133     typedef multimap<TPriority, CConstRef<IEnvRegMapper> > TPriorityMap;
00134 
00135     AutoPtr<CNcbiEnvironment> m_Env;
00136     TPriorityMap      m_PriorityMap;
00137     bool              m_Modified; ///< only tracks mods made through this.
00138     TFlags            m_Flags;
00139 };
00140 
00141 
00142 /// CSimpleEnvRegMapper --
00143 ///
00144 /// Treat environment variables named <prefix><name><suffix> as
00145 /// registry entries with section <section> and key <section>.  Empty
00146 /// prefixes are legal, but must be specified explicitly.  Each
00147 /// section name is limited to a single prefix/suffix pair; however,
00148 /// there are no obstacles to placing multiple such mappings in a
00149 /// CEnvironmentRegistry.
00150 ///
00151 /// Not used in the default configuration.
00152 
00153 class  CSimpleEnvRegMapper : public IEnvRegMapper
00154 {
00155 public:
00156     CSimpleEnvRegMapper(const string& section, const string& prefix,
00157                         const string& suffix = kEmptyStr);
00158 
00159     string RegToEnv (const string& section, const string& name) const;
00160     bool   EnvToReg (const string& env, string& section, string& name) const;
00161     string GetPrefix(void) const;
00162 private:
00163     string m_Section, m_Prefix, m_Suffix;
00164 };
00165 
00166 
00167 /// CNcbiEnvRegMapper --
00168 ///
00169 /// Somewhat more elaborate mapping used by default, with support for
00170 /// tree conversion.  Special node names (starting with dots) get mapped as
00171 ///     [<section>].<name> <-> NCBI_CONFIG_<name>__<section> ;
00172 /// all other names get mapped as
00173 ///     [<section>]<name> <-> NCBI_CONFIG__<section>__<name> .
00174 
00175 class  CNcbiEnvRegMapper : public IEnvRegMapper
00176 {
00177 public:
00178     string RegToEnv (const string& section, const string& name) const;
00179     bool   EnvToReg (const string& env, string& section, string& name) const;
00180     string GetPrefix(void) const;
00181 
00182 private:
00183     static const char* sm_Prefix;
00184 };
00185 
00186 
00187 END_NCBI_SCOPE
00188 
00189 
00190 /* @} */
00191 
00192 #endif  /* CORELIB___ENV_REG__HPP */
00193 
00194 

Generated on Sun Dec 6 21:58:39 2009 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Mon Dec 07 16:20:34 2009 by modify_doxy.py rev. 173732