How can I make SourceInsight understand smart pointers? - c++

If my code uses smart pointers, navigation and completion don't work with SourceInsight. For example with this simple example code:
class test {
public:
void fun(){}
};
int main() {
boost::scoped_ptr<test> a;
a->fun();
return 0;
}
When I click fun() in main , SourceInsight tells me "symbol not found".
Is there something I should do to fix that, or does SourceInsight have limitations?

So I've given this a try. Downloaded, virus-scanned, signed up for free trial, installed wine, run!
Now, I can make SourceInsight import the symbols from the Boost directories:
I then enter the path to my Boost directory: Z:\home\sehe\custom\boost\
Processing takes a minute or two:
Seems ok:
Now if you import these symbols in your project:
You will find you can navigate to boost::scoped_ptr<> just fine (I double-clicked on the a in the declaration of a):
But it will not find references to fun:
Finds only the declaration:
Of course you can use simple string search, but that wasn't the point.
Here's the HTML export: https://s3.amazonaws.com/stackoverflow-sehe/f14b4ee2-9b4d-49cd-893d-cf69372dd586/HTML/test.cpp.html
Conclusion
It seems this is a limitation of SourceInsight, not due to misconfiguration.

Related

Custom submodules in pytorch / libtorch C++

Full disclosure, I asked this same question on the PyTorch forums about a few days ago and got no reply, so this is technically a repost, but I believe it's still a good question, because I've been unable to find an answer anywhere online. Here goes:
Can you show an example of using register_module with a custom module?
The only examples I’ve found online are registering linear layers or convolutional layers as the submodules.
I tried to write my own module and register it with another module and I couldn’t get it to work.
My IDE is telling me no instance of overloaded function "MyModel::register_module" matches the argument list -- argument types are: (const char [14], TreeEmbedding)
(TreeEmbedding is the name of another struct I made which extends torch::nn::Module.)
Am I missing something? An example of this would be very helpful.
Edit: Additional context follows below.
I have a header file "model.h" which contains the following:
struct TreeEmbedding : torch::nn::Module {
TreeEmbedding();
torch::Tensor forward(Graph tree);
};
struct MyModel : torch::nn::Module{
size_t embeddingSize;
TreeEmbedding treeEmbedding;
MyModel(size_t embeddingSize=10);
torch::Tensor forward(std::vector<Graph> clauses, std::vector<Graph> contexts);
};
I also have a cpp file "model.cpp" which contains the following:
MyModel::MyModel(size_t embeddingSize) :
embeddingSize(embeddingSize)
{
treeEmbedding = register_module("treeEmbedding", TreeEmbedding{});
}
This setup still has the same error as above. The code in the documentation does work (using built-in components like linear layers), but using a custom module does not. After tracking down torch::nn::Linear, it looks as though that is a ModuleHolder (Whatever that is...)
Thanks,
Jack
I will accept a better answer if anyone can provide more details, but just in case anyone's wondering, I thought I would put up the little information I was able to find:
register_module takes in a string as its first argument and its second argument can either be a ModuleHolder (I don't know what this is...) or alternatively it can be a shared_ptr to your module. So here's my example:
treeEmbedding = register_module<TreeEmbedding>("treeEmbedding", make_shared<TreeEmbedding>());
This seemed to work for me so far.

How to configure Eclipse/CDT/C++ formatter to not break line between a function returned type and the function name [duplicate]

I ran into a problem with the Eclipse formatter. It won't format my code correctly when declaring methods within a class declaration. It puts a new line after the method's return type.
I already exported the style xml file and examined the settings in it, but none of the settings have any apparent connection to this problem, and the settings editor in Eclipse didn't show the same problem happening in it's sample code for method declarations.
Here is an example bit of code for what I want to have happen:
class MyClass
{
public:
MyClass();
void myMethod();
};
However, this is what I get:
class MyClass
{
public:
MyClass();
void
myMethod();
};
Again, in the styles editor, the code doesn't have this problem and looks just how I want it to, but in the actual code, the story is different.
I'm using version 3.8.0. Any help is appreciated.
Edit: I deleted those source files that were formatted incorrectly (after formatting the code several times to no avail) and replaced them with "identical" files with the same methods, same structure, etc. I formatted the code this time and it worked. This is probably a bug, but I'm leaving it up just in case anyone else encounters a similar problem or has a solution to avoiding this problem in the first place.
I hand edited two files under the main eclipse projects directory
.metadata\.plugins\org.eclipse.core.runtime\.settings
The two files:
file 1: org.eclipse.cdt.core.prefs, change this line from "insert" to "do not insert"
org.eclipse.cdt.core.formatter.insert_new_line_before_identifier_in_function_declaration=do not insert
file 2: org.eclipse.cdt.ui.prefs,
scan this file for "insert_new_line_before_identifier_in_function_declaration" and make a similar change from insert to do not insert next to it, should be obvious
Note I seen this problem on indigo and juno, the fix described above was in juno.
If you have a custom formatter config, export it first (settings>C/C++ General>Formatter>Edit>Export). Then change the following line to "do not insert". Save the XML.
<setting id="org.eclipse.cdt.core.formatter.insert_new_line_before_identifier_in_function_declaration" value="do not insert"/>
Delete the current config and import the one you changed.
There's a specific preference in the formatter options starting from cdt 9.8 included in Eclipse 2019-06.

How to make a system call and read the stdout, in D?

I thought to try using D for some system administration scripts which require high performance (for comparing performance with python/perl etc).
I can't find an example in the tutorials I looked through so far (dsource.org etc.) on how to make a system call (i.e. calling another software) and receiving it's output from stdout, though?
If I missed it, could someone point me to the right docs/tutorial, or provide with the answer right away?
Well, then I of course found it: http://www.digitalmars.com/d/2.0/phobos/std_process.html#shell (Version using the Tango library here: http://www.dsource.org/projects/tango/wiki/TutExec).
The former version is the one that works with D 2.0 (thereby the current dmd compiler that comes with ubuntu).
I got this tiny example to work now, compiled with dmd:
import std.stdio;
import std.process;
void main() {
string output = shell("ls -l");
write(output);
}
std.process has been updated since... the new function is spawnShell
import std.stdio;
import std.process;
void main(){
auto pid = spawnShell("ls -l");
write(pid);
}

How a main function is being called in a c/cpp project

Good day good-looking computer people,
I might be asking a bit too much, but here it goes.
I'm trying to do a bit of reserve engineering on this sound library. Looking at the main.cpp file (which I have posted below) it has two methods, setup and play. I'm a bit confused as to how this is working:
When you run the Xcode project, shouldn't there be a main function that is the first method called? I don't see that here.
The play function is being called (because I hear music), but it must be from elsewhere since in needs the argument output. Where could it be being called from?
To try to be a bit more specific, this is my question:
If the program isn't starting from a main method in the main.cpp file, where else could it be starting from ?
#include "maximilian.h"
double outputs[2],moreoutputs[2]; //some track outputs
double filtered, ramped, filtered2;
osc buffertest,ramp;
mix mymix,bobbins;//some panning busses
double env[4]={200,0,0,50};//the kick drum pitch envelope data
double env2[6]={10000,0,9000,5,0,5};//the hi hat pitch envelope dat
envelope b,f;//two envelopers
sample beats;
extern int channels=2;//stereo-must be supported by hardware
extern int buffersize=256;//should be fine for most things
extern int samplerate=44100;//SR must be supported by card. It's always the default
void setup() {//some inits
b.amplitude=env[0];//starting value for envelope b
f.amplitude=env2[0];//same for f
beats.load("/Users/ericbrotto/Desktop/beats.wav");//put a path to a soundfile here. Wav format only.
printf("Summary:\n%s", beats.getSummary());//get info on samples if you like
}
void play(double *output) {//this is where the magic happens. Very slow magic.
filtered2=beats.play(1*(1./34), 0, beats.length());
bobbins.stereo(filtered2, moreoutputs, 0.5);//invert the pan
output[0]=outputs[0]+moreoutputs[0];//stick it in the out!!
output[1]=outputs[1]+moreoutputs[1];
}
Thanks!
If the program isn't starting from a
main method in the main.cpp file,
where else could it be starting from ?
From a main method in a different source file or pre-compiled library, probably, or definitely.
A main method is required and must have the name main unless you tell the compiler otherwise. It is possible that the main method gets included through a header or is implemented by a macro.
in the directory where you unpacked the library -
find . | xargs egrep "main"
It has to be there somewhere... ;)
You're dealing with a library, not a standalone program. It doesn't have a main function, since it won't run on its own. When a developer uses a library, such as this, his application calls out to the library's functions (such as play).
I don't entirely understand from what you hear music. What have you compiled? An example program? In that case, that example program probably does have a main function and at some point calls the library's play.
main is either getting defined in the the maximillian.h, something it includes, or it is already compiled into the maximillian library.
Put a breakpoints on the front of each function, run the app, and examine call stack when breakpoint is hit.
what i understand from it is the cpp file for the implementation of this library's header it is not a complete program but when you use this lib in your program you have to do is
1) use its setup method to setup or load the file u want to play
2) you have to call play to play the sound
that it about this/.
For your question
If the program isn't starting from a main method in the main.cpp file,
where else could it be starting from
?
Global objects and static members of classes will be initialized before call to main() function.
For below code
class Test
{
public:
};
Test* fun()
{
return new Test;
}
Test *p = fun();
int main()
{
}
For initializing pointer p, fun() will be called before main() function.
Line 491 of maximillian.cpp

