first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / configuration / ini_roller.h
1 #ifndef INI_ROLLER_CLASS
2 #define INI_ROLLER_CLASS
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : ini_roller                                                        *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
9 *******************************************************************************
10 * Copyright (c) 2001-$now By Author.  This program is free software; you can  *
11 * redistribute it and/or modify it under the terms of the GNU General Public  *
12 * License as published by the Free Software Foundation; either version 2 of   *
13 * the License or (at your option) any later version.  This is online at:      *
14 *     http://www.fsf.org/copyleft/gpl.html                                    *
15 * Please send any updates to: fred@gruntose.com                               *
16 \*****************************************************************************/
17
18 #include "configurator.h"
19
20 #include <basis/definitions.h>
21 #include <basis/mutex.h>
22 #include <structures/roller.h>
23
24 namespace configuration {
25
26 //! Implements an id generator that interacts with a configuration file.
27 /*!
28   This provides an int_roller (which provides rolling ids given a range to
29   issue them from) that is stored in a configurator.  The instantiated
30   object is the real source of the ids, but the configurator file is
31   periodically updated to reflect the current id state.  If a program
32   is restarted later, then it will start using ids that it had not already
33   issued in its last run, as long as the configurator is a persistent object.
34   Note that the range of ids had better be quite large; otherwise the program
35   could still have live entries under an id that is about to be reissued
36   due to wrap-around.
37 */
38
39 class ini_roller : public virtual basis::root_object
40 {
41 public:
42   ini_roller(configurator &config, const basis::astring &section,
43           const basis::astring &entry, int min, int max);
44     //!< creates a roller that updates "config" with the current id value.
45     /*!< the updates are not continuous, but are done periodically to avoid
46     constantly writing to the "config".  the "section" and "entry" dictate
47     where the entry is saved in the "config".  the "min" and "max" provide the
48     range of the id, where it will start at "min", increment by one until it
49     reaches at most "max", and then it will start back at "min" again.  note
50     that "config" must exist for the duration of the ini_roller. */
51
52   virtual ~ini_roller();
53
54   DEFINE_CLASS_NAME("ini_roller");
55
56   int next_id();
57     //!< returns the next number to be issued.
58
59   int current_id() const;
60     //!< returns the current id; this is the one that was last issued.
61
62 private:
63   configurator &_ini;  //!< provides access to the ini file.
64   structures::int_roller *_ids;  //!< the current id number is managed here.
65   basis::astring *_section;  //!< remembers the right section for our id entry.
66   basis::astring *_entry;  //!< remembers the entry name.
67   basis::mutex *_lock;  //!< keeps us thread safe.
68 };
69
70 } //namespace.
71
72 #endif
73