1 // attributes.cc
2 //
3 // $COPYRIGHT$
4 //
5 // $Id: attributes.cc,v 1.6 2000/09/03 19:38:43 bwbarrett Exp $
6 //
7 // Description: An example of some of the ways to add attributes
8 // to the registry
9 //
10
11
12 #include <string>
13 #include <iostream>
14 #include <inilib.h>
15
16 using namespace std;
17 using namespace INI;
18
19 int
main(int argc,char * argv[])20 main(int argc, char *argv[])
21 {
22 // As you know (you have read the docs, right), there is more than
23 // one way to add an attribute. Not only that, but there is a
24 // difference in how the attributes are created depending on how you
25 // first created the attribute.
26
27 // To make the syntax a little easier, explicitly create a section
28 // and deal with that directly:
29 section example_section;
30
31 // Method 1 for adding an attribute: using the insert method. This
32 // creates an attribute who's type is either an int, double, string,
33 // or bool depending on what type the value you passed tothe insert
34 // method.
35
36 // For example, to create an attribute called value 1 with an
37 // integer value, we would call:
38 example_section.insert("value 1", 1);
39
40 // or to do the same, but with a double, we could call:
41 example_section.insert("value 2", 2.2);
42
43 // of course, that is really no different than this:
44 double im_a_double = 2.2;
45 example_section.insert("value 3", im_a_double);
46
47
48 // Method 2 for adding an attribute: implicitly creating an
49 // attribute usingthe [] operator. This occurs in one of two
50 // situations: 1) you call <variable> = <some section>["foo"], where
51 // foo is not in <some section> or 2) you call <some section>["foo"]
52 // = variable, where foo is not in <some section>.
53 //
54 // In both of these cases, an attribute must be created before we
55 // know the type of the attribute. So we use a wrapper attribute to
56 // cover this case. Once the type is known, the wrapper attribute
57 // creates an attribute of that type and redirects all methods to
58 // that attribute type.
59 //
60 // What does all this mean for you? There is an extra function call
61 // every time you access an attribute created this way. That's it.
62 // So if you don't mind an extra function call here and there, just
63 // use this method. It's much easier. This how attributes are
64 // created in all the other examples.
65
66 // This will create an int attribute with value 1:
67 example_section["value 4"] = 1;
68
69 // This will create a double attribute with value 5:
70 example_section["value 5"] = 5;
71
72 // This will create an int attribute, with unspecified value. A
73 // value of 0 will be assigned to im_an_int:
74 int im_an_int = example_section["value 6"];
75 example_section["value 7"] = im_an_int;
76
77 // Regardless of whether the value assigned to/from is an int,
78 // double, string, or bool, the code operates in a similar fasion.
79 // All possiblities are not shown so that this example didn't go on
80 // forever.
81
82 // have the program produce some output...
83 cout << "This program really doesn't produce any output." << endl;
84
85
86 return 0;
87 }
88