diff --git a/wrap/ForwardDeclaration.h b/wrap/ForwardDeclaration.h new file mode 100644 index 000000000..2c9b6fa0c --- /dev/null +++ b/wrap/ForwardDeclaration.h @@ -0,0 +1,32 @@ +/* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + +/** + * @file Class.h + * @brief describe the C++ class that is being wrapped + * @author Frank Dellaert + * @author Andrew Melim + * @author Richard Roberts + **/ + +#pragma once + +#include + +namespace wrap { + + struct ForwardDeclaration { + std::string name; + bool isVirtual; + ForwardDeclaration() : isVirtual(false) {} + }; + +} \ No newline at end of file diff --git a/wrap/TemplateInstantiationTypedef.cpp b/wrap/TemplateInstantiationTypedef.cpp new file mode 100644 index 000000000..da1e8f616 --- /dev/null +++ b/wrap/TemplateInstantiationTypedef.cpp @@ -0,0 +1,61 @@ +/* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + +/** + * @file Class.cpp + * @author Frank Dellaert + * @author Andrew Melim + * @author Richard Roberts + **/ + +#include "TemplateInstantiationTypedef.h" + +#include "utilities.h" + +using namespace std; + +namespace wrap { + + Class TemplateInstantiationTypedef::findAndExpand(const vector& classes) const { + // Find matching class + std::vector::const_iterator clsIt = classes.end(); + for(std::vector::const_iterator it = classes.begin(); it != classes.end(); ++it) { + if(it->name == className && it->namespaces == classNamespaces && it->templateArgs.size() == typeList.size()) { + clsIt = it; + break; + } + } + + if(clsIt == classes.end()) + throw DependencyMissing(wrap::qualifiedName("::", classNamespaces, className), + "instantiation into typedef name " + wrap::qualifiedName("::", namespaces, name) + + ". Ensure that the typedef provides the correct number of template arguments."); + + // Instantiate it + Class classInst = *clsIt; + for(size_t i = 0; i < typeList.size(); ++i) + classInst = classInst.expandTemplate(classInst.templateArgs[i], typeList[i]); + + // Fix class properties + classInst.name = name; + classInst.templateArgs.clear(); + classInst.typedefName = clsIt->qualifiedName("::") + "<"; + if(typeList.size() > 0) + classInst.typedefName += wrap::qualifiedName("::", typeList[0]); + for(size_t i = 1; i < typeList.size(); ++i) + classInst.typedefName += (", " + wrap::qualifiedName("::", typeList[i])); + classInst.typedefName += ">"; + classInst.namespaces = namespaces; + + return classInst; + } + +} \ No newline at end of file diff --git a/wrap/TemplateInstantiationTypedef.h b/wrap/TemplateInstantiationTypedef.h new file mode 100644 index 000000000..192d35324 --- /dev/null +++ b/wrap/TemplateInstantiationTypedef.h @@ -0,0 +1,39 @@ +/* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + +/** + * @file Class.h + * @brief describe the C++ class that is being wrapped + * @author Frank Dellaert + * @author Andrew Melim + * @author Richard Roberts + **/ + +#pragma once + +#include +#include + +#include "Class.h" + +namespace wrap { + +struct TemplateInstantiationTypedef { + std::vector classNamespaces; + std::string className; + std::vector namespaces; + std::string name; + std::vector > typeList; + + Class findAndExpand(const std::vector& classes) const; +}; + +} \ No newline at end of file diff --git a/wrap/TypeAttributesTable.cpp b/wrap/TypeAttributesTable.cpp new file mode 100644 index 000000000..865015a6e --- /dev/null +++ b/wrap/TypeAttributesTable.cpp @@ -0,0 +1,58 @@ +/* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + +/** + * @file Class.cpp + * @author Frank Dellaert + * @author Andrew Melim + * @author Richard Roberts + **/ + +#include "TypeAttributesTable.h" +#include "Class.h" +#include "utilities.h" + +#include + +using namespace std; + +namespace wrap { + + /* ************************************************************************* */ + void TypeAttributesTable::addClasses(const vector& classes) { + BOOST_FOREACH(const Class& cls, classes) { + if(!insert(make_pair(cls.qualifiedName("::"), TypeAttributes(cls.isVirtual))).second) + throw DuplicateDefinition("class " + cls.qualifiedName("::")); + } + } + + /* ************************************************************************* */ + void TypeAttributesTable::addForwardDeclarations(const vector& forwardDecls) { + BOOST_FOREACH(const ForwardDeclaration& fwDec, forwardDecls) { + if(!insert(make_pair(fwDec.name, TypeAttributes(fwDec.isVirtual))).second) + throw DuplicateDefinition("class " + fwDec.name); + } + } + + /* ************************************************************************* */ + void TypeAttributesTable::checkValidity(const vector& classes) const { + BOOST_FOREACH(const Class& cls, classes) { + // Check that class is virtual if it has a parent + if(!cls.qualifiedParent.empty() && !cls.isVirtual) + throw AttributeError(cls.qualifiedName("::"), "Has a base class so needs to be declared virtual, change to 'virtual class "+cls.name+" ...'"); + // Check that parent is virtual as well + if(!cls.qualifiedParent.empty() && !at(wrap::qualifiedName("::", cls.qualifiedParent)).isVirtual) + throw AttributeError(wrap::qualifiedName("::", cls.qualifiedParent), + "Is the base class of " + cls.qualifiedName("::") + ", so needs to be declared virtual"); + } + } + +} \ No newline at end of file diff --git a/wrap/TypeAttributesTable.h b/wrap/TypeAttributesTable.h new file mode 100644 index 000000000..dea6d0bf5 --- /dev/null +++ b/wrap/TypeAttributesTable.h @@ -0,0 +1,55 @@ +/* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + +/** + * @file Class.h + * @brief describe the C++ class that is being wrapped + * @author Frank Dellaert + * @author Andrew Melim + * @author Richard Roberts + **/ + +#include +#include +#include + +#include "ForwardDeclaration.h" + +#pragma once + +namespace wrap { + + // Forward declarations + struct Class; + +/** Attributes about valid classes, both for classes defined in this module and + * also those forward-declared from others. At the moment this only contains + * whether the class is virtual, which is used to know how to copy the class, + * and whether to try to convert it to a more derived type upon returning it. + */ +struct TypeAttributes { + bool isVirtual; + TypeAttributes() : isVirtual(false) {} + TypeAttributes(bool isVirtual) : isVirtual(isVirtual) {} +}; + +/** Map of type names to attributes. */ +class TypeAttributesTable : public std::map { +public: + TypeAttributesTable() {} + + void addClasses(const std::vector& classes); + void addForwardDeclarations(const std::vector& forwardDecls); + + void checkValidity(const std::vector& classes) const; +}; + +} \ No newline at end of file