namelist.h

Go to the documentation of this file.
00001 // OSB library ************************************************ -*- C++ -*-
00006 /*
00007   AUTHOR(S): Stephan Broennimann (vb)
00008 
00009   RCS information
00010    $Name: OSB_060808 $
00011    $Revision: 1.9 $
00012 
00013   License
00014    OSB rating and billing library for communication networks
00015    Copyright (C) 2004, 2005, 2006  OSB systems
00016 
00017    This file may be distributed and/or modify under the terms of the
00018    GNU General Public License (GPL) as published by the Free Software
00019    Foundation which is provided in the file LICENSE.GPL included in the
00020    packaging of this file.
00021 
00022    The file is distributed in the hope that it will be useful, but WITHOUT
00023    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00024    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00025    for more details.
00026 
00027    Holders of a OSB Commercial License may use this file under the terms
00028    and conditions of this commercial license.
00029  */
00030 #ifndef _NAMELIST_H_
00031 #define _NAMELIST_H_
00032 
00033 // ************************************************************************
00034 // included header files
00035 #include "namelistfwd.h"
00036 
00037 // + standard includes
00038 #include <string>
00039 #include <map>
00040 
00041 // + libraries
00042 
00043 // + local headers
00044 
00045 // + class declarations
00046 namespace OSB_DB {
00047     class Session;
00048 }
00049 
00050 // ************************************************************************
00051 // namespace extensions
00052 namespace OSB_LIB {
00053 
00054 // ************************************************************************
00055 // forward declarations
00056 
00057 // ************************************************************************
00058 // type definitions
00059 
00060 // ************************************************************************
00061 // class definitions
00105     template<typename Key, typename Comp>
00106     class NameList {
00107     public:
00109         typedef Key KeyType;
00110 
00112         struct Entry {
00114             explicit Entry(
00115                 const std::string& name = "",
00116                 const std::string& des  = ""
00117             );
00119             std::string name_;
00121             std::string des_;
00122         };                              // struct Entry
00123 
00125         typedef std::map<KeyType, Entry, Comp> Cont;
00126 
00127     public:
00129         NameList();
00131         NameList(const NameList& rhs);
00137         NameList& operator=(const NameList& rhs);
00139         ~NameList();
00140 
00142 
00143 
00150         const Entry& get(const Key& key) const;
00152         const std::string& name(const Key& key) const;
00154         const std::string& des(const Key& key) const;
00156         const Cont& entries() const;
00158 
00160 
00161 
00162         void clear();
00168         Entry& operator[](const Key& key);
00175         void setNullEntry(
00176             const Entry& nullEntry
00177         );
00179 
00180     private:
00186         const Entry* nullEntry() const;
00187 
00188     private:
00190         Cont entries_;
00192         mutable Entry* nullEntry_;
00193     };                                  // class NameList
00194 
00198     struct NameListReader {
00199     public:
00201         struct Row {
00203             Row();
00205             void reset();
00206 
00208             long id1_;
00214             long id2_;
00216             std::string name_;
00222             std::string des_;
00223         };                              // struct Row
00224     public:
00226 
00227 
00228         std::string table_;
00230         std::string colId1_;
00236         std::string colId2_;
00238         std::string colName_;
00244         std::string colDes_;
00246 
00254         void read(
00255             const OSB_DB::Session& session
00256         );
00257 
00261         virtual void addRow(
00262             const Row& row
00263         ) = 0;
00264 
00265     public:
00267         NameListReader();
00269         virtual ~NameListReader();
00270 
00271     protected:
00273         NameListReader(const NameListReader& rhs);
00275         NameListReader& operator=(const NameListReader& rhs);
00276     };                                  // struct NameListReader
00277 
00283     template<typename NameList>
00284     struct Id1InfoReader : public NameListReader {
00289         Id1InfoReader(
00290             const std::string& table,
00291             const std::string& colId1,
00292             const std::string& colName = "NAME",
00293             const std::string& colDes  = "DES"
00294         );
00295 
00302         void read(
00303             const OSB_DB::Session& session,
00304                   NameList&        dest
00305         );
00306 
00314         void addRow(const Row& row);
00315     private:
00317         NameList* namelist_;
00318     };                                  // struct Id1InfoReader
00319 
00325     template<typename NameList>
00326     struct Id2InfoReader : public NameListReader {
00331         Id2InfoReader(
00332             const std::string& table,
00333             const std::string& colId1,
00334             const std::string& colId2,
00335             const std::string& colName = "NAME",
00336             const std::string& colDes  = "DES"
00337         );
00338 
00345         void read(
00346             const OSB_DB::Session& session,
00347                   NameList&        dest
00348         );
00349 
00357         void addRow(const Row& row);
00358     private:
00360         NameList* namelist_;
00361     };                                  // struct Id2InfoReader
00362 }                                       // namespace OSB_LIB
00363 
00364 // ************************************************************************
00365 // inline definitions
00366 namespace OSB_LIB {
00367     // ********************************************************************
00368     // struct NameList:Entry
00369     template<typename Key, typename Comp>
00370     NameList<Key, Comp>::Entry::Entry(
00371         const std::string& name,
00372         const std::string& des
00373     ) : name_(name), des_(des)
00374     {
00375         // empty
00376     }
00377 
00378     // ********************************************************************
00379     // class NameList {
00380     template<typename Key, typename Comp>
00381     NameList<Key, Comp>::NameList()
00382       : nullEntry_(0)
00383     {
00384         // empty
00385     }
00386 
00388     template<typename Key, typename Comp>
00389     NameList<Key, Comp>::NameList(const NameList& rhs)
00390       : nullEntry_(0)
00391     {
00392         if (0 != rhs.nullEntry_) {
00393             setNullEntry(*rhs.nullEntry_);
00394         }
00395     }
00396 
00397     template<typename Key, typename Comp>
00398     NameList<Key, Comp>& NameList<Key, Comp>::operator=(
00399         const NameList& rhs
00400     )
00401     {
00402         if (this == &rhs) return;
00403 
00404         delete nullEntry_;
00405         nullEntry_ = 0;
00406 
00407         if (0 != rhs.nullEntry_) {
00408             setNullEntry(*rhs.nullEntry_);
00409         }
00410     }
00411 
00412     template<typename Key, typename Comp>
00413     NameList<Key, Comp>::~NameList()
00414     {
00415         delete nullEntry_;
00416     }
00417 
00418     template<typename Key, typename Comp>
00419     const typename NameList<Key, Comp>::Cont&
00420     NameList<Key, Comp>::entries() const
00421     {
00422         return entries_;
00423     }
00424 
00425     template<typename Key, typename Comp>
00426     void NameList<Key, Comp>::clear()
00427     {
00428         entries_.clear();
00429         delete nullEntry_;
00430         nullEntry_ = 0;
00431     }
00432 
00433     template<typename Key, typename Comp>
00434     inline typename NameList<Key, Comp>::Entry&
00435     NameList<Key, Comp>::operator[](const Key& key)
00436     {
00437         return entries_[key];
00438     }
00439 
00440     template<typename Key, typename Comp>
00441     void NameList<Key, Comp>::setNullEntry(
00442         const Entry& nullEntry
00443     )
00444     {
00445         delete nullEntry_;
00446         nullEntry_ = new Entry(nullEntry);
00447     }
00448 
00449     template<typename Key, typename Comp>
00450     inline const typename NameList<Key, Comp>::Entry&
00451     NameList<Key, Comp>::get(const Key& key) const
00452     {
00453         const Entry* rc = 0;
00454         typename Cont::const_iterator e = entries_.find(key);
00455         if (entries_.end() != e) rc = &(e->second);
00456         else                     rc = nullEntry();
00457         return *rc;
00458     }
00459 
00460     template<typename Key, typename Comp>
00461     inline const std::string&
00462     NameList<Key, Comp>::name(const Key& key) const
00463     {
00464         return get(key).name_;
00465     }
00466 
00468     template<typename Key, typename Comp>
00469     inline const std::string&
00470     NameList<Key, Comp>::des(const Key& key) const
00471     {
00472         return get(key).des_;
00473     }
00474 
00475     template<typename Key, typename Comp>
00476     const typename NameList<Key, Comp>::Entry*
00477     NameList<Key, Comp>::nullEntry() const
00478     {
00479         if (0 == nullEntry_) {
00480             nullEntry_ = new Entry();
00481         }
00482         return nullEntry_;
00483     }
00484 
00485     // ********************************************************************
00486     // struct Id1InfoReader
00487     template<typename NameList>
00488     Id1InfoReader<NameList>::Id1InfoReader(
00489         const std::string& table,
00490         const std::string& colId1,
00491         const std::string& colName,
00492         const std::string& colDes
00493     ) : namelist_(0)
00494     {
00495         table_   = table;
00496         colId1_  = colId1;
00497         colName_ = colName;
00498         colDes_  = colDes;
00499     }
00500 
00501     template<typename NameList>
00502     void Id1InfoReader<NameList>::read(
00503         const OSB_DB::Session& session,
00504               NameList&        dest
00505     )
00506     {
00507         namelist_ = &dest;
00508         NameListReader::read(session);
00509         namelist_ = 0;
00510     }
00511 
00512     template<typename NameList>
00513     void Id1InfoReader<NameList>::addRow(const Row& row)
00514     {
00515         typedef typename NameList::KeyType Key;
00516         typedef typename NameList::Entry   Entry;
00517 
00518         if (0 == namelist_) return;
00519 
00520         Key key(row.id1_);
00521         (*namelist_)[key] = Entry(row.name_, row.des_);
00522     }
00523 
00524     // ********************************************************************
00525     // struct Id2InfoReader
00526     template<typename NameList>
00527     Id2InfoReader<NameList>::Id2InfoReader(
00528         const std::string& table,
00529         const std::string& colId1,
00530         const std::string& colId2,
00531         const std::string& colName,
00532         const std::string& colDes
00533     ) : namelist_(0)
00534     {
00535         table_   = table;
00536         colId1_  = colId1;
00537         colId2_  = colId2;
00538         colName_ = colName;
00539         colDes_  = colDes;
00540     }
00541 
00542     template<typename NameList>
00543     void Id2InfoReader<NameList>::read(
00544         const OSB_DB::Session& session,
00545               NameList&        dest
00546     )
00547     {
00548         namelist_ = &dest;
00549         NameListReader::read(session);
00550         namelist_ = 0;
00551     }
00552 
00553     template<typename NameList>
00554     void Id2InfoReader<NameList>::addRow(const Row& row)
00555     {
00556         typedef typename NameList::KeyType Key;
00557         typedef typename NameList::Entry   Entry;
00558 
00559         if (0 == namelist_) return;
00560 
00561         Key key(row.id1_, row.id2_);
00562         (*namelist_)[key] = Entry(row.name_, row.des_);
00563     }
00564 }                                       // namespace OSB_LIB
00565 #endif                                  // #ifndef _NAMELIST_H_

Generated on Sat Sep 2 14:06:33 2006 for OSB Library by  doxygen 1.4.7