Skip to content

Commit

Permalink
Merge pull request #18472 from ianna/dd-element-registry-cleanup
Browse files Browse the repository at this point in the history
DD Element Registry Cleanup
  • Loading branch information
davidlange6 committed Apr 26, 2017
2 parents 588aa71 + 542f708 commit f536883
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 146 deletions.
29 changes: 10 additions & 19 deletions DetectorDescription/Parser/interface/DDLElementRegistry.h
@@ -1,28 +1,21 @@
#ifndef DDL_ElementRegistry_H
#define DDL_ElementRegistry_H

#include <string>
#include <map>
#ifndef DETECTOR_DESCRIPTION_PARSER_DDL_ELEMENT_REGISTRY_H
#define DETECTOR_DESCRIPTION_PARSER_DDL_ELEMENT_REGISTRY_H

#include "DetectorDescription/Base/interface/Singleton.h"
#include "DetectorDescription/Base/interface/Singleton.icc"

class DDXMLElement;
#include "DetectorDescription/ExprAlgo/interface/ExprEvalSingleton.h"

#include <CLHEP/Evaluator/Evaluator.h>
#include "DetectorDescription/ExprAlgo/interface/ExprEvalSingleton.h"
#include <string>
#include <map>
#include <memory>

class DDXMLElement;

/// The main class for processing parsed elements.
/** \class DDLElementRegistry
*
*
* DDLElementRegistry.h - description
* -------------------
* begin : Wed Oct 24 2001
* email : case@ucdhep.ucdavis.edu
*
* This class is designed to serve as a registry of all DDL XML elements.
* It inherits from DDXMLElementRegistry.
*
* This class is responsible for constructing and destructing
* any necessary DDL element.
Expand All @@ -33,7 +26,7 @@ class DDLElementRegistry
{

public:
typedef std::map <std::string, DDXMLElement*> RegistryMap;
typedef std::map <std::string, std::shared_ptr<DDXMLElement> > RegistryMap;

DDLElementRegistry();

Expand All @@ -48,10 +41,8 @@ class DDLElementRegistry
* return a pointer if already registered or NULL, no instantiating.
*
*/
DDXMLElement* getElement(const std::string& name);
std::shared_ptr<DDXMLElement> getElement(const std::string& name);

/// Get the name given a pointer. This may not be needed...
const std::string& getElementName(DDXMLElement* theElement) const;
ClhepEvaluator &evaluator() { return ExprEvalSingleton::instance(); }

