[solved] can nested namespace be used in generate_from_xml?

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

[solved] can nested namespace be used in generate_from_xml?

Post by DeepSOIC »

Hi
This xml:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<GenerateModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="generateMetaModel_Module.xsd">
  <PythonExport 
	  Father="ParaGeometryPy" 
	  Name="ParaPointPy" 
	  Twin="ParaPoint" 
	  TwinPointer="ParaPoint" 
	  Include="Mod/ConstraintSolver/App/G2D/ParaPoint.h" 
	  FatherInclude="Mod/ConstraintSolver/App/ParaGeometryPy.h" 
	  Namespace="FCS::G2D" 
	  Constructor="true"
	  Delete="true"
	  FatherNamespace="FCS"
	  DisableNotify="true">
	<Documentation>
	  <Author Licence="LGPL" Name="DeepSOIC" EMail="vv.titov@gmail.com" />
	  <DeveloperDocu>2D point</DeveloperDocu>
	  <UserDocu>2D point</UserDocu>
	</Documentation>
    
    
  </PythonExport>
</GenerateModel>
...produces an invalid ParaPoint.h, with this faulty header guard:

Code: Select all

#ifndef FCS::G2D_PARAPOINTPY_H
#define FCS::G2D_PARAPOINTPY_H
...
Is there a way to trick it into working with the c++ class defined in FCS::G2D? There will be colliding names in FCS::G3D namespace, so I have a few options:
* do not nest (i.e., use FCSG2D and FCSG3D namespaces)
* no namespaces for 2d and 3d, just add prefix/suffix to all class names
all seem slightly inelegant...
and final,
* fix generate model. But how? It's a weird contraption of a python program that takes an xml (xsd) and generates (another) python program that takes an xml and generates a C++ file. Why on eartch is that meta-generator needed is beyond my understanding... and figuring out how to bend all this stuff seems tricky...
Last edited by DeepSOIC on Wed Nov 20, 2019 7:39 pm, edited 1 time in total.
wmayer
Founder
Posts: 20324
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: can nested namespace be used in generate_from_xml?

Post by wmayer »

I think the only sensible way to handle it is to modify the Python generation script to handle this situation correctly. I will have a look later today...
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: can nested namespace be used in generate_from_xml?

Post by DeepSOIC »

thanks in advance!
another problem with the header:

Code: Select all

/** The python export class for ParaPoint
 */
