mrw-c++-0.92 (mrw)
- new file: version.cpp - new file header for all sources - work around warning in mrw::auto<T> - possibility to compile without log4cxx - work around bugs in demangle.h and libiberty.h - corrections in documentation - added simple tracing mechanism - more warnings - small corrections in Auto<>::Free and a new test for it - possibility to compile without stack trace
This commit is contained in:
132
mrw/arg.cpp
Normal file
132
mrw/arg.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
/** @file
|
||||
|
||||
$Id$
|
||||
|
||||
$Date$
|
||||
$Author$
|
||||
|
||||
@copy © Marc Wäckerlin
|
||||
@license LGPL, see file <a href="license.html">COPYING</a>
|
||||
|
||||
$Log$
|
||||
Revision 1.1 2004/08/28 16:13:42 marc
|
||||
mrw-c++-0.92 (mrw)
|
||||
- new file: version.cpp
|
||||
- new file header for all sources
|
||||
- work around warning in mrw::auto<T>
|
||||
- possibility to compile without log4cxx
|
||||
- work around bugs in demangle.h and libiberty.h
|
||||
- corrections in documentation
|
||||
- added simple tracing mechanism
|
||||
- more warnings
|
||||
- small corrections in Auto<>::Free and a new test for it
|
||||
- possibility to compile without stack trace
|
||||
|
||||
*/
|
||||
#include <mrw/arg.hpp>
|
||||
#include <mrw/exception.hpp>
|
||||
|
||||
namespace mrw {
|
||||
|
||||
const std::string& Param::Value::toString() const throw(std::exception) {
|
||||
throw mrw::bad_cast();
|
||||
}
|
||||
|
||||
int Param::Value::toInt() const throw(std::exception) {
|
||||
throw mrw::bad_cast();
|
||||
}
|
||||
|
||||
bool Param::Value::toBool() const throw(std::exception) {
|
||||
throw mrw::bad_cast();
|
||||
}
|
||||
|
||||
void Param::IntValue::operator=(const std::string& s) throw(std::exception) {
|
||||
if (!(std::stringstream(s)>>_i)) throw mrw::bad_cast();
|
||||
}
|
||||
|
||||
const mrw::SmartPointer<Param::Value>& Param::operator[](unsigned int i) const
|
||||
throw(std::exception) {
|
||||
if (i<_params.size()) return _params[i];
|
||||
throw mrw::out_of_range
|
||||
(((std::stringstream&)
|
||||
(std::stringstream()<<"check failed: "
|
||||
<<i<<'<'<<_params.size())).str());
|
||||
}
|
||||
|
||||
mrw::SmartPointer<Param::Value>& Param::setable(unsigned int i)
|
||||
throw(std::exception) {
|
||||
if (i<_params.size()) return _params[i];
|
||||
throw mrw::out_of_range
|
||||
(((std::stringstream&)
|
||||
(std::stringstream()<<"check failed: "
|
||||
<<i<<'<'<<_params.size())).str());
|
||||
}
|
||||
|
||||
Args& Args::operator<<(const mrw::Opt& opt) throw(std::invalid_argument) {
|
||||
// twice the same, but other sort order
|
||||
Options::iterator it(_options.insert(_options.end(), opt));
|
||||
if (!_shortopts.insert(ShortOpts::value_type(opt._shortname, it)).second)
|
||||
throw mrw::invalid_argument(std::string(1, opt._shortname));
|
||||
if (!_longopts.insert(LongOpts::value_type(opt._longname, it)).second)
|
||||
throw mrw::invalid_argument(opt._longname);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Args& Args::operator<<(const char *const*const argv) throw(std::exception) {
|
||||
if (_argc<0)
|
||||
throw mrw::invalid_argument("argc was not set when shifting argv");
|
||||
return parse(_argc, argv);
|
||||
}
|
||||
|
||||
const Opt& Args::find(char c) const throw(std::exception) {
|
||||
ShortOpts::const_iterator it(_shortopts.find(c));
|
||||
if (it==_shortopts.end()) throw mrw::out_of_range(std::string(1, c));
|
||||
return *it->second;
|
||||
}
|
||||
|
||||
const Opt& Args::find(const std::string& s) const throw(std::exception) {
|
||||
LongOpts::const_iterator it(_longopts.find(s));
|
||||
if (it==_longopts.end()) throw mrw::out_of_range(s);
|
||||
return *it->second;
|
||||
}
|
||||
|
||||
Args& Args::parse(int argc, const char*const*const argv)
|
||||
throw(std::exception) {
|
||||
if (argc>0) _filename = argv[0];
|
||||
for (int i(1); i<argc; ++i) {
|
||||
std::string arg(argv[i]);
|
||||
if (arg.find("--")==0 && arg!="--") { // long arguments
|
||||
LongOpts::iterator it(_longopts.find(arg));
|
||||
if (it!=_longopts.end() || i+it->second->args().size()>=argc)
|
||||
throw mrw::invalid_argument(arg);
|
||||
it->second->set();
|
||||
for (int j(0), l(it->second->args().size()); j<l; ++j) {
|
||||
*(it->second->args().setable(j)) = argv[++i];
|
||||
}
|
||||
} else if (arg.find("-")==0) { // short arguments
|
||||
// first check all, then set all
|
||||
for (int j(1), l(arg.size()); j<l; ++j) {
|
||||
ShortOpts::iterator it(_shortopts.find(arg[j]));
|
||||
if (it==_shortopts.end() || it->second->args().size()>0 &&
|
||||
(j+1!=l || i+it->second->args().size()>=argc))
|
||||
throw mrw::invalid_argument(arg);
|
||||
}
|
||||
for (int j(1), l(arg.size()); j<l; ++j) {
|
||||
ShortOpts::iterator it(_shortopts.find(arg[j]));
|
||||
it->second->set();
|
||||
if (j+1==l && it->second->args().size()>0) {
|
||||
for (int k(0); k < it->second->args().size(); ++k) {
|
||||
*(it->second->args().setable(k)) = argv[++i];
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (arg!="--") _otherargs.push_back(arg);
|
||||
while (++i<argc) _otherargs.push_back(argv[i]);
|
||||
}
|
||||
}
|
||||
if (_help && find(_help)) help();
|
||||
return *this;
|
||||
}
|
||||
|
||||
}
|
31
mrw/auto.cpp
31
mrw/auto.cpp
@@ -1,31 +0,0 @@
|
||||
#include <mrw/auto.hpp>
|
||||
#include <unistd.h> // close
|
||||
#include <sys/stat.h> // fstat
|
||||
#include <sys/mman.h> // mmap
|
||||
|
||||
namespace mrw {
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
AutoFile& AutoFile::reset(int fd) throw() {
|
||||
if (_fd!=-1) close(_fd);
|
||||
_fd = fd;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
AutoMapper::AutoMapper(int fd, size_t sz, void* addr,
|
||||
int prot, int flags, off_t off) throw(): _cont(0) {
|
||||
if (!(_sz=sz)) {
|
||||
struct stat st;
|
||||
if (fd==-1 || fstat(fd, &st)==-1) return;
|
||||
_sz = st.st_size;
|
||||
}
|
||||
if ((_cont=mmap(addr, _sz, prot, flags, fd, off))==MAP_FAILED) release();
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
AutoMapper::~AutoMapper() throw() {
|
||||
if (_cont && _sz) munmap(_cont, _sz);
|
||||
}
|
||||
}
|
162
mrw/smartpointer_test.cpp
Normal file
162
mrw/smartpointer_test.cpp
Normal file
@@ -0,0 +1,162 @@
|
||||
/** @file
|
||||
|
||||
$Id$
|
||||
|
||||
$Date$
|
||||
$Author$
|
||||
|
||||
@copy © Marc Wäckerlin
|
||||
@license LGPL, see file <a href="license.html">COPYING</a>
|
||||
|
||||
$Log$
|
||||
Revision 1.1 2004/08/28 16:13:42 marc
|
||||
mrw-c++-0.92 (mrw)
|
||||
- new file: version.cpp
|
||||
- new file header for all sources
|
||||
- work around warning in mrw::auto<T>
|
||||
- possibility to compile without log4cxx
|
||||
- work around bugs in demangle.h and libiberty.h
|
||||
- corrections in documentation
|
||||
- added simple tracing mechanism
|
||||
- more warnings
|
||||
- small corrections in Auto<>::Free and a new test for it
|
||||
- possibility to compile without stack trace
|
||||
|
||||
*/
|
||||
#include <mrw/smartpointer.hpp>
|
||||
#include <cppunit/TestFixture.h>
|
||||
#include <cppunit/ui/text/TestRunner.h>
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
#include <cppunit/extensions/TestFactoryRegistry.h>
|
||||
#include <string>
|
||||
|
||||
class Content {
|
||||
private:
|
||||
int& _drop;
|
||||
Content();
|
||||
public:
|
||||
Content(int& drop): _drop(drop) {}
|
||||
virtual ~Content() {++_drop;}
|
||||
};
|
||||
class A: public Content {public: A(int& d): Content(d) {} virtual void fn() {}};
|
||||
class B: public A {public: B(int& d): A(d) {}};
|
||||
class C: public A {public: C(int& d): A(d) {}};
|
||||
class SmartPointerTest:
|
||||
public mrw::SmartPointerParent, public CppUnit::TestFixture {
|
||||
public:
|
||||
void SimpleConstructor() {
|
||||
mrw::SmartPointer<Content> p1;
|
||||
CPPUNIT_ASSERT(!(getPointer(p1) || getCounter(p1)));
|
||||
}
|
||||
void CopyConstructor() {
|
||||
mrw::SmartPointer<Content> p1;
|
||||
mrw::SmartPointer<Content> p2(p1);
|
||||
CPPUNIT_ASSERT(!(getPointer(p1) || getCounter(p1) ||
|
||||
getPointer(p2) || getCounter(p2)));
|
||||
}
|
||||
void PointerConstructor() {
|
||||
int drops(0);
|
||||
mrw::SmartPointer<Content> p1(0);
|
||||
mrw::SmartPointer<Content> p2(new Content(drops));
|
||||
CPPUNIT_ASSERT(!(getPointer(p1) || getCounter(p1) ||
|
||||
drops!=0 || !getPointer(p2) || !getCounter(p2) ||
|
||||
getCounter(p2)->get()!=1));
|
||||
}
|
||||
void CastConstructor() {
|
||||
int drops1(0);
|
||||
mrw::SmartPointer<A> pa(new B(drops1));
|
||||
mrw::SmartPointer<B> pb(pa);
|
||||
mrw::SmartPointer<C> pc(pa);
|
||||
mrw::SmartPointer<A> pa2(pb);
|
||||
CPPUNIT_ASSERT(!(drops1!=0 || !getPointer(pa) || !getCounter(pa) ||
|
||||
getPointer(pb)!=getPointer(pa) ||
|
||||
getCounter(pb)!=getCounter(pa) ||
|
||||
getPointer(pc) || getCounter(pc) ||
|
||||
getCounter(pa)->get()!=3 ||
|
||||
getPointer(pa2)!=getPointer(pa) ||
|
||||
getCounter(pa2)!=getCounter(pa)));
|
||||
}
|
||||
void Destructor() {
|
||||
int drops(0);
|
||||
mrw::SmartPointer<Content>* p1 =
|
||||
new mrw::SmartPointer<Content>(new Content(drops));
|
||||
delete p1;
|
||||
CPPUNIT_ASSERT(!(drops!=1));
|
||||
}
|
||||
void CopyAssign() {
|
||||
int drops1(0);
|
||||
int drops2(0);
|
||||
mrw::SmartPointer<Content> p1(new Content(drops1));
|
||||
mrw::SmartPointer<Content> p2(new Content(drops2));
|
||||
p2 = p1;
|
||||
p1 = p2;
|
||||
CPPUNIT_ASSERT(!(drops1!=0 || !getPointer(p1) || !getCounter(p1) ||
|
||||
getCounter(p1)->get()!=2 ||
|
||||
getPointer(p1)!=getPointer(p2) ||
|
||||
drops2!=1 || !getPointer(p2) || !getCounter(p2) ||
|
||||
getCounter(p2)->get()!=2 ||
|
||||
getCounter(p1)!=getCounter(p2)));
|
||||
}
|
||||
void PointerAssign() {
|
||||
int drops1(0);
|
||||
int drops2(0);
|
||||
int drops3(0);
|
||||
mrw::SmartPointer<Content> p1(new Content(drops1));
|
||||
mrw::SmartPointer<Content> p2(0);
|
||||
p1 = new Content(drops2);
|
||||
p1 = 0;
|
||||
p2 = new Content(drops3);
|
||||
CPPUNIT_ASSERT(!(drops1!=1 || getPointer(p1) || getCounter(p1) ||
|
||||
drops2!=1 || !getPointer(p2) || !getCounter(p2) ||
|
||||
drops3!=0 || getCounter(p2)->get()!=1));
|
||||
}
|
||||
void CastAssign() {
|
||||
int drops1(0);
|
||||
int drops2(0);
|
||||
int drops3(0);
|
||||
mrw::SmartPointer<A> pa(new B(drops1));
|
||||
mrw::SmartPointer<B> pb(new B(drops2));
|
||||
mrw::SmartPointer<C> pc(new C(drops3));
|
||||
pa = pa;
|
||||
pa = pb;
|
||||
pa = pc;
|
||||
pb = pa;
|
||||
pc = pa;
|
||||
CPPUNIT_ASSERT(!(drops1!=1 || drops2!=1 || drops3!=0 ||
|
||||
!getPointer(pa) || getPointer(pa)!=getPointer(pc) ||
|
||||
!getCounter(pa) || getCounter(pa)!=getCounter(pc) ||
|
||||
getCounter(pa)->get()!=2 || getPointer(pb) ||
|
||||
getCounter(pb)));
|
||||
}
|
||||
void PointerAccess() {
|
||||
mrw::SmartPointer<std::string> p1(new std::string);
|
||||
mrw::SmartPointer<std::string> p2;
|
||||
*p1 = "Hallo Welt!";
|
||||
CPPUNIT_ASSERT(!(p1.operator->()!=&*p1 || p1->find("Welt")!=6 ||
|
||||
p2.operator->()));
|
||||
}
|
||||
void OperatorBool() {
|
||||
mrw::SmartPointer<std::string> p1(new std::string);
|
||||
mrw::SmartPointer<std::string> p2;
|
||||
CPPUNIT_ASSERT(!(!p1 || p2));
|
||||
}
|
||||
CPPUNIT_TEST_SUITE(SmartPointerTest);
|
||||
CPPUNIT_TEST(SimpleConstructor);
|
||||
CPPUNIT_TEST(CopyConstructor);
|
||||
CPPUNIT_TEST(PointerConstructor);
|
||||
CPPUNIT_TEST(CastConstructor);
|
||||
CPPUNIT_TEST(Destructor);
|
||||
CPPUNIT_TEST(CopyAssign);
|
||||
CPPUNIT_TEST(PointerAssign);
|
||||
CPPUNIT_TEST(CastAssign);
|
||||
CPPUNIT_TEST(PointerAccess);
|
||||
CPPUNIT_TEST(OperatorBool);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
};
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION(SmartPointerTest);
|
||||
|
||||
int main() {
|
||||
CppUnit::TextUi::TestRunner runner;
|
||||
runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
|
||||
return runner.run() ? 0 : 1;
|
||||
}
|
31
mrw/version.cpp.in
Normal file
31
mrw/version.cpp.in
Normal file
@@ -0,0 +1,31 @@
|
||||
/** @file
|
||||
|
||||
$Id$
|
||||
|
||||
$Date$
|
||||
$Author$
|
||||
|
||||
@copy © Marc Wäckerlin
|
||||
@license LGPL, see file <a href="license.html">COPYING</a>
|
||||
|
||||
$Log$
|
||||
Revision 1.1 2004/08/28 16:13:42 marc
|
||||
mrw-c++-0.92 (mrw)
|
||||
- new file: version.cpp
|
||||
- new file header for all sources
|
||||
- work around warning in mrw::auto<T>
|
||||
- possibility to compile without log4cxx
|
||||
- work around bugs in demangle.h and libiberty.h
|
||||
- corrections in documentation
|
||||
- added simple tracing mechanism
|
||||
- more warnings
|
||||
- small corrections in Auto<>::Free and a new test for it
|
||||
- possibility to compile without stack trace
|
||||
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace mrw {
|
||||
const std::string version("@(#)@PACKAGENAME@-@MAJOR@.@MINOR@@SUPPORT@");
|
||||
}
|
Reference in New Issue
Block a user