-
Notifications
You must be signed in to change notification settings - Fork 4
Home
Ramin Farajpour Cami edited this page Jun 29, 2017
·
5 revisions
Welcome to the PParam wiki!
PParam is a portable parameter(data structure) definition framework for C++. By PParam, developers could define their parameters, and then easily convert them to XML/JSON and read/write the parameters from/to files or databases.
PParam contains XParam library. If you want to earn knowledge of this library click on the link.
PParam contains SParam library. If you want to earn knowledge of this library click on the link.
PParam contains XList library. If you want to earn knowledge of this library click on the link.
This is a simple example to help you programming with this library.
#include <iostream>
#include <sparam.hpp>
#include <xparam.hpp>
using namespace pparam;
using namespace std;
class Device;
class DeviceType : public XMixParam
{
public:
enum Machine{
SERVER,
PC,
LAPTOP,
VM,
MAX
};
typedef XInt Literal;
DeviceType(const string &pname):
XMixParam(pname),
machine("machine", SERVER)
{
addParam(&machine);
}
DeviceType():
XMixParam("machines"),
machine("machine", SERVER)
{
addParam(&machine);
}
void set_type(Literal _machine)
{
machine = _machine;
}
Literal get_type() const
{
return machine.get_value();
}
XParam *getTypeParam()
{
return (XParam*) (this);
}
string value()
{
return machine.value();
}
void set_machine(const int _machine)
{
machine = _machine;
}
Device *newT() throw (Exception);
public:
static const string typeString[MAX];
private:
XEnumParam<DeviceType> machine;
};
class Device : public XMixParam
{
public:
typedef DeviceType Type;
Device():
XMixParam("machines"),
machine("machine"),
ip("ip"),
legalMAC("MAC_Address"),
uptime("uptime", 0, -1),
cpuUsage("cpu_usage", 0, 100),
ramUsage("ram_usage", 0, 100)
{
addParam(&machine);
addParam(&ip);
addParam(&legalMAC);
addParam(&uptime);
addParam(&cpuUsage);
addParam(&ramUsage);
}
virtual void type(Type &_type) const
{
}
bool key(string &_key)
{
_key = legalMAC.value();
return true;
}
void set_ipAddress(string _ip)
{
ip.setAddress(_ip);
}
void set_macAddress(string _mac)
{
legalMAC = _mac;
}
void set_uptime(XULong _time)
{
uptime.set_value(_time);
}
void set_cpuUsage(XFloat _cpu)
{
cpuUsage.set_value(_cpu);
}
void set_ramUsage(XFloat _ram)
{
ramUsage.set_value(_ram);
}
void set_machine(const string _machine)
{
machine = _machine;
}
private:
XTextParam machine;
IPxParam ip;
MACAddressParam legalMAC;
XIntParam<XULong> uptime;
XIntParam<XFloat> cpuUsage;
XIntParam<XFloat> ramUsage;
};
class Server : public Device
{
public:
Server()
{
set_machine("server");
}
virtual void type(Type &_type) const
{
_type.set_machine(DeviceType::SERVER);
}
};
class Pc : public Device
{
public:
Pc()
{
set_machine("PC");
}
virtual void type(Type &_type) const
{
_type.set_machine(DeviceType::PC);
}
};
class Laptop : public Device
{
public:
Laptop()
{
set_machine("laptop");
}
virtual void type(Type &_type) const
{
_type.set_machine(DeviceType::LAPTOP);
}
};
class Vm : public Device
{
public:
Vm()
{
set_machine("VM");
}
virtual void type(Type &_type) const
{
_type.set_machine(DeviceType::VM);
}
};
class DeviceList : public XListParam<Device, string>
{
public:
typedef XListParam<Device, string>::iterator DeviceIterator;
DeviceList() :
XListParam<Device, string>("Device_List")
{
enable_smap();
}
};
const string DeviceType::typeString[MAX] = {
"server",
"pc",
"laptop",
"vm"
};
Device *DeviceType::newT() throw (Exception) {
if (machine.get_value() == SERVER )
return new Server;
if (machine.get_value() == PC )
return new Pc;
if (machine.get_value() == LAPTOP )
return new Laptop;
if (machine.get_value() == VM )
return new Vm;
throw Exception("Undefined machine", TracePoint("device"));
}
int main()
{
DeviceList deviceList;
try
{
Server server;
server.set_ipAddress("192.168.1.1");
server.set_macAddress("00:A0:C9:14:C8:29");
server.set_cpuUsage(25);
server.set_ramUsage(50);
server.set_uptime(122);
deviceList.addT(server);
Laptop laptop;
laptop.set_ipAddress("192.168.1.2");
laptop.set_macAddress("00:A0:C9:14:C8:30");
laptop.set_cpuUsage(20);
laptop.set_ramUsage(40);
laptop.set_uptime(20);
deviceList.addT(laptop);
Vm vm;
vm.set_ipAddress("192.168.1.2");
vm.set_macAddress("00:A0:C9:14:C8:31");
vm.set_cpuUsage(20);
vm.set_ramUsage(40);
vm.set_uptime(20);
deviceList.addT(vm);
Pc pc;
pc.set_ipAddress("192.168.1.2");
pc.set_macAddress("00:A0:C9:14:C8:32");
pc.set_cpuUsage(20);
pc.set_ramUsage(40);
pc.set_uptime(20);
deviceList.addT(pc);
} catch (Exception &exception) {
cout << exception.what() << endl;
}
cout << deviceList.xml() << endl;
return 0;
}<Device_List> <machines> <machine>server</machine> <ip>192.168.1.1</ip> <MAC_Address>00:A0:C9:14:C8:29</mac_address> <uptime>122</uptime> <cpu_usage>25</cpu_usage> <ram_usage>50</ram_usage> </machines> <machines> <machine>laptop</machine> <ip>192.168.1.2</ip> <MAC_Address>00:A0:C9:14:C8:30</mac_address> <uptime>20</uptime> <cpu_usage>20</cpu_usage> <ram_usage>40</ram_usage> </machines> <machines> <machine>VM</machine> <ip>192.168.1.2</ip> <MAC_Address>00:A0:C9:14:C8:31</mac_address> <uptime>20</uptime> <cpu_usage>20</cpu_usage> <ram_usage>40</ram_usage> </machines> <machines> <machine>PC</machine> <ip>192.168.1.2</ip> <MAC_Address>00:A0:C9:14:C8:32</mac_address> <uptime>20</uptime> <cpu_usage>20</cpu_usage> <ram_usage>40</ram_usage> </machines> </device_list>