class FCS::G2DExport ParaPointPy : public FCS::ParaGeometryPy
{
FCS::G2DExport should be FCSExport, or whatever that doesn't have :: in it

I have manually fixed the generated header file and committed it to https://github.com/DeepSOIC/FreeCAD-ell ... intSolver1
The generate_from_xml that causes problems is commented out. You can use this branch to try it. It probably won't build on Linux, as I didn't even try; it certainly won't build with Py2 (but commenting out some Py3-only code in AppConstraintSolver.cpp should help)
wmayer
Founder
Posts: 20324
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: can nested namespace be used in generate_from_xml?

Post by wmayer »

There are altogether three things that must be changed:
  1. The file guard from FCS::G2D_PARAPOINTPY_H to FCS_G2D_PARAPOINTPY_H
    This is very easy to fix with @self.export.Namespace.upper().replace("::", "_")@ inside templateClassPyExport.py
  2. The same for the export macro with @self.export.Namespace.replace("::","_")@
  3. The name space in the header file
The last point is the tricky part because in the header the declaration cannot be:

Code: Select all

namespace FCS::G2D
{
...
}
Instead it must be

Code: Select all

namespace FCS
{
namespace G2D
{
...
}
}
For the opening curly brace one could use @self.export.Namespace.replace("::"," {\nnamespace ")@ and for the second closing curly brace

Code: Select all

@"}"*len(self.export.Namespace.split("::"))@
might work.

Attached is the modified script. Can you test please if this creates valid C++ code?
Attachments
templateClassPyExport.py
(46.81 KiB) Downloaded 157 times
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: can nested namespace be used in generate_from_xml?

Post by DeepSOIC »

wmayer wrote: Wed Nov 20, 2019 6:27 pm Attached is the modified script. Can you test please if this creates valid C++ code?

Code: Select all

FAILED: src/Base/VectorPy.h src/Base/VectorPy.cpp 
cmd.exe /C "cd /D S:\_vt\dev\PC\Qt\FreeCAD\FreeCAD-ellipse\src\Base && S:\_vt\dev\PC\Qt\FreeCAD\libpack\12.1.2\bin\python.exe S:\_vt\dev\PC\Qt\FreeCAD\FreeCAD-ellipse\src\Tools\generate.py --outputPath S:\_vt\dev\PC\Qt\FreeCAD\build-release\src\Base VectorPy.xml"
Traceback (most recent call last):
  File "S:\_vt\dev\PC\Qt\FreeCAD\FreeCAD-ellipse\src\Tools\generate.py", line 100, in <module>
    main()
  File "S:\_vt\dev\PC\Qt\FreeCAD\FreeCAD-ellipse\src\Tools\generate.py", line 96, in main
    generate(filename,defaultPath)
  File "S:\_vt\dev\PC\Qt\FreeCAD\FreeCAD-ellipse\src\Tools\generate.py", line 50, in generate
    Export.Generate()
  File "S:\_vt\dev\PC\Qt\FreeCAD\FreeCAD-ellipse\src\Tools\generateTemplates\templateClassPyExport.py", line 33, in Generate
    generateBase.generateTools.replace(self.TemplateModule,locals(),file)
  File "S:\_vt\dev\PC\Qt\FreeCAD\FreeCAD-ellipse\src\Tools\generateBase\generateTools.py", line 134, in replace
    cop.copy(lines_block)
  File "S:\_vt\dev\PC\Qt\FreeCAD\FreeCAD-ellipse\src\Tools\generateBase\generateTools.py", line 121, in copy
    self.copyblock()
  File "S:\_vt\dev\PC\Qt\FreeCAD\FreeCAD-ellipse\src\Tools\generateBase\generateTools.py", line 95, in copyblock
    temporary_exec(stat, self.globals, self.locals)
  File "S:\_vt\dev\PC\Qt\FreeCAD\FreeCAD-ellipse\src\Tools\generateBase\generateTools.py", line 21, in temporary_exec
    __exec_new__(text, globals, locals)
  File "S:\_vt\dev\PC\Qt\FreeCAD\FreeCAD-ellipse\src\Tools\generateBase\__exec_new.py", line 4, in __exec_new__
    exec(text, globals, locals)
  File "<string>", line 1, in <module>
  File "S:\_vt\dev\PC\Qt\FreeCAD\FreeCAD-ellipse\src\Tools\generateBase\generateTools.py", line 95, in copyblock
    temporary_exec(stat, self.globals, self.locals)
  File "S:\_vt\dev\PC\Qt\FreeCAD\FreeCAD-ellipse\src\Tools\generateBase\generateTools.py", line 21, in temporary_exec
    __exec_new__(text, globals, locals)
  File "S:\_vt\dev\PC\Qt\FreeCAD\FreeCAD-ellipse\src\Tools\generateBase\__exec_new.py", line 4, in __exec_new__
    exec(text, globals, locals)
  File "<string>", line 2, in <module>
AttributeError: 'Methode' object has no attribute 'Class'
and a whole load of other similar looking errors.

Is it enough that I just replaced the file in src\Tools\generateTemplates\templateClassPyExport.py, or should I run something?
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: can nested namespace be used in generate_from_xml?

Post by DeepSOIC »

and this is the content of ParaPointPy.h, in full:

Code: Select all

// This file is generated by src/Tools/generateTemaplates/templateClassPyExport.py out of the XML file
// Every change you make here gets lost in the next full rebuild!
#ifndef FCS_G2D_PARAPOINTPY_H
#define FCS_G2D_PARAPOINTPY_H

#include <Mod/ConstraintSolver/App/ParaGeometryPy.h>
#include <Mod/ConstraintSolver/App/G2D/ParaPoint.h>
#include <string>

User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: can nested namespace be used in generate_from_xml?

Post by DeepSOIC »

I probably need to rebase
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: can nested namespace be used in generate_from_xml?

Post by DeepSOIC »

rebased, seems to make Qt's code analyzer happy! Thanks! Will take a while to build...
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: can nested namespace be used in generate_from_xml?

Post by DeepSOIC »

Fan-freaking-tastik, thanks Werner!
Now that I look at the changes, I feel like I could have done it myself :oops:
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: [solved] can nested namespace be used in generate_from_xml?

Post by DeepSOIC »

Are you committing it to master? or should I just commit it to constraintsolver branch?
Post Reply