G
g.vukoman
Hi all,
I asking me how to design my method. Should I use pointer only? Or
references? Or Values? I don't know...
Below is a small method to read the mem amount of a machine. Fell free
to edit the src below
#include "MemSysInfo.h"
namespace SysInfo {
unsigned int * SysInfo::MemSysInfo_t::GetSize() const { // SHOULD I
RETURN A POINTER/REFERENCE/VALUE?
using namespace std;
using namespace boost;
ifstream* const inFile = new ifstream("/proc/meminfo"); // SHOULD
string* inLine = new string; // I
const string* const outLine = new string("\\2"); // USE
const regex* const rgx = new regex("^(MemTotal: )(\\d+)
( kB)?"); // POINTER ONLY?
unsigned int* memSize = new unsigned int(0); // WHAT WOULD
stringstream* stringToInt = new stringstream; // YOU USE?
if(!inFile->is_open())
throw "can_not_open_proc_meminfo"; // SHOULD I DELETE POINTERS
BEFORE THROW?
else if(inFile == 0 || inLine == 0 || outLine == 0 || rgx == 0 ||
memSize == 0 || stringToInt == 0)
throw "no_memory_available"; // SHOULD I DELETE POINTERS BEFORE
THROW?
while(getline(*inFile, *inLine))
if(regex_match(*inLine, *rgx)) {
*stringToInt << regex_replace(*inLine, *rgx, *outLine);
*stringToInt >> *memSize;
break;
}
inFile->close();
delete inFile;
delete inLine;
delete outLine;
delete rgx;
delete stringToInt;
return(memSize);
}
} // namespace SysInfo
Goran
I asking me how to design my method. Should I use pointer only? Or
references? Or Values? I don't know...
Below is a small method to read the mem amount of a machine. Fell free
to edit the src below
#include "MemSysInfo.h"
namespace SysInfo {
unsigned int * SysInfo::MemSysInfo_t::GetSize() const { // SHOULD I
RETURN A POINTER/REFERENCE/VALUE?
using namespace std;
using namespace boost;
ifstream* const inFile = new ifstream("/proc/meminfo"); // SHOULD
string* inLine = new string; // I
const string* const outLine = new string("\\2"); // USE
const regex* const rgx = new regex("^(MemTotal: )(\\d+)
( kB)?"); // POINTER ONLY?
unsigned int* memSize = new unsigned int(0); // WHAT WOULD
stringstream* stringToInt = new stringstream; // YOU USE?
if(!inFile->is_open())
throw "can_not_open_proc_meminfo"; // SHOULD I DELETE POINTERS
BEFORE THROW?
else if(inFile == 0 || inLine == 0 || outLine == 0 || rgx == 0 ||
memSize == 0 || stringToInt == 0)
throw "no_memory_available"; // SHOULD I DELETE POINTERS BEFORE
THROW?
while(getline(*inFile, *inLine))
if(regex_match(*inLine, *rgx)) {
*stringToInt << regex_replace(*inLine, *rgx, *outLine);
*stringToInt >> *memSize;
break;
}
inFile->close();
delete inFile;
delete inLine;
delete outLine;
delete rgx;
delete stringToInt;
return(memSize);
}
} // namespace SysInfo
Goran