ResourceBundle.h

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/common/ResourceBundle.h#13 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2004-2009 SQLstream, Inc.
00006 // Copyright (C) 2005-2009 LucidEra, Inc.
00007 //
00008 // This program is free software; you can redistribute it and/or modify it
00009 // under the terms of the GNU General Public License as published by the Free
00010 // Software Foundation; either version 2 of the License, or (at your option)
00011 // any later version approved by The Eigenbase Project.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #ifndef Fennel_ResourceBundle_Included
00024 #define Fennel_ResourceBundle_Included
00025 
00026 #include <functional>
00027 #include <map>
00028 #include <set>
00029 #include <string>
00030 
00031 #include "fennel/common/Locale.h"
00032 #include "fennel/synch/SynchObj.h"
00033 
00034 FENNEL_BEGIN_NAMESPACE
00035 
00036 using namespace std;
00037 
00038 class FENNEL_COMMON_EXPORT ResourceBundle
00039 {
00040 protected:
00041     explicit ResourceBundle(
00042         const string &baseName,
00043         const Locale &locale,
00044         const string &location);
00045     virtual ~ResourceBundle();
00046 
00047     void setParent(ResourceBundle *bundle);
00048 
00049 public:
00050     const set<string> getKeys() const;
00051     const string &getMessage(const string &key) const;
00052     bool hasMessage(const string &key) const;
00053 
00054     const string &getBaseName() const;
00055     const Locale &getLocale() const;
00056 
00057     static void setGlobalResourceFileLocation(const string &location);
00058 
00059     static RecursiveMutex &getMutex();
00060 
00061 private:
00062     void loadMessages();
00063 
00064     const string _baseName;
00065     const Locale _locale;
00066     const string _location;
00067 
00068     ResourceBundle *_parent;
00069 
00070     map<string, string> _messages;
00071 
00072     static RecursiveMutex mutex;
00073 };
00074 
00075 
00076 // Template for instantiating specific resource bundles.
00077 // _GRB is a class generated by MonRG's resgen.
00078 // _BC must be a map<Locale, _GRB *, localeLess>.
00079 // _BC_ITER must be _BC::iterator
00080 
00081 // REVIEW: SWZ 02-Aug-2005: Originally wanted to just use _BC::iterator
00082 // within makeInstance, but that wasn't working.  JVS suggested using
00083 //     typedef typename _BC::iterator _BC_ITER;
00084 // That works, but changing it now means updating the resgen code to
00085 // output compatible calls to makeInstance.
00086 
00087 template<class _GRB, class _BC, class _BC_ITER>
00088 _GRB *makeInstance(
00089     _BC &bundleCache,
00090     const Locale &locale)
00091 {
00092     RecursiveMutexGuard mutexGuard(ResourceBundle::getMutex());
00093 
00094     _BC_ITER iter = bundleCache.find(locale);
00095     if (iter == bundleCache.end()) {
00096         _GRB *bundle = new _GRB(locale);
00097 
00098         if (locale.hasParentLocale()) {
00099             // lookup the parent bundle
00100             _GRB *parentBundle = makeInstance<_GRB, _BC, _BC_ITER>(
00101                 bundleCache,
00102                 locale.getParentLocale());
00103 
00104             bundle->setParent(parentBundle);
00105         } else if (locale != Locale("")) {
00106             // lookup the default bundle
00107             _GRB *defaultBundle = makeInstance<_GRB, _BC, _BC_ITER>(
00108                 bundleCache,
00109                 Locale(""));
00110 
00111             bundle->setParent(defaultBundle);
00112         }
00113 
00114         bundleCache[locale] = bundle;
00115         return bundle;
00116     }
00117 
00118     return (*iter).second;
00119 }
00120 
00121 FENNEL_END_NAMESPACE
00122 
00123 #endif // not Fennel_ResourceBundle_Included
00124 
00125 // End ResourceBundle.h

Generated on Mon Jun 22 04:00:18 2009 for Fennel by  doxygen 1.5.1