PluginConfigCache.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:15k
- /*
- * ===========================================================================
- * PRODUCTION $Log: PluginConfigCache.cpp,v $
- * PRODUCTION Revision 1000.2 2004/06/01 20:42:56 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.3
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: PluginConfigCache.cpp,v 1000.2 2004/06/01 20:42:56 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Author: Robert G. Smith
- *
- * File Description:
- * CPluginConfigCache is a collection of saved (or saveable) configuration values
- * for plugins. It is the parent container class streamed in and out of ASN.1 files
- * to persist these configuration values.
- * The contents of a Cache are CPluginConfigValues, indexed by their type and style
- * names. It is intended that 'type' would refer to the type of the data and 'style'
- * to the saved data values.
- *
- * Remark:
- * This code was originally generated by application DATATOOL
- * using specifications from the data definition file
- * 'plugin.asn'.
- */
- // standard includes
- // generated includes
- #include <ncbi_pch.hpp>
- #include <gui/config/PluginConfigCache.hpp>
- #include <gui/config/PluginConfigValues.hpp>
- #include <algorithm>
- // generated classes
- BEGIN_NCBI_SCOPE
- BEGIN_objects_SCOPE // namespace ncbi::objects::
- //
- // simple functor to check if a type matches a given value
- //
- struct STypeMatches
- {
- STypeMatches(const string& val) : m_Key(val) {}
- bool operator() (const CRef<CPluginConfigValues>& pcv) const
- {
- return pcv->TypeMatches(m_Key);
- }
- private:
- const string& m_Key;
- };
- // destructor
- CPluginConfigCache::~CPluginConfigCache(void)
- {
- }
- // merge two PCC's together, presumably from different files.
- // the other_pcc's values over-write this one's,
- // when the type, style and key are duplicates.
- // Think of it as a data preserving assignment.
- CPluginConfigCache& CPluginConfigCache::Merge(const CPluginConfigCache& other_pcc)
- {
- if (&other_pcc == this)
- return *this;
-
- // for all PCVs from other_pcc
- const TData& other_data = other_pcc.GetData();
- ITERATE(TData, pcv_it, other_data) {
- const CPluginConfigValues& other_pcv = **pcv_it;
- SavePCV(other_pcv); // merge PCV's as units.
- /*
- // If we already have this one, merge it over the old one.
- // else just add it.
- const TData::iterator& pcv_it2 =
- s_FindPCV(other_pcv.GetId().GetType(), other_pcv.GetId().GetStyle(), SetData());
- if (pcv_it2 != SetData().end()) {
- (*pcv_it2)->Assign(other_pcv);
- }
- else {
- SavePCV(other_pcv);
- }
- */
- }
- return *this;
- }
- // accessors
- bool CPluginConfigCache::HasPCV(const string& type, const string& style) const
- {
- if (CanGetData()) {
- const TData& pcv_list = GetData();
- const TData::const_iterator& pcv_it = s_FindPCV(type, style, pcv_list);
- return (pcv_it != pcv_list.end());
- }
- return false;
- }
- const CPluginConfigValues& CPluginConfigCache::GetPCV(const string& type, const string& style) const
- {
- const TData& pcv_list = GetData();
- const TData::const_iterator& pcv_it = s_FindPCV(type, style, pcv_list);
- if (pcv_it == pcv_list.end())
- ThrowUnassigned(0);
- return **pcv_it;
- }
- CPluginConfigCache::TData CPluginConfigCache::GetPCVsByType(const string& aType) const
- {
- TData pcvs_out;
- const TData& pcvs_in = GetData();
-
- TData::const_iterator pcv_it =
- find_if(pcvs_in.begin(), pcvs_in.end(), STypeMatches(aType));
- while (pcv_it != pcvs_in.end()) {
- pcvs_out.push_back(*pcv_it);
- pcv_it = find_if(++pcv_it, pcvs_in.end(), STypeMatches(aType));
- }
- return pcvs_out;
- }
- list<string> CPluginConfigCache::GetStyleNamesByType(const string& aType) const
- {
- list<string> styles;
- const TData& pcvs_in = GetData();
-
- TData::const_iterator pcv_end = pcvs_in.end();
- TData::const_iterator pcv_it =
- find_if(pcvs_in.begin(), pcv_end, STypeMatches(aType));
- while (pcv_it != pcv_end) {
- styles.push_back((*pcv_it)->GetId().GetStyle());
- pcv_it = find_if(++pcv_it, pcv_end, STypeMatches(aType));
- }
- return styles;
- }
- const string&
- CPluginConfigCache::GetStringByKey(const CPluginConfigValues& pcv,
- const string& key,
- const string& delim) const
- {
- try {
- // what kind of PCV is this?
- if (pcv.CanGetByKey()) {
- return pcv.GetStringByKey(key, delim);
- } else if (pcv.CanGetByType()) {
- // Split the key into the part before the first delimiter and the part after.
- string key1, key2;
- NStr::SplitInTwo(key, delim, key1, key2);
- _ASSERT( ! key1.empty()); // no empty key parts.
-
- if ( ! key2.empty()) {
- // look up style name with first part of the key as the type
- const string& style = pcv.GetStyleByType(key1);
- // using that 'included' PCV look up our value with the rest of the key.
- // Note: no infinite recursion here since the key keeps getting shorter.
- return GetStringByKey(key1, style, key2, delim);
- }
- // else only single part key, but this PCV contains references
- // to other PCVs not to values.
- // Drop through and throw.
- }
- // else data not set. Drop through and throw.
- } catch (CSerialException&) {
- // our key wasn't found. Look in another set of values with this type
- // HOW do we check for infinite loops/recursion ???
- if (pcv.CanGetRefer_to_style()) {
- return GetStringByKey(pcv.GetId().GetType(), pcv.GetRefer_to_style(), key, delim);
- }
- // still not found. Throw.
- }
- pcv.ThrowUnassigned(2);
- static const string s;
- return s; // NOT REACHED. Stops compiler warnings.
- }
- CPluginConfigValues& // return the key/value PCV we actually store stuff in.
- CPluginConfigCache::AddKeyString( // this will be pcv if typekey is empty.
- CPluginConfigValues& pcv, // the PCV we will start adding stuff to.
- const string& typekey, // type names for 'include' PCV(s)
- const string& key, // data key.
- const string& value, // data to store.
- const string& delim // delimiter for both typekey and key.
- )
- {
- if (typekey.empty()) { // PCV is just data not references to other PCVs.
- // _ASSERT(pcv.CanAddKeys()); // if not we just wipe out the 'include' there already.
- pcv.AddKeyString(key, value, delim);
- return pcv;
- }
- // Split the key into the part before the first delimiter and the part after.
- string typekey1, typekey2;
- NStr::SplitInTwo(typekey, delim, typekey1, typekey2);
- _ASSERT( ! typekey1.empty()); // no empty key parts.
- string style;
- try {
- style = pcv.GetStyleByType(typekey1);
- } catch (const CSerialException&) {
- style = "default";
- }
- // _ASSERT(pcv.CanAddPCId()); // if not we just wipe out the keyvals there already.
- // Add the reference in this PCV
- pcv.AddPCId(typekey1, style);
- // Find or Add the PCV we are referring to and continue there.
- // Recursive but typekey keeps getting shorter.
- return AddKeyString(typekey1, style, typekey2, key, value, delim);
- }
- bool CPluginConfigCache::DelKeyvalue(
- CPluginConfigValues& pcv,
- const string& key,
- const string& delim
- )
- {
- // what kind of PCV is this?
- if ( pcv.CanGetByKey() && pcv.DelKeyval(key, delim) ) {
- return true;
- } else if (pcv.CanGetByType()) {
- // Split the key into the part before the first delimiter and the part after.
- string key1, key2;
- NStr::SplitInTwo(key, delim, key1, key2);
- _ASSERT( ! key1.empty()); // no empty key parts.
-
- if ( ! key2.empty()) {
- // look up style name with first part of the key as the type
- const string& style = pcv.GetStyleByType(key1);
- // using that 'included' PCV look up our value with the rest of the key.
- // Note: no infinite recursion here since the key keeps getting shorter.
- return DelKeyvalue(key1, style, key2, delim);
- }
- // else only single part key, but this PCV contains references
- // to other PCVs not to values.
- }
- // our key wasn't found. Look in another set of values with this type
- // HOW do we check for infinite loops/recursion ???
- if (pcv.CanGetRefer_to_style()) {
- return DelKeyvalue(pcv.GetId().GetType(), pcv.GetRefer_to_style(), key, delim);
- }
- // still not found.
- return false;
- }
- // Set of values mutators
- CPluginConfigValues& CPluginConfigCache::SetPCV(const string& type, const string& style)
- {
- TData& pcv_list = SetData();
- const TData::iterator& pcv_it = s_FindPCV(type, style, pcv_list);
- if (pcv_it == pcv_list.end()) {
- CRef<CPluginConfigValues> pcv_new(new CPluginConfigValues(type, style));
- pcv_list.push_back(pcv_new);
- return *pcv_new;
- }
- return **pcv_it;
- }
- // duplicate the argument and store it in the Cache,
- // if there is no other with this type and style.
- // else replace that one with this one.
- CPluginConfigValues& CPluginConfigCache::SavePCV(const CPluginConfigValues& pcv)
- {
- TData& pcv_list = SetData();
- const string& type = pcv.GetId().GetType();
- const string& style = pcv.GetId().GetStyle();
- TData::iterator pcv_it = s_FindPCV(type, style, pcv_list);
- if (pcv_it == pcv_list.end()) {
- CRef<CPluginConfigValues> pcv_new(new CPluginConfigValues());
- pcv_new->Assign(pcv);
- pcv_list.push_back(pcv_new);
- return *pcv_new;
- }
- (*pcv_it)->Assign(pcv);
- return **pcv_it;
- }
- // duplicate a PCV giving the new copy a new style name.
- CPluginConfigValues& CPluginConfigCache::DupPCV(const string& type, const string& oldstyle, const string& newstyle)
- {
- if (oldstyle == newstyle)
- return SetPCV(type, oldstyle);
-
- const CPluginConfigValues& pcv_old = GetPCV(type, oldstyle);
-
- TData& pcv_list = SetData();
- TData::iterator pcv_it = s_FindPCV(type, newstyle, pcv_list);
- if (pcv_it == pcv_list.end()) {
- CRef<CPluginConfigValues> pcv_new(new CPluginConfigValues());
- pcv_new->Assign(pcv_old);
- pcv_new->SetId().SetStyle(newstyle);
- pcv_list.push_back(pcv_new);
- return *pcv_new;
- }
- (*pcv_it)->Assign(pcv_old);
- (*pcv_it)->SetId().SetStyle(newstyle);
- return **pcv_it;
- }
- void CPluginConfigCache::DeletePCV(const string& type, const string& style)
- {
- TData& pcv_list = SetData();
- TData::iterator pcv_it = s_FindPCV(type, style, pcv_list);
- if (pcv_it != pcv_list.end()) {
- pcv_list.erase(pcv_it);
- }
- return;
- }
- // comparison functor
- class TypeStyleMatches {
- public:
- TypeStyleMatches(const string& i, const string& s)
- : type(i), style(s) {}
-
- bool operator()(const CRef<CPluginConfigValues>& pcv) {
- return pcv->TypeMatches(type) && pcv->StyleMatches(style);
- }
- private:
- const string& type;
- const string& style;
- };
-
- CPluginConfigCache::TData::const_iterator
- CPluginConfigCache::s_FindPCV(const string& type, const string& style, const CPluginConfigCache::TData& pcvs)
- {
- _ASSERT (!type.empty());
- _ASSERT (!style.empty());
- return find_if(pcvs.begin(), pcvs.end(), TypeStyleMatches(type, style));
- }
- CPluginConfigCache::TData::iterator
- CPluginConfigCache::s_FindPCV(const string& type, const string& style, CPluginConfigCache::TData& pcvs)
- {
- _ASSERT (!type.empty());
- _ASSERT (!style.empty());
- return find_if(pcvs.begin(), pcvs.end(), TypeStyleMatches(type, style));
- }
- string CPluginConfigCache::MakeUniqueStyle(const string& type, const string& style) const
- {
- string new_style(style);
- unsigned long suffix_number = 0;
-
- while (HasPCV(type, new_style)) {
- new_style = style + " " + NStr::UIntToString(++suffix_number);
- }
-
- return new_style;
- }
- END_objects_SCOPE // namespace ncbi::objects::
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- *
- * $Log: PluginConfigCache.cpp,v $
- * Revision 1000.2 2004/06/01 20:42:56 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.3
- *
- * Revision 1.3 2004/05/21 22:27:39 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.2 2003/11/21 12:49:12 rsmith
- * Add ability to delete values by key.
- *
- * Revision 1.1 2003/10/10 17:43:04 rsmith
- * moved from gui/plugin to gui/config
- *
- * Revision 1.10 2003/08/26 19:35:07 rsmith
- * add methods to get a list of style names for a particular type
- * and to return an unused style name, give a type and style.
- *
- * Revision 1.9 2003/08/19 18:36:08 rsmith
- * use SplitInTwo instead of duplicating code.
- *
- * Revision 1.8 2003/08/19 18:16:53 rsmith
- * delete ExistsPCV, redundant with HasPCV.
- * new method AddKeyString.
- * Merge merges PCVs as units. Does not recursively merge their values.
- *
- * Revision 1.7 2003/08/13 21:40:13 ucko
- * CPluginConfigCache::Merge: avoid shadowing pcv_it
- *
- * Revision 1.6 2003/08/13 20:36:47 rsmith
- * get rid of include of find_if.hpp
- *
- * Revision 1.5 2003/08/13 20:21:44 rsmith
- * Add Merge method and get rid of FindIf
- *
- * Revision 1.4 2003/08/06 13:10:31 dicuccio
- * Replaced std::find_if() with FindIf() - work-around for MSVC's broken STL
- * implementation (no const_mem_fun1_t<>)
- *
- * Revision 1.3 2003/08/05 19:23:49 meric
- * Modified GetStringByKey() return value to avoid compiler warnings
- *
- * Revision 1.2 2003/08/05 19:01:36 rsmith
- * change members used in mem_fun to satisfy certain compilers.
- *
- * Revision 1.1 2003/08/05 17:35:59 rsmith
- * Classes to allow saving of plugin's configuration values as ASN.1.
- *
- *
- * ===========================================================================
- */
- /* Original file checksum: lines: 64, chars: 1901, CRC32: 6f975f95 */