Reduce unnecessary use of smart pointers

Speed up type checking by using std::maps and switch statements
Add compile flag to win/IfcGeom.vcproj
This commit is contained in:
Thomas Krijnen
2011-09-03 13:58:59 +00:00
parent b17832e659
commit d02eeac132
21 changed files with 5539 additions and 5491 deletions
+3085 -3078
View File
File diff suppressed because it is too large Load Diff
+2259 -2257
View File
File diff suppressed because it is too large Load Diff
+47 -37
View File
@@ -280,8 +280,8 @@ TokenArgument::TokenArgument(Token t) {
token = t;
}
EntityArgument::EntityArgument(IfcUtil::IfcSchemaEntity e) {
entity = e;
EntityArgument::EntityArgument(Ifc2x3::Type::Enum ty, Token t) {
entity = new IfcUtil::IfcArgumentSelect(ty,new TokenArgument(t));
}
//
@@ -292,17 +292,15 @@ ArgumentList::ArgumentList(Tokens* t, std::vector<unsigned int>& ids) {
while( Token next = t->Next() ) {
if ( TokenFunc::isOperator(next,',') ) continue;
if ( TokenFunc::isOperator(next,')') ) break;
if ( TokenFunc::isOperator(next,'(') ) Push( ArgumentPtr(new ArgumentList(t,ids)) );
if ( TokenFunc::isOperator(next,'(') ) Push( new ArgumentList(t,ids) );
else {
if ( TokenFunc::isIdentifier(next) ) ids.push_back(TokenFunc::asInt(next));
if ( TokenFunc::isDatatype(next) ) {
Ifc::file->Seek(TokenFunc::Offset(next));
EntityPtr e = EntityPtr(new Entity(0,t));
e->Load(ids,true);
IfcUtil::IfcSchemaEntity entity = Ifc2x3::SchemaEntity(e);
Push ( ArgumentPtr(new EntityArgument(entity)) );
t->Next();
Push ( new EntityArgument(Ifc2x3::Type::FromString(TokenFunc::asString(next)),t->Next()) );
t->Next();
} else {
Push ( ArgumentPtr(new TokenArgument(next)) );
Push ( new TokenArgument(next) );
}
}
}
@@ -344,7 +342,7 @@ ArgumentList::operator std::vector<std::string>() const {
return r;
}
ArgumentList::operator IfcUtil::IfcSchemaEntity() const { throw IfcException("Argument is not an IFC type"); }
ArgumentList::operator SHARED_PTR<IfcUtil::IfcAbstractSelect>() const { throw IfcException("Argument is not an IFC type"); }
//ArgumentList::operator IfcUtil::IfcAbstractSelect::ptr() const { throw IfcException("Argument is not an IFC type"); }
ArgumentList::operator IfcEntities() const {
IfcEntities l ( new IfcEntityList() );
std::vector<ArgumentPtr>::const_iterator it;
@@ -372,6 +370,12 @@ std::string ArgumentList::toString() const {
return ss.str();
}
bool ArgumentList::isNull() const { return false; }
ArgumentList::~ArgumentList() {
for( std::vector<ArgumentPtr>::iterator it = list.begin(); it != list.end(); it ++ ) {
delete (*it);
}
list.clear();
}
//
// Functions for casting the TokenArgument to other types
@@ -384,9 +388,10 @@ TokenArgument::operator std::vector<float>() const { throw IfcException("Argumen
TokenArgument::operator std::vector<int>() const { throw IfcException("Argument is not a list of ints"); }
TokenArgument::operator std::vector<std::string>() const { throw IfcException("Argument is not a list of strings"); }
TokenArgument::operator IfcUtil::IfcSchemaEntity() const { return Ifc::EntityById(TokenFunc::asInt(token)); }
TokenArgument::operator SHARED_PTR<IfcUtil::IfcAbstractSelect>() const {
return SHARED_PTR<IfcUtil::IfcAbstractSelect>( new IfcUtil::IfcEntitySelect(*this) );
}
/*TokenArgument::operator IfcUtil::IfcAbstractSelect::ptr() const {
//TODO Fix memory leak
return new IfcUtil::IfcEntitySelect(*this);
}*/
TokenArgument::operator IfcEntities() const { throw IfcException("Argument is not a list of entities"); }
unsigned int TokenArgument::Size() const { return 1; }
ArgumentPtr TokenArgument::operator [] (unsigned int i) const { throw IfcException("Argument is not a list of arguments"); }
@@ -402,17 +407,15 @@ EntityArgument::operator std::string() const { throw IfcException("Argument is n
EntityArgument::operator std::vector<float>() const { throw IfcException("Argument is not a list of floats"); }
EntityArgument::operator std::vector<int>() const { throw IfcException("Argument is not a list of ints"); }
EntityArgument::operator std::vector<std::string>() const { throw IfcException("Argument is not a list of strings"); }
EntityArgument::operator IfcUtil::IfcSchemaEntity() const {
return SHARED_PTR<IfcUtil::IfcAbstractSelect>( new IfcUtil::IfcArgumentSelect(entity->type(),entity->entity->getArgument(0)) );
}
EntityArgument::operator SHARED_PTR<IfcUtil::IfcAbstractSelect>() const {
return SHARED_PTR<IfcUtil::IfcAbstractSelect>( new IfcUtil::IfcArgumentSelect(entity->type(),entity->entity->getArgument(0)) );
}
EntityArgument::operator IfcUtil::IfcSchemaEntity() const { return entity; }
//EntityArgument::operator IfcUtil::IfcAbstractSelect::ptr() const { return entity; }
EntityArgument::operator IfcEntities() const { throw IfcException("Argument is not a list of entities"); }
unsigned int EntityArgument::Size() const { return 1; }
unsigned int EntityArgument::Size() const { return 1; }
ArgumentPtr EntityArgument::operator [] (unsigned int i) const { throw IfcException("Argument is not a list of arguments"); }
std::string EntityArgument::toString() const { return entity->entity->toString(); }
std::string EntityArgument::toString() const { return Ifc2x3::Type::ToString(entity->type()); }
//return entity->entity->toString(); }
bool EntityArgument::isNull() const { return false; }
EntityArgument::~EntityArgument() { delete entity; }
//
// Reads an Entity from the list of Tokens
@@ -458,13 +461,13 @@ void Entity::Load(std::vector<unsigned int>& ids, bool seek) {
_type = Ifc2x3::Type::FromString(TokenFunc::asString(datatype));
}
Token open = Ifc::tokens->Next();
args = ArgumentPtr(new ArgumentList(Ifc::tokens, ids));
args = new ArgumentList(Ifc::tokens, ids);
unsigned int old_offset = Ifc::file->Tell();
Token semilocon = Ifc::tokens->Next();
if ( ! TokenFunc::isOperator(semilocon,';') ) Ifc::file->Seek(old_offset);
}
Ifc2x3::Type::Enum Entity::type() {
Ifc2x3::Type::Enum Entity::type() const {
return _type;
}
@@ -489,6 +492,10 @@ std::string Entity::toString() {
return ss.str();
}
Entity::~Entity() {
delete args;
}
//
// Returns the entities of type c that have this entity in their ArgumentList
//
@@ -514,7 +521,7 @@ IfcEntities Entity::getInverse(Ifc2x3::Type::Enum c, int i, const std::string& a
}
return l;
}
bool Entity::is(Ifc2x3::Type::Enum v) { return _type == v; }
bool Entity::is(Ifc2x3::Type::Enum v) const { return _type == v; }
unsigned int Entity::id() { return _id; }
//
@@ -529,6 +536,7 @@ bool Ifc::Init(std::istream& f, int len) {
return Ifc::Init(new File(f,len));
}
bool Ifc::Init(IfcParse::File* f) {
Ifc2x3::InitStringMap();
file = f;
if ( ! file->valid ) return false;
tokens = new Tokens (file);
@@ -542,7 +550,7 @@ bool Ifc::Init(IfcParse::File* f) {
if ( log1 ) std::cout << "Scanning file..." << std::endl;
while ( true ) {
if ( currentId ) {
e = EntityPtr(new Entity(currentId,tokens));
e = new Entity(currentId,tokens);
entity = Ifc2x3::SchemaEntity(e);
if ( log1 && !((++x)%1000) ) std::cout << "\r#" << currentId << " " << std::flush;
Ifc2x3::Type::Enum ty = entity->type();
@@ -589,18 +597,16 @@ bool Ifc::Init(IfcParse::File* f) {
Ifc2x3::IfcSIUnit::ptr unit = Ifc2x3::IfcSIUnit::ptr();
float value = 1.0f;
if ( base->is(Ifc2x3::Type::IfcConversionBasedUnit) ) {
const Ifc2x3::IfcConversionBasedUnit::ptr u = reinterpret_pointer_cast<IfcUtil::IfcAbstractSelect,Ifc2x3::IfcConversionBasedUnit>(*it);
const Ifc2x3::IfcConversionBasedUnit::ptr u = reinterpret_pointer_cast<IfcUtil::IfcAbstractSelect,Ifc2x3::IfcConversionBasedUnit>(base);
const Ifc2x3::IfcMeasureWithUnit::ptr u2 = u->ConversionFactor();
Ifc2x3::IfcUnit u3 = u2->UnitComponent();
if ( u3->is(Ifc2x3::Type::IfcSIUnit) ) {
unit = reinterpret_pointer_cast<IfcUtil::IfcAbstractSelect,Ifc2x3::IfcSIUnit>(u3);
unit = (Ifc2x3::IfcSIUnit*) u3;
}
Ifc2x3::IfcValue v = u2->ValueComponent();
if ( v->isSimpleType() ) {
IfcUtil::IfcArgumentSelect::ptr v2 = reinterpret_pointer_cast<IfcUtil::IfcAbstractSelect,IfcUtil::IfcArgumentSelect>(v);
const float f = *v2->wrappedValue();
value *= f;
}
IfcUtil::IfcArgumentSelect* v2 = (IfcUtil::IfcArgumentSelect*) v;
const float f = *v2->wrappedValue();
value *= f;
} else if ( base->is(Ifc2x3::Type::IfcSIUnit) ) {
unit = reinterpret_pointer_cast<IfcUtil::IfcAbstractSelect,Ifc2x3::IfcSIUnit>(base);
}
@@ -646,13 +652,17 @@ IfcException::~IfcException() throw () {}
const char* IfcException::what() const throw() { return error.c_str(); }
void Ifc::Dispose() {
file->Close();
delete file;
delete tokens;
offsets.clear();
for( MapEntityById::const_iterator it = byid.begin(); it != byid.end(); ++ it ) {
delete it->second->entity;
delete it->second;
}
bytype.clear();
byid.clear();
byref.clear();
file->Close();
delete file;
delete tokens;
offsets.clear();
}
float UnitPrefixToValue( Ifc2x3::IfcSIPrefix::IfcSIPrefix v ) {
@@ -675,7 +685,7 @@ float UnitPrefixToValue( Ifc2x3::IfcSIPrefix::IfcSIPrefix v ) {
else return 1.0f;
}
void Ifc::SetOutput(std::ostream* l1, std::ostream* l2) { log1 = l1; log2 = l2; }
void Ifc::LogMessage(const std::string& type, const std::string& message, const SHARED_PTR<IfcAbstractEntity>& entity) {
void Ifc::LogMessage(const std::string& type, const std::string& message, const IfcAbstractEntityPtr entity) {
if ( log2 ) {
(*log2) << "[" << type << "] " << message << std::endl;
if ( entity ) (*log2) << entity->toString() << std::endl;
+12 -9
View File
@@ -46,7 +46,7 @@ namespace IfcParse {
class Entity;
class Entities;
typedef SHARED_PTR<Entity> EntityPtr;
typedef Entity* EntityPtr;
typedef SHARED_PTR<Entities> EntitiesPtr;
//
@@ -130,6 +130,7 @@ namespace IfcParse {
void Push(ArgumentPtr l);
public:
ArgumentList(Tokens* t, std::vector<unsigned int>& ids);
~ArgumentList();
operator int() const;
operator bool() const;
operator float() const;
@@ -138,7 +139,7 @@ namespace IfcParse {
operator std::vector<int>() const;
operator std::vector<std::string>() const;
operator IfcUtil::IfcSchemaEntity() const;
operator SHARED_PTR<IfcUtil::IfcAbstractSelect>() const;
//operator IfcUtil::IfcAbstractSelect::ptr() const;
operator IfcEntities() const;
unsigned int Size() const;
ArgumentPtr operator [] (unsigned int i) const;
@@ -164,7 +165,7 @@ namespace IfcParse {
operator std::vector<int>() const;
operator std::vector<std::string>() const;
operator IfcUtil::IfcSchemaEntity() const;
operator SHARED_PTR<IfcUtil::IfcAbstractSelect>() const;
//operator IfcUtil::IfcAbstractSelect::ptr() const;
operator IfcEntities() const;
unsigned int Size() const;
ArgumentPtr operator [] (unsigned int i) const;
@@ -179,9 +180,10 @@ namespace IfcParse {
//
class EntityArgument : public Argument {
private:
IfcUtil::IfcSchemaEntity entity;
IfcUtil::IfcArgumentSelect* entity;
public:
EntityArgument(IfcUtil::IfcSchemaEntity e);
EntityArgument(Ifc2x3::Type::Enum ty, Token t);
~EntityArgument();
operator int() const;
operator bool() const;
operator float() const;
@@ -190,7 +192,7 @@ namespace IfcParse {
operator std::vector<int>() const;
operator std::vector<std::string>() const;
operator IfcUtil::IfcSchemaEntity() const;
operator SHARED_PTR<IfcUtil::IfcAbstractSelect>() const;
//operator IfcUtil::IfcAbstractSelect::ptr() const;
operator IfcEntities() const;
unsigned int Size() const;
ArgumentPtr operator [] (unsigned int i) const;
@@ -212,14 +214,15 @@ namespace IfcParse {
unsigned int offset;
Entity(unsigned int i, Tokens* t);
Entity(unsigned int i, Tokens* t, unsigned int o);
~Entity();
IfcEntities getInverse(Ifc2x3::Type::Enum c = Ifc2x3::Type::ALL);
IfcEntities getInverse(Ifc2x3::Type::Enum c, int i, const std::string& a);
void Load(std::vector<unsigned int>& ids, bool seek=false);
ArgumentPtr getArgument (unsigned int i);
std::string toString();
std::string datatype();
Ifc2x3::Type::Enum type();
bool is(Ifc2x3::Type::Enum v);
Ifc2x3::Type::Enum type() const;
bool is(Ifc2x3::Type::Enum v) const;
unsigned int id();
};
@@ -254,7 +257,7 @@ private:
static std::ostream* log2;
public:
static void SetOutput(std::ostream* l1, std::ostream* l2);
static void LogMessage(const std::string& type, const std::string& message, const SHARED_PTR<IfcAbstractEntity>& entity=SHARED_PTR<IfcAbstractEntity>());
static void LogMessage(const std::string& type, const std::string& message, const IfcAbstractEntityPtr entity=0);
static IfcParse::File* file;
static IfcParse::Tokens* tokens;
template <class T>
+9 -6
View File
@@ -18,6 +18,7 @@
********************************************************************************/
#include "IfcUtil.h"
#include <iostream>
void IfcEntityList::push(IfcUtil::IfcSchemaEntity l) {
if ( l ) ls.push_back(l);
@@ -48,14 +49,16 @@ IfcEntities IfcEntityList::getInverse(Ifc2x3::Type::Enum c, int ar, const std::s
return l;
}
bool IfcUtil::IfcEntitySelect::is(Ifc2x3::Type::Enum v) { return entity->is(v); }
Ifc2x3::Type::Enum IfcUtil::IfcEntitySelect::type() { return entity->type(); }
IfcUtil::IfcEntitySelect::IfcEntitySelect(SHARED_PTR<IfcBaseClass> b) { entity = b->entity; }
bool IfcUtil::IfcEntitySelect::is(Ifc2x3::Type::Enum v) const { return entity->is(v); }
Ifc2x3::Type::Enum IfcUtil::IfcEntitySelect::type() const { return entity->type(); }
IfcUtil::IfcEntitySelect::IfcEntitySelect(IfcSchemaEntity b) { entity = b->entity; }
IfcUtil::IfcEntitySelect::IfcEntitySelect(IfcAbstractEntityPtr e) { entity = e; }
bool IfcUtil::IfcEntitySelect::isSimpleType() { return false; }
IfcUtil::IfcEntitySelect::~IfcEntitySelect() { delete entity; }
bool IfcUtil::IfcArgumentSelect::is(Ifc2x3::Type::Enum v) { return _type == v; }
Ifc2x3::Type::Enum IfcUtil::IfcArgumentSelect::type() { return _type; }
bool IfcUtil::IfcArgumentSelect::is(Ifc2x3::Type::Enum v) const { return _type == v; }
Ifc2x3::Type::Enum IfcUtil::IfcArgumentSelect::type() const { return _type; }
IfcUtil::IfcArgumentSelect::IfcArgumentSelect(Ifc2x3::Type::Enum t, ArgumentPtr a) { _type = t; arg = a; }
ArgumentPtr IfcUtil::IfcArgumentSelect::wrappedValue() { return arg; }
bool IfcUtil::IfcArgumentSelect::isSimpleType() { return true; }
bool IfcUtil::IfcArgumentSelect::isSimpleType() { return true; }
IfcUtil::IfcArgumentSelect::~IfcArgumentSelect() { delete arg; }
+31 -23
View File
@@ -27,15 +27,17 @@
#include "../ifcparse/Ifc2x3enum.h"
class IfcAbstractEntity;
typedef SHARED_PTR<IfcAbstractEntity> IfcAbstractEntityPtr;
//typedef SHARED_PTR<IfcAbstractEntity> IfcAbstractEntityPtr;
typedef IfcAbstractEntity* IfcAbstractEntityPtr;
class IfcEntityList;
typedef SHARED_PTR<IfcEntityList> IfcEntities;
template <class F, class T>
inline SHARED_PTR<T> reinterpret_pointer_cast(SHARED_PTR<F> from) {
SHARED_PTR<void> v = std::tr1::static_pointer_cast<void,F>(from);
return std::tr1::static_pointer_cast<T,void>(v);
inline T* reinterpret_pointer_cast(F* from) {
return (T*)from;
//SHARED_PTR<void> v = std::tr1::static_pointer_cast<void,F>(from);
//return std::tr1::static_pointer_cast<T,void>(v);
}
namespace IfcUtil {
@@ -43,11 +45,12 @@ namespace IfcUtil {
class IfcBaseClass {
public:
IfcAbstractEntityPtr entity;
virtual bool is(Ifc2x3::Type::Enum v) = 0;
virtual Ifc2x3::Type::Enum type() = 0;
virtual bool is(Ifc2x3::Type::Enum v) const = 0;
virtual Ifc2x3::Type::Enum type() const = 0;
};
typedef SHARED_PTR<IfcBaseClass> IfcSchemaEntity;
//typedef SHARED_PTR<IfcBaseClass> IfcSchemaEntity;
typedef IfcBaseClass* IfcSchemaEntity;
}
@@ -67,10 +70,10 @@ public:
template <class T>
class IfcTemplatedEntityList {
std::vector<SHARED_PTR<T> > ls;
std::vector<T*> ls;
public:
typedef typename std::vector<SHARED_PTR<T> >::const_iterator it;
inline void push(SHARED_PTR<T> t) {if (t) ls.push_back(t);}
typedef typename std::vector<T*>::const_iterator it;
inline void push(T* t) {if (t) ls.push_back(t);}
inline void push(SHARED_PTR< IfcTemplatedEntityList<T> > t) { for ( typename T::it it = t->begin(); it != t->end(); ++it ) push(*it); }
inline it begin() { return ls.begin(); }
inline it end() { return ls.end(); }
@@ -79,34 +82,37 @@ public:
};
class Argument;
typedef SHARED_PTR<Argument> ArgumentPtr;
//typedef SHARED_PTR<Argument> ArgumentPtr;
typedef Argument* ArgumentPtr;
namespace IfcUtil {
class IfcAbstractSelect : public IfcBaseClass {
public:
typedef SHARED_PTR< IfcTemplatedEntityList<IfcAbstractSelect> > list;
typedef IfcTemplatedEntityList<IfcAbstractSelect>::it it;
typedef SHARED_PTR<IfcAbstractSelect> ptr;
typedef IfcAbstractSelect* ptr;
virtual bool isSimpleType() = 0;
};
class IfcEntitySelect : public IfcAbstractSelect {
public:
typedef SHARED_PTR<IfcEntitySelect> ptr;
IfcEntitySelect(SHARED_PTR<IfcUtil::IfcBaseClass> b);
typedef IfcEntitySelect* ptr;
IfcEntitySelect(IfcSchemaEntity b);
IfcEntitySelect(IfcAbstractEntityPtr e);
bool is(Ifc2x3::Type::Enum v);
Ifc2x3::Type::Enum type();
~IfcEntitySelect();
bool is(Ifc2x3::Type::Enum v) const;
Ifc2x3::Type::Enum type() const;
bool isSimpleType();
};
class IfcArgumentSelect : public IfcAbstractSelect {
Ifc2x3::Type::Enum _type;
ArgumentPtr arg;
public:
typedef SHARED_PTR<IfcArgumentSelect> ptr;
typedef IfcArgumentSelect* ptr;
IfcArgumentSelect(Ifc2x3::Type::Enum t, ArgumentPtr a);
~IfcArgumentSelect();
ArgumentPtr wrappedValue();
bool is(Ifc2x3::Type::Enum v);
Ifc2x3::Type::Enum type();
bool is(Ifc2x3::Type::Enum v) const;
Ifc2x3::Type::Enum type() const;
bool isSimpleType();
};
}
@@ -122,12 +128,13 @@ public:
virtual operator std::vector<int>() const = 0;
virtual operator std::vector<std::string>() const = 0;
virtual operator IfcUtil::IfcSchemaEntity() const = 0;
virtual operator SHARED_PTR<IfcUtil::IfcAbstractSelect>() const = 0;
//virtual operator IfcUtil::IfcAbstractSelect::ptr() const = 0;
virtual operator IfcEntities() const = 0;
virtual unsigned int Size() const = 0;
virtual ArgumentPtr operator [] (unsigned int i) const = 0;
virtual std::string toString() const = 0;
virtual bool isNull() const = 0;
virtual ~Argument() {};
};
class IfcAbstractEntity {
@@ -136,10 +143,11 @@ public:
virtual IfcEntities getInverse(Ifc2x3::Type::Enum c, int i, const std::string& a) = 0;
virtual std::string datatype() = 0;
virtual ArgumentPtr getArgument (unsigned int i) = 0;
virtual Ifc2x3::Type::Enum type() = 0;
virtual bool is(Ifc2x3::Type::Enum v) = 0;
virtual ~IfcAbstractEntity() {};
virtual Ifc2x3::Type::Enum type() const = 0;
virtual bool is(Ifc2x3::Type::Enum v) const = 0;
virtual std::string toString() = 0;
virtual unsigned int id() = 0;
};
#endif
#endif