VirtualBox

Changeset 6104 in vbox


Ignore:
Timestamp:
Dec 17, 2007 1:30:40 PM (17 years ago)
Author:
vboxsync
Message:

Main:Settings: Added brief usage introduction.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/VBox/settings.h

    r6076 r6104  
    8989 * conflicts (followed by crashes, memory corruption etc.). A proper solution
    9090 * for these conflicts is to be found.
     91 *
     92 * In order to load a settings file the program creates a TreeBackend instance
     93 * using one of the specific backends (e.g. XmlTreeBackend) and then passes an
     94 * Input stream object (e.g. File or MemoryBuf) to the TreeBackend::read()
     95 * method to parse the stream and build the settings tree. On success, the
     96 * program uses the TreeBackend::rootKey() method to access the root key of
     97 * the settings tree. The root key provides access to the whole tree of
     98 * settings through the methods of the Key class which allow to read, change
     99 * and create new key values. Below is an example that uses the XML backend to
     100 * load the settings tree, then modifies it and then saves the modifications.
     101 *
     102 * @code
     103    using namespace settings;
     104
     105    try
     106    {
     107        File file (File::ReadWrite, "myfile.xml");
     108        XmlTreeBackend tree;
     109
     110        // load the tree, parse it and validate using the XML schema
     111        tree.read (aFile, "myfile.xsd", XmlTreeBackend::Read_AddDefaults);
     112
     113        // get the root key
     114        Key root = tree.key();
     115        printf ("root=%s\n", root.name());
     116
     117        // enumerate all child keys of the root key named Foo
     118        Key::list children = root.keys ("Foo");
     119        for (Key::list::const_iterator it = children.begin();
     120             it != children.end();
     121             ++ it)
     122        {
     123            // get the "level" attribute
     124            int level = (*it).value <int> ("level");
     125            if (level > 5)
     126            {
     127                // if so, create a "Bar" key if it doesn't exist yet
     128                Key bar = (*it).createKey ("Bar");
     129                // set the "date" attribute
     130                RTTIMESPEC now;
     131                RTTimeNow (&now);
     132                bar.setValue <RTTIMESPEC> ("date", now);
     133            }
     134            else if (level < 2)
     135            {
     136                // if its below 2, delete the whole "Foo" key
     137                (*it).zap();
     138            }
     139        }
     140
     141        // save the tree on success (the second try is to distinguish between
     142        // stream load and save errors)
     143        try
     144        {
     145            aTree.write (aFile);
     146        }
     147        catch (const EIPRTFailure &err)
     148        {
     149            // this is an expected exception that may happen in case of stream
     150            // read or write errors
     151            printf ("Could not save the settings file '%s' (%Vrc)");
     152                    file.uri(), err.rc());
     153
     154            return FAILURE;
     155        }
     156
     157        return SUCCESS;
     158    }
     159    catch (const EIPRTFailure &err)
     160    {
     161        // this is an expected exception that may happen in case of stream
     162        // read or write errors
     163        printf ("Could not load the settings file '%s' (%Vrc)");
     164                file.uri(), err.rc());
     165    }
     166    catch (const XmlTreeBackend::Error &err)
     167    {
     168        // this is an XmlTreeBackend specific exception exception that may
     169        // happen in case of XML parse or validation errors
     170        printf ("Could not load the settings file '%s'.\n%s"),
     171                file.uri(), err.what() ? err.what() : "Unknown error");
     172    }
     173    catch (const std::exception &err)
     174    {
     175        // the rest is unexpected (e.g. should not happen unless you
     176        // specifically wish so for some reason and therefore allow for a
     177        // situation that may throw one of these from within the try block
     178        // above)
     179        AssertMsgFailed ("Unexpected exception '%s' (%s)\n",
     180                         typeid (err).name(), err.what());
     181    catch (...)
     182    {
     183        // this is even more unexpected, and no any useful info here
     184        AssertMsgFailed ("Unexpected exception\n");
     185    }
     186
     187    return FAILURE;
     188 * @endcode
     189 *
     190 * Note that you can get a raw (string) value of the attribute using the
     191 * Key::stringValue() method but often it's simpler and better to use the
     192 * templated Key::value<>() method that can convert the string to a value of
     193 * the given type for you (and throw exceptions when the converison is not
     194 * possible). Similarly, the Key::setStringValue() methid is used to set a raw
     195 * string value and there is a templated Key::setValue<>() method to set a
     196 * typed value which will implicitly convert it to a string.
     197 *
     198 * Currently, types supported by Key::value<>() and Key::setValue<>() include
     199 * all C and IPRT integer types, bool and RTTIMESPEC (represented as isoDate
     200 * in XML). You can always add support for your own types by creating
     201 * additional specializations of the FromString<>() and ToString<>() templates
     202 * in the settings namespace (see the real examples in this header).
     203 *
     204 * See individual funciton, class and method descriptions to get more details
     205 * on the Settings File Manipulation API.
    91206 */
    92207
     
    11371252
    11381253/**
    1139  * The MemoryFile class represents a stream implementation that reads from the
     1254 * The MemoryBuf class represents a stream implementation that reads from the
    11401255 * memory buffer.
    11411256 */
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette