Dynamically create content of pages #554
-
|
Hi, Great code! Love it! Using it for a home project but my C++ competence is weak/old/rusty. 20+ years since I worked with c++ and coding, and a lot have happened with the language. I'm trying to dynamically populate create pages with elements, but my c++ competence is simply not good enough. My current static variant works fine, i.e. [a variant of] this: To make it more dynamic, I intended to build a vector of AutoConnectElement(s) and later throw that at AutoConnectAux. I've tried many variants, and I cannot simply make it work/compile. I've tried to understand how to make this work with AutoConnectElementVT but cannot find any examples to lean against. My trial-and-error has now reached an end and hopefully someone can step in and assist. Here's a sample code that will not compile. This variant throws: Removing the So, now I need to stop my trial-and-error and ask for help. Thanks in advance, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
|
@faanskit, Thank you. #include <Arduino.h>
#include <vector>
#include <WiFi.h>
#include <WebServer.h>
#include <AutoConnect.h>
AutoConnect portal;
void getProviderMenuStr(std::vector<String>& providers) {
providers.push_back("foo");
providers.push_back("bar");
providers.push_back("qux");
}
void getMarketMenuStr(std::vector<String>& areas) {
areas.push_back("Johnson City");
areas.push_back("East HIlls");
areas.push_back("North Castle");
}
class MyPortal {
public:
MyPortal() {}
~MyPortal() {}
AutoConnectAux* configPage(void);
};
AutoConnectAux* MyPortal::configPage(void) {
std::vector<String> providers;
std::vector<String> areas;
getProviderMenuStr(providers);
getMarketMenuStr(areas);
AutoConnectSelect* myProv = new AutoConnectSelect("Provider", providers, "Select provider", 1);
AutoConnectSelect* myArea = new AutoConnectSelect("Area", areas, "Select area", 1);
return new AutoConnectAux("/area", "Config...", true, {*myProv, *myArea});
}
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
MyPortal myPortal;
portal.join({ *myPortal.configPage() });
portal.begin();
}
void loop() {
portal.handleClient();
} |
Beta Was this translation helpful? Give feedback.
-
It may not be necessary, but I don't think you should rely too much on what's on the web to understand modern C++. C++ has undergone a major transformation with C++11. It has evolved abstraction modeling backed by powerful typing. As a result, copying and moving objects is a deliberate choice of the programmer in modern C++ programming. The compiler reads the intent and (consequently) decides whether to apply the pointer operation or leave the hidden temporary instance on the stack. For example, the type Therefore, manipulating a pointer to an object of type AutoConnectElement with the intention of a collection set of data will not work. In your case, your knowledge of 90's C++ (or rather, C) is so deep that you probably took the approach of fixing the instance to a certain location in memory space and then manipulating the data structure with a pointer. That is a correct insight, but in modern C++, there are cases where that is counterproductive. In particular, you need to be careful in cases where STL is used a lot. It is also about the appropriateness of a function returning an object. Local variables of primitive types are allocated on the stack and are undefined after the function returns. In the case of named objects, however, the value can be referenced without copying cost if the appropriate code is written. The return type of If you are interested, I suggest you take a look at a book, Effective Modern C++. Regards, |
Beta Was this translation helpful? Give feedback.
@faanskit, Thank you.
So, is it like this?