Problem debugging C++ with an Eclipse based IDE

This is a weird question in that I'm not sure where to start looking.
First of all, I haven't done any C++ programming for the last 10 years so it could be me thats forgotten a few things. Secondly, the IDE I'm using is Eclipse based (which I've never used) and customized for Samsung bada based mobile development (it kicks off an emulator for debugging purposes)
I'm posting my code samples as images because the StackOverflow WYSIWYG editor seems to have a problem parsing C++.
[EDIT] Due to complaints I've edited my question to remove the images. Hope that helps :)
I have the following header file...
#include <FApp.h>
#include <FBase.h>
#include <FGraphics.h>
#include <FSystem.h>
#include <FMedia.h>
using namespace Osp::Media;
using namespace Osp::Graphics;
class NineAcross :
public Osp::App::Application,
public Osp::System::IScreenEventListener
{
public:
static Osp::App::Application* CreateInstance(void);
public:
NineAcross();
~NineAcross();
public:
bool OnAppInitializing(Osp::App::AppRegistry& appRegistry);
private:
Image *_problematicDecoder;
};
...and the following cpp file...
#include "NineAcross.h"
using namespace Osp::App;
using namespace Osp::Base;
using namespace Osp::System;
using namespace Osp::Graphics;
using namespace Osp::Media;
NineAcross::NineAcross()
{
}
NineAcross::~NineAcross()
{
}
Application* NineAcross::CreateInstance(void)
{
// Create the instance through the constructor.
return new NineAcross();
}
bool NineAcross::OnAppInitializing(AppRegistry& appRegistry)
{
Image *workingDecoder;
workingDecoder->Construct();
_problematicDecoder->Construct();
return true;
}
Now, in my cpp file, if I comment out the line that reads _problematicDecoder->Construct();...I'm able to set a breakpoint and happily step over the call to Constuct() on workingDecoder. However, as soon as I uncomment the line that reads _problematicDecoder->Construct();... I end up with the IDE telling me...
"No source available for "Osp::Media::Image::Construct()"
In other words, why can I NOT debug this code when I reference Image *image from a header file?
Any ideas?
Thanks :-)
This usually means you're stepping through some code which you do not posses its source.
I assume here that Osp::Media::Image is a class supplied by Samsung or similar for which you do not have the cpp file. So this means the debugger can't show you the current code line while you're at a function of Osp::Media::Image.
Alternatively, there's a good chance you do have all of the source code for this class, but Eclipse doesn't know where it is. In this case you can add the correct directories under the Debug Configurations window.
Ok, problem solved.
The idea is to first new up an instance of Image like so...
_decoder = new Osp::Media::Image();
And then do _decoder->Construct().
Funny enough, this seems blatantly obvious to me now coming from the C# world, though why the code I posted for workingDecoder works is still somewhat mysterious to me. The fact the sample projects pre-loaded with the bada IDE don't seem to make a call to new() leads me to believe that perhaps those samples are outdated our out of synch.
Either that or I really AM wildly out of the C++ loop.
Anyway thanks so much for the effort guys.
Appreciated :)