private:
Expand Down
17 changes: 9 additions & 8 deletions DetectorDescription/Parser/src/DDLAlgorithm.cc
Expand Up @@ -29,11 +29,11 @@ DDLAlgorithm::preProcessElement( const std::string& name, const std::string& nms
void
DDLAlgorithm::processElement( const std::string& name, const std::string& nmspace, DDCompactView& cpv )
{
DDXMLElement* myNumeric = myRegistry_->getElement( "Numeric" );
DDXMLElement* myString = myRegistry_->getElement( "String" );
DDXMLElement* myVector = myRegistry_->getElement( "Vector" );
DDXMLElement* myMap = myRegistry_->getElement( "Map" );
DDXMLElement* myrParent = myRegistry_->getElement( "rParent" );
auto myNumeric = myRegistry_->getElement( "Numeric" );
auto myString = myRegistry_->getElement( "String" );
auto myVector = myRegistry_->getElement( "Vector" );
auto myMap = myRegistry_->getElement( "Map" );
auto myrParent = myRegistry_->getElement( "rParent" );

DDName algoName( getDDName( nmspace ));
DDLogicalPart lp( DDName( myrParent->getDDName( nmspace )));
Expand All @@ -57,9 +57,10 @@ DDLAlgorithm::processElement( const std::string& name, const std::string& nmspac

DDAlgorithmHandler handler;
atts = getAttributeSet();
DDLVector* tv = dynamic_cast<DDLVector*>( myVector );
DDLMap* tm = dynamic_cast<DDLMap*>( myMap );
handler.initialize( algoName, lp, nArgs, tv->getMapOfVectors(), tm->getMapOfMaps(), sArgs, tv->getMapOfStrVectors());
handler.initialize( algoName, lp, nArgs,
static_cast<DDLVector*>( myVector.get())->getMapOfVectors(),
static_cast<DDLMap*>( myMap.get())->getMapOfMaps(), sArgs,
static_cast<DDLVector*>( myVector.get())->getMapOfStrVectors());
handler.execute( cpv );

// clear used/referred to elements.
Expand Down
12 changes: 6 additions & 6 deletions DetectorDescription/Parser/src/DDLBooleanSolid.cc
Expand Up @@ -32,9 +32,9 @@ DDLBooleanSolid::processElement( const std::string& name, const std::string& nms
// <UnionSolid name="bs" firstSolid="blah" secondSolid="argh"> <Translation...> <rRotation .../> </UnionSolid
// AND <UnionSolid> <rSolid...> <rSolid...> <Translation...> <rRotation...> </UnionSolid>

DDXMLElement* myrSolid = myRegistry_->getElement( "rSolid" ); // get rSolid children
DDXMLElement* myTranslation = myRegistry_->getElement( "Translation" ); // get Translation child
DDXMLElement* myrRotation = myRegistry_->getElement( "rRotation" ); // get rRotation child
auto myrSolid = myRegistry_->getElement( "rSolid" ); // get rSolid children
auto myTranslation = myRegistry_->getElement( "Translation" ); // get Translation child
auto myrRotation = myRegistry_->getElement( "rRotation" ); // get rRotation child

ClhepEvaluator & ev = myRegistry_->evaluator();
DDXMLAttribute atts = getAttributeSet();
Expand Down Expand Up @@ -133,9 +133,9 @@ DDLBooleanSolid::dumpBooleanSolid( const std::string& name, const std::string& n
if (atts.find("secondSolid") != atts.end()) s+= " secondSolid=\"" + atts.find("secondSolid")->second + "\"";
s += ">\n";

DDXMLElement* myrSolid = myRegistry_->getElement("rSolid"); // get rSolid children
DDXMLElement* myTranslation = myRegistry_->getElement("Translation"); // get Translation child
DDXMLElement* myrRotation = myRegistry_->getElement("rRotation"); // get rRotation child
auto myrSolid = myRegistry_->getElement("rSolid"); // get rSolid children
auto myTranslation = myRegistry_->getElement("Translation"); // get Translation child
auto myrRotation = myRegistry_->getElement("rRotation"); // get rRotation child
if (myrSolid->size() > 0)
{
for (size_t i = 0; i < myrSolid->size(); ++i)
Expand Down
4 changes: 2 additions & 2 deletions DetectorDescription/Parser/src/DDLCompositeMaterial.cc
Expand Up @@ -39,8 +39,8 @@ DDLCompositeMaterial::processElement( const std::string& name, const std::string
mat = DDMaterial( ddn, ev.eval( nmspace, atts.find( "density" )->second ));

// Get references to relevant DDL elements that are needed.
DDXMLElement* myMF = myRegistry_->getElement( "MaterialFraction" );
DDXMLElement* myrMaterial = myRegistry_->getElement( "rMaterial" );
auto myMF = myRegistry_->getElement( "MaterialFraction" );
auto myrMaterial = myRegistry_->getElement( "rMaterial" );

// Get the names from those elements and also the namespace for the reference element.
// The parent element CompositeMaterial MUST be in the same namespace as this fraction.
Expand Down
98 changes: 37 additions & 61 deletions DetectorDescription/Parser/src/DDLElementRegistry.cc
Expand Up @@ -44,150 +44,136 @@ DDLElementRegistry::DDLElementRegistry( void )

DDLElementRegistry::~DDLElementRegistry( void )
{
// Complicated cleanup. I keep track of DDXMLElements that have
// already been deleted using this vector. Then delete them one-by-one.
std::vector<DDXMLElement*> toDelete;
for( RegistryMap::const_iterator it = registry_.begin(), end = registry_.end(); it != end; ++it )
{
std::vector<DDXMLElement*>::const_iterator deleteIt = std::find( toDelete.begin(), toDelete.end(), it->second );
if( deleteIt == toDelete.end())
{
toDelete.push_back( it->second );
delete it->second;
}
}
registry_.clear();
}

// -------------------------------------------------------------------------
// Implementation
// -------------------------------------------------------------------------
DDXMLElement*
std::shared_ptr<DDXMLElement>
DDLElementRegistry::getElement( const std::string& name )
{
RegistryMap::iterator it = registry_.find(name);
DDXMLElement* myret = NULL;
RegistryMap::iterator it = registry_.find( name );
std::shared_ptr<DDXMLElement> myret( nullptr );
if( it != registry_.end())
{
myret = it->second;
return it->second;
} else {
// Make the Solid handlers and register them.
if (name == "Box")
{
myret = new DDLBox(this);
myret = std::make_shared<DDLBox>(this);
}
else if (name == "Cone")
{
myret = new DDLCone(this);
myret = std::make_shared<DDLCone>(this);
}
else if (name == "Polyhedra" || name == "Polycone")
{
myret = new DDLPolyGenerator(this);
myret = std::make_shared<DDLPolyGenerator>(this);
}
else if (name == "Trapezoid" || name == "Trd1")
{
myret = new DDLTrapezoid(this);
myret = std::make_shared<DDLTrapezoid>(this);
}
else if (name == "PseudoTrap")
{
myret = new DDLPseudoTrap(this);
myret = std::make_shared<DDLPseudoTrap>(this);
}
else if (name == "Tubs" || name == "CutTubs" || name == "Tube" || name == "TruncTubs")
{
myret = new DDLTubs(this);
myret = std::make_shared<DDLTubs>(this);
}
else if (name == "Torus")
{
myret = new DDLTorus(this);
myret = std::make_shared<DDLTorus>(this);
}
else if (name == "ReflectionSolid")
{
myret = new DDLReflectionSolid(this);
myret = std::make_shared<DDLReflectionSolid>(this);
}
else if (name == "UnionSolid" || name == "SubtractionSolid"
|| name == "IntersectionSolid")
{
myret = new DDLBooleanSolid(this);
myret = std::make_shared<DDLBooleanSolid>(this);
}
else if (name == "ShapelessSolid")
{
myret = new DDLShapelessSolid(this);
myret = std::make_shared<DDLShapelessSolid>(this);
}
else if (name == "Sphere")
{
myret = new DDLSphere(this);
myret = std::make_shared<DDLSphere>(this);
}
else if (name == "Orb")
{
myret = new DDLOrb(this);
myret = std::make_shared<DDLOrb>(this);
}
else if (name == "EllipticalTube")
{
myret = new DDLEllipticalTube(this);
myret = std::make_shared<DDLEllipticalTube>(this);
}
else if (name == "Ellipsoid")
{
myret = new DDLEllipsoid(this);
myret = std::make_shared<DDLEllipsoid>(this);
}
else if (name == "Parallelepiped")
{
myret = new DDLParallelepiped(this);
myret = std::make_shared<DDLParallelepiped>(this);
}

// LogicalParts, Positioners, Materials, Rotations, Reflections
// and Specific (Specified?) Parameters
else if (name == "PosPart")
{
myret = new DDLPosPart(this);
myret = std::make_shared<DDLPosPart>(this);
}
else if (name == "CompositeMaterial")
{
myret = new DDLCompositeMaterial(this);
myret = std::make_shared<DDLCompositeMaterial>(this);
}
else if (name == "ElementaryMaterial")
{
myret = new DDLElementaryMaterial(this);
myret = std::make_shared<DDLElementaryMaterial>(this);
}
else if (name == "LogicalPart")
{
myret = new DDLLogicalPart(this);
myret = std::make_shared<DDLLogicalPart>(this);
}
else if (name == "ReflectionRotation" || name == "Rotation" )
{
myret = new DDLRotationAndReflection(this);
myret = std::make_shared<DDLRotationAndReflection>(this);
}
else if (name == "SpecPar")
{
myret = new DDLSpecPar(this);
myret = std::make_shared<DDLSpecPar>(this);
}
else if (name == "RotationSequence")
{
myret = new DDLRotationSequence(this);
myret = std::make_shared<DDLRotationSequence>(this);
}
else if (name == "RotationByAxis")
{
myret = new DDLRotationByAxis(this);
myret = std::make_shared<DDLRotationByAxis>(this);
}
// Special, need them around.
else if (name == "SpecParSection") {
myret = new DDXMLElement(this, true);
myret = std::make_shared<DDXMLElement>(this, true);
}
else if (name == "Vector") {
myret = new DDLVector(this);
myret = std::make_shared<DDLVector>(this);
}
else if (name == "Map") {
myret = new DDLMap(this);
myret = std::make_shared<DDLMap>(this);
}
else if (name == "String") {
myret = new DDLString(this);
myret = std::make_shared<DDLString>(this);
}
else if (name == "Numeric") {
myret = new DDLNumeric(this);
myret = std::make_shared<DDLNumeric>(this);
}
else if (name == "Algorithm") {
myret = new DDLAlgorithm(this);
myret = std::make_shared<DDLAlgorithm>(this);
}
else if (name == "Division") {
myret = new DDLDivision(this);
myret = std::make_shared<DDLDivision>(this);
}

// Supporting Cast of elements.
Expand All @@ -202,7 +188,7 @@ DDLElementRegistry::getElement( const std::string& name )
|| name == "rRotation" || name == "rReflectionRotation"
|| name == "DDDefinition" )
{
myret = new DDXMLElement(this);
myret = std::make_shared<DDXMLElement>(this);
}

// IF it is a new element return a default XMLElement which processes nothing.
Expand All @@ -213,7 +199,7 @@ DDLElementRegistry::getElement( const std::string& name )
// XML elements of the DDLSchema are taken care of by this.
else
{
myret = new DDXMLElement(this);
myret = std::make_shared<DDXMLElement>(this);
}

// Actually register the thing
Expand All @@ -222,14 +208,4 @@ DDLElementRegistry::getElement( const std::string& name )
return myret;
}

const std::string&
DDLElementRegistry::getElementName( DDXMLElement* theElement ) const
{
for( RegistryMap::const_iterator it = registry_.begin(), end = registry_.end(); it != end; ++it )
if( it->second == theElement )
return it->first;
return registry_.find( "***" )->first;
}

template class DDI::Singleton<DDLElementRegistry>;

4 changes: 2 additions & 2 deletions DetectorDescription/Parser/src/DDLLogicalPart.cc
Expand Up @@ -46,8 +46,8 @@ void
DDLLogicalPart::processElement( const std::string& name, const std::string& nmspace, DDCompactView& cpv )
{
// rMaterial and rSolid
DDXMLElement* myrMaterial = myRegistry_->getElement("rMaterial"); // get Material reference child
DDXMLElement* myrSolid = myRegistry_->getElement("rSolid"); // get Solid reference child
auto myrMaterial = myRegistry_->getElement("rMaterial"); // get Material reference child
auto myrSolid = myRegistry_->getElement("rSolid"); // get Solid reference child

DDXMLAttribute atts = getAttributeSet();

Expand Down
6 changes: 3 additions & 3 deletions DetectorDescription/Parser/src/DDLMap.cc
Expand Up @@ -46,21 +46,21 @@ template <typename ScannerT> struct Mapper::definition
void
MapPair::operator() (char const* str, char const* end) const
{
DDLMap* myDDLMap = dynamic_cast < DDLMap* > (DDLGlobalRegistry::instance().getElement("Map"));
std::shared_ptr<DDLMap> myDDLMap = std::static_pointer_cast<DDLMap>(DDLGlobalRegistry::instance().getElement("Map"));
myDDLMap->do_pair(str, end);
}

void
MapMakeName::operator() (char const* str, char const* end) const
{
DDLMap* myDDLMap = dynamic_cast < DDLMap* > (DDLGlobalRegistry::instance().getElement("Map"));
std::shared_ptr<DDLMap> myDDLMap = std::static_pointer_cast<DDLMap>(DDLGlobalRegistry::instance().getElement("Map"));
myDDLMap->do_makeName(str, end);
}

void
MapMakeDouble::operator() (char const* str, char const* end)const
{
DDLMap* myDDLMap = dynamic_cast < DDLMap* > (DDLGlobalRegistry::instance().getElement("Map"));
std::shared_ptr<DDLMap> myDDLMap = std::static_pointer_cast<DDLMap>(DDLGlobalRegistry::instance().getElement("Map"));
myDDLMap->do_makeDouble(str, end);
}

Expand Down
2 changes: 1 addition & 1 deletion DetectorDescription/Parser/src/DDLMaterial.cc
Expand Up @@ -22,7 +22,7 @@ DDLMaterial::setReference( const std::string& nmspace, DDCompactView& cpv )
// Attempt to make sure Material elements can be in LogicalPart elements.
if (myRegistry_->getElement("LogicalPart")->size() > 0)
{
DDXMLElement* refmat = myRegistry_->getElement("rMaterial");
auto refmat = myRegistry_->getElement("rMaterial");
std::vector<std::string> names;
std::vector<std::string> values;
names.push_back("name");
Expand Down

0 comments on commit f536883

Please sign in to comment.