1 // XrmDatabaseHelper.hh
2 // Copyright (c) 2002 Henrik Kinnunen (fluxgen at fluxbox dot org)
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21 
22 #ifndef FBTK_XRMDATABASEHELPER_HH
23 #define FBTK_XRMDATABASEHELPER_HH
24 
25 #include <X11/Xresource.h>
26 
27 /**
28   This is a helper for XrmDatabase
29   when database goes out of scope
30   the XrmDatabase variable will be destroyed.
31 */
32 namespace FbTk {
33 
34 class XrmDatabaseHelper
35 {
36 public:
XrmDatabaseHelper(char const * filename=0)37     XrmDatabaseHelper(char const * filename=0)
38         : m_database(0)
39     { if (filename != 0) load(filename); }
40 
~XrmDatabaseHelper()41     ~XrmDatabaseHelper() {
42         close();
43     }
44 
45     /// assignment operator
operator =(const XrmDatabase & database)46     XrmDatabaseHelper& operator=(const XrmDatabase& database) {
47         if (database == m_database) // assignment to self
48             return *this;
49 
50         if (m_database!=0)
51             close();
52         m_database = database;
53         return *this;
54     }
load(const char * filename)55     bool load(const char *filename) {
56         if (filename == 0)
57             return false;
58         XrmDatabase db = XrmGetFileDatabase(filename);
59         if (db == 0)
60             return false;
61         close(); // close old database
62         m_database = db; // set new and return true
63         return true;
64     }
close()65     void close() {
66         if (m_database != 0) {
67             XrmDestroyDatabase(m_database);
68             m_database = 0;
69         }
70     }
71 
operator ==(const XrmDatabase & database) const72     bool operator == (const XrmDatabase& database) const { return m_database == database; }
operator *()73     XrmDatabase & operator*() { return m_database; }
74 
75 private:
76     XrmDatabase m_database;
77 };
78 
79 } // namespace FbTk
80 
81 #endif // FBTK_XRMDATABASEHELPER_HH
82