Property File

This code example shows the simplest use of OSL. The saving and loading of one object.

Compile on Linux with:

gcc -o propertyfile -I ../../OSL/include/ main.cpp Properties.cpp -L ../../OSL/lib/ -lstdc++ -losl



main.cpp


#include <osl/rfx.h>
#include <osl/objstream.h>

#include "Properties.h"
#include "MyApp.h"

using namespace osl;

int main(int argc, char **argv)
{
    osl::cls_reg::init();

    Properties *properties;

    // Load the property file
    ifstream_xml in("myconf.ini");
    if (in.good()) {
        in >> properties;
        in.close();
    } else {
        properties = new Properties();
        properties->user = "tom";
    }

    // Run the application
    MyApp myapp(properties);
    myapp.run();

    // Save the property file
    ofstream_xml out("myconf.ini");
    out << properties;

    return 0;
}

Properties.h


#ifndef properties_h_included
#define properties_h_included

#include <string>
using namespace std;

class Properties
{
public:
    string user;
    string userpwd;
    string workdirectory;
    int count;

public:
    Properties()
    : count(0)
    {
    }
};


#endif

Properties.cpp


#include "Properties.h"
#include <osl/rfx.h>

void osl_init_Properties(osl::cls<Properties>& c) {
    c.member(&Properties::user, "user");
    c.member(&Properties::userpwd, "userpwd");
    c.member(&Properties::workdirectory, "workdirectory");
    c.member(&Properties::count, "count");
}
osl::cls<Properties> cProperties("Properties", osl_init_Properties);

MyApp.h


#ifndef MYAPP_H_INCLUDED
#define MYAPP_H_INCLUDED

#include "Properties.h"

#include <iostream>
using namespace std;

class MyApp
{
public:
    MyApp(Properties *props) : properties(props) {}
    
    void run() {
        cout << properties->user << endl;
    }
    
private:
        
    Properties *properties;
};


#endif

myconf.ini


<objectlibrary serializer="osl::srl_xml">
<object type="Properties" id="00000000014971d0">
    <memberlist>
            <member type="osl::string" name="user">tom</member>
            <member type="osl::string" name="userpwd"></member>
            <member type="osl::string" name="workdirectory"></member>
            <member type="osl::sint" name="count">0</member>
        </memberlist>
    </object>
</objectlibrary>
Follow @ClearObjects