GithubHelp home page GithubHelp logo

AttachRequest custom parameters about cppdap HOT 8 CLOSED

google avatar google commented on September 20, 2024
AttachRequest custom parameters

from cppdap.

Comments (8)

ben-clayton avatar ben-clayton commented on September 20, 2024

Hi @rapgenic,

Thank you for your interest in cppdap.
There was no easy way to do this (without duplicating the field reflection of the base class into the derived class). #33 however gives you the ability to do this.

Once this lands you can use typeinfo_test.cpp as a reference for usage.

Many thanks,
Ben

from cppdap.

ben-clayton avatar ben-clayton commented on September 20, 2024

Now landed.

from cppdap.

rapgenic avatar rapgenic commented on September 20, 2024

@ben-clayton Thank you for your quick answer and implementation!

I tried and I am actually able to subclass the AttachRequest type, however I do not understand how to register the handler: I tried using my custom type in the callback but it does not compile.

I tried to dynamic_cast the AttachRequest to my custom type, but that did not work either.

Could you give me a simple example for making it work? Thank you very much, again

from cppdap.

ben-clayton avatar ben-clayton commented on September 20, 2024

If you want to just add some custom fields to the request (not the response) you'd declare your extended AttachRequest request message type as:

namespace dap {

class AttachRequestEx : public AttachRequest {
 public:
  dap::number some_custom_data;
};

DAP_STRUCT_TYPEINFO_EXT(AttachRequestEx,
                        AttachRequest,
                        "attach", // This is the wire protocol name for the AttachRequest
                        DAP_FIELD(some_custom_data, "some_custom_data")); // Repeat these for each of your custom fields

}  // namespace dap

You can then set up a handler for this extended message type with:

void bindAttachRequestExHandler(std::unique_ptr<dap::Session> session) {
  session->registerHandler([](const dap::AttachRequestEx& request) {
    dap::AttachResponse response;
    return response;
  });
}

If you want to send a AttachRequestEx you can with:

void sendAttachRequestEx(std::unique_ptr<dap::Session> session) {
  dap::AttachRequestEx request; // Assign to the custom fields.
  session->send(request);
}

If you also want a custom derived response type:

namespace dap {

class AttachResponseEx : public AttachResponse {
 public:
  dap::number some_custom_data;
};

DAP_STRUCT_TYPEINFO_EXT(AttachResponseEx,
                        AttachResponse,
                        "",
                        DAP_FIELD(some_custom_data, "some_custom_data"));

class AttachRequestEx : public AttachRequest {
 public:
  using Response = AttachResponseEx; // This is the magic that binds the AttachRequestEx to the AttachResponseEx
  dap::number some_custom_data;
};

DAP_STRUCT_TYPEINFO_EXT(AttachRequestEx,
                        AttachRequest,
                        "attach", // This is the wire protocol name for the AttachRequest
                        DAP_FIELD(some_custom_data, "some_custom_data"));

}  // namespace dap

void bindAttachRequestExHandler(std::unique_ptr<dap::Session> session) {
  session->registerHandler([](const dap::AttachRequestEx&) {
    dap::AttachResponseEx response; // Note the Ex suffix
    return response;
  });
}

void sendAttachRequestEx(std::unique_ptr<dap::Session> session) {
  dap::AttachRequestEx request; // This is the same...
  session->send(request); // but the return type is a dap::AttachResponseEx
}

If you're still having difficulties, please share with me your code snippet and compiler error.

Many thanks,
Ben


Edit: Fixed the wire protocol name which should have been "attach". See:

DAP_IMPLEMENT_STRUCT_TYPEINFO(AttachRequest,
"attach",
DAP_FIELD(restart, "__restart"));

from cppdap.

rapgenic avatar rapgenic commented on September 20, 2024

I think I have managed to do it.

The error was mine, the source was not compiling because I did not return a response.

Thank you very much for your help, I think the issue can be closed now!
Giulio

from cppdap.

ben-clayton avatar ben-clayton commented on September 20, 2024

Great to hear.
Shout if you have any other questions or issues!

from cppdap.

kuafuwang avatar kuafuwang commented on September 20, 2024

This is my way . Name another AttachRequest type.

namespace dap {
	// The attach request is sent from the client to the debug adapter to attach to
	// a debuggee that is already running. Since attaching is debugger/runtime
	// specific, the arguments for this request are not part of this specification.
	struct JavaDebugAttachRequest : public Request {
		using Response = AttachResponse;

		JavaDebugAttachRequest() = default;
		~JavaDebugAttachRequest() = default;

		// Optional data from the previous, restarted session.
		// The data is sent as the 'restart' attribute of the 'terminated' event.
		// The client should leave the data intact.
	   // optional<variant<array<any>, boolean, integer, null, number, object, string>>
		//    restart;
		string type;
		string name;
		string request;
		optional<string> projectName;
		array<string> sourcePaths;

		string hostName;
		integer port;
		integer timeout;
	};

	DAP_DECLARE_STRUCT_TYPEINFO(JavaDebugAttachRequest);

	DAP_IMPLEMENT_STRUCT_TYPEINFO(JavaDebugAttachRequest,
		"attach",
		DAP_FIELD(type, "type"),
		DAP_FIELD(name, "name"),
		DAP_FIELD(request, "request"),
		DAP_FIELD(projectName, "projectName"),
		DAP_FIELD(sourcePaths, "sourcePaths"),
		DAP_FIELD(hostName, "hostName"),
		DAP_FIELD(port, "port"),
		DAP_FIELD(timeout, "timeout"));
}

from cppdap.

ben-clayton avatar ben-clayton commented on September 20, 2024

@kuafuwang - your solution works, but the DAP_STRUCT_TYPEINFO_EXT solution is more future proof - if the DAP specification later adds additional fields to the AttachRequest, these will automatically be included in your derived type. You could of course just embed those new fields directly into your JavaDebugAttachRequest, but this a tedious and manual process that you'd need to do every time you update cppdap that's rolled to the new spec.

Also - DAP_STRUCT_TYPEINFO is a helper that performs both the DAP_DECLARE_STRUCT_TYPEINFO and DAP_IMPLEMENT_STRUCT_TYPEINFO macros into one:

cppdap/include/dap/typeof.h

Lines 212 to 217 in cc93ba9

// DAP_STRUCT_TYPEINFO() is a helper for declaring and implementing a TypeOf<>
// specialization for STRUCT in a single statement.
// Must be used within the 'dap' namespace.
#define DAP_STRUCT_TYPEINFO(STRUCT, NAME, ...) \
DAP_DECLARE_STRUCT_TYPEINFO(STRUCT); \
DAP_IMPLEMENT_STRUCT_TYPEINFO(STRUCT, NAME, __VA_ARGS__)

from cppdap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.