From: stephan b. <sg...@us...> - 2004-12-27 22:32:55
|
Update of /cvsroot/pclasses/pclasses2/src/Util In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16561/src/Util Added Files: SimplePropertyStore.cpp Log Message: egg: LexT-based very simple property store. Still need to add multimap support. --- NEW FILE: SimplePropertyStore.cpp --- //////////////////////////////////////////////////////////////////////// // SimplePropertyStore - exactly that. // // Author: stephan beal <st...@s1...> // License: Public Domain //////////////////////////////////////////////////////////////////////// #include <stdlib.h> #include <stdio.h> #include <iostream> #include <fstream> #include <map> #include <string> #include <pclasses/Phoenix.h> #include <pclasses/Util/SimplePropertyStore.h> namespace P { namespace Util { using namespace std; typedef SimplePropertyStore::key_type key_type; typedef SimplePropertyStore::mapped_type mapped_type; const key_type SimplePropertyStore::operator[] ( const string & key ) const { return this->get( key ); } SimplePropertyStore::SimplePropertyStore() { return; } SimplePropertyStore::~SimplePropertyStore() { } unsigned long SimplePropertyStore::count() const { return this->m_map.size(); } void SimplePropertyStore::insert( const value_type & v ) { this->set( v.first, v.second ); } mapped_type SimplePropertyStore::get( const key_type & key, const mapped_type & defaultVal ) const { if ( !this->isSet( key ) ) return defaultVal; // DO NOT call LIBE_{DEBUG,VERBOSE} from here! map_type::const_iterator citer = m_map.find( key ); if ( citer != m_map.end() ) return ( *citer ).second; return defaultVal; } void SimplePropertyStore::set( const key_type & key, const mapped_type & val ) { if ( key.empty() ) return; //COUT << "set("<<key<<" =["<<val<<"]" << std::endl; m_map[key] = val; return; } bool SimplePropertyStore::isSet( const key_type & mkey ) const { key_type key = mkey; // experiment to try to work around a segfault. if ( key.empty() ) return false; map_type::const_iterator iter; iter = this->m_map.find( key ); return ( iter != m_map.end() ) ? true : false; } bool SimplePropertyStore::unset( const key_type & key ) { map_type::iterator iter; iter = m_map.find( key ); if ( iter == m_map.end() ) return false; m_map.erase( iter ); return true; } bool SimplePropertyStore::clear() { if ( m_map.empty() ) return false; m_map.erase( m_map.begin(), m_map.end() ); return true; } SimplePropertyStore::iterator SimplePropertyStore::begin() { return this->m_map.begin(); } SimplePropertyStore::const_iterator SimplePropertyStore::begin()const { return this->m_map.begin(); } SimplePropertyStore::iterator SimplePropertyStore::end() { return this->m_map.end(); } SimplePropertyStore::const_iterator SimplePropertyStore::end()const { return this->m_map.end(); } SimplePropertyStore::iterator SimplePropertyStore::find( const key_type & key ) { return m_map.find( key ); } SimplePropertyStore::map_type & SimplePropertyStore::getMap() { return m_map; } const SimplePropertyStore::map_type & SimplePropertyStore::getMap() const { return m_map; } SimplePropertyStore::map_type::size_type SimplePropertyStore::size() const { return this->m_map.size(); } unsigned int SimplePropertyStore::merge( const SimplePropertyStore & src, SimplePropertyStore & dest ) { return dest.merge( src ); } unsigned int SimplePropertyStore::merge( const SimplePropertyStore & src ) { if ( &src == this ) { //CERR << "merge() source and destinaion SimplePropertyStore objects are the same! Ignoring!" << endl; return 0; } if ( src.count() == 0 ) return 0; const_iterator iter = src.begin(); key_type key; key_type val; unsigned int count = 0; while ( iter != src.end() ) { ++count; key = ( *iter ).first; this->set( key, src.get( key, ( *iter ).second ) ); ++iter; } return count; } bool SimplePropertyStore::getBool( const key_type & key, bool defaultVal ) const { if ( !this->isSet( key ) ) return defaultVal; return SimplePropertyStore::boolVal( this->get( key, "???" ) ); } /** phoenix<> initializer functor. */ struct TruesMapInitializer { typedef std::map<std::string,bool> map_type; void operator()( map_type & map ) { map["true"] = true; map["TRUE"] = true; map["True"] = true; map["yes"] = true; map["YES"] = true; map["Yes"] = true; map["y"] = true; map["Y"] = true; map["1"] = true; } }; bool // static SimplePropertyStore::boolVal( const key_type & key ) { typedef std::map < std::string, bool > TrueMap; typedef P::Phoenix<TrueMap,SimplePropertyStore,TruesMapInitializer> PHX; TrueMap & trues = PHX::instance(); return trues.end() != trues.find( key ); } }} |