Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


Straightforward Settings

John Torjo is the General Manager of Macadamian Romania. Even after 10+ years, he's still in love with C++. He can be reached at [email protected]

Storage of settings is an issue you've no doubt dealt with many times. You've seen settings stored in .ini files, or in the registry. What you need is a simple library to abstract this away—and that's what I'm about to present. So leave the coffee behind, and get ready for less stress!

Usability: More than Meets the Eye

Settings are key to flexibility—your application will adapt to the user's needs. This is good for your customer, for your boss, and for you.

However, different settings might be kept in different places. But to you, the programmer, this can be made transparent. What I'll present here is code that will do just that. At the beginning of the application, you'll specify the locations for keeping the settings with just a few lines of code. If some of your settings' locations need to be changed, you'll only need to change one or two lines of code. How's that for cool?

Test Drive

So how do you use it? Well, it's called straightforward for a reason:

// get
type val = setting(name);
// set
setting(name) = val;
// forced get
type val = setting<type>(name);
// forced set
setting<type>(name) = val;

Listing One shows a few examples.

// get
std::string name = setting("user.name");
std::string pass = setting("user.passw");
long retries = setting("app.retries");

// set
setting("app.retries") = 5;
setting("user.name") = "John";
setting("user.passw") = "secret";

// forced get
long w = setting<long>("width");

// forced set
setting<unsigned long>("width") = 12400;

Listing One

You'll need the forced get and forced set in more complex scenarios, when implicit conversions might come into place, when you use the constant in a templated function, when you want to force a constant to be of a certain type, and so on; see Listing Two.

// this needs a forced get
// otherwise, it wouldn't know which operator* to call
int size = setting<int>("width") * setting<int>("height");

struct employee {
  operator std::string() const { return name; }
employee e;
// assuming employee does not have an operator<<,
// this needs a forced set (which will cause the automatic
// conversion of e to std::string)
setting<std::string>("user.name") = e;

Listing Two

Case-insensitive Names

Setting names are case-insensitive. The last thing you'll need is to think: Is it "user.name", "User.name", "User.Name", or "USER.NAME"? No matter what convention you use, the unfortunate truth is that someone will break it. You don't want that; check out Listing Three.

// the following are equivalent
std::string s = setting("user.name");
std::string s = setting("user.Name");
std::string s = setting("User.name");
std::string s = setting("uSer.naMe");
// the following are equivalent
setting("user.name") = s;
setting("User.name") = s;
setting("USER.NAME") = s;
Listing Three

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.