-1
\$\begingroup\$

I'm making a library to build and manage a website. I want to control what gets to be inserted. For example I don't want to be able to insert <foo bar="i"></foo> as xml builders allow.

I'm working with microcontrollers (currently with esp8266ex) that has support for WiFi and can act as a webserver. As you may or may not know, managing a complex web site in c++ is not easy. For now, as far as I know, everybody is building a webpage using ´string +="site"´. For simple projects that's fine, but as you start getting into multiple dynamic pages it gets harder.

This is the end result I need (or similar):

node<dataType::div> d_div;
d_div.addParameter<param::_class>("div1");
d_div.addParameter<param::name>('c');
d_div.addParameter<param::id>(34);

and this is the code that I've written to achieve that:

namespace dataType {
    typedef enum {a,div} type;
}

namespace param {
    typedef enum {name, id, _class} param;
}

template<param::param N>
struct helper {
    static void f();   
};
template<>
struct helper<param::name> {
    static void f(char c) {std::cout << c << std::endl;}
};
template<>
struct helper<param::id> {
    static void f(int i) {std::cout << i << std::endl;}
};
template<>
struct helper<param::_class> 
{
    static void f(std::string s) {std::cout << s << std::endl;}
};


template <dataType::type T>
class node 
{
public:
    template <param::param P>
    void addParameter(int i){
        helper<P>::f(i);
    }
    template <param::param P>   
    void addParameter(char c){
        helper<P>::f(c);
    }
    template <param::param P>   
    void addParameter(std::string s){
        helper<P>::f(s);
    }
};

Is this a good approach? Is there a way to improve this? Or am I doing this all wrong?

As you may or may not know, in HTML each tag supports global attributes and (if it has) specific attributes that only that tag supports. If I take the approach that Quuxplusone suggested some problems will arise.

node d_div = node("div").setClass("div1").setName("c").setId(34);

  1. having node("div") would allow any tag to be inserted - solution: enum and/or a bunch of if else
  2. having a specific method for each tag will greatly increase the code size. (There are currently 173 attributes in total)
  3. as mentioned above, each tag has global and specific attributes. How would i control that?

ps. The code above does indeed compiles, so you can test it.

\$\endgroup\$

closed as unclear what you're asking by Dannnno, t3chb0t, mdfst13, syb0rg, Mast Jul 26 '16 at 16:02

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    \$\begingroup\$ Could you explain the use case? This is super vague as is, and its very unclear to me what you're asking \$\endgroup\$ – Dannnno Jul 25 '16 at 17:17
  • 1
    \$\begingroup\$ Does this code actually work in practice? Could you include a code snippet that produces an HTML document? \$\endgroup\$ – 200_success Jul 25 '16 at 17:47
  • \$\begingroup\$ I haven't gotten to that point yet. But it will not be a problem. All of the nodes will be stored in the rooted ordered tree. And to produce the HTML I would just go trough the tree (preorder) and generate the markup. \$\endgroup\$ – CodeBreaker Jul 25 '16 at 17:58
  • \$\begingroup\$ Can you also explain why this is the solution you've chosen? \$\endgroup\$ – Dannnno Jul 25 '16 at 18:33
  • 1
    \$\begingroup\$ @Quuxplusone This question was borderline; I considered closing it, but am also declining to override the close votes. To me, it was unclear whether the code "works", since it doesn't actually accomplish the task. Other users may have other reasons for closing. Feel free to ask a question on Code Review Meta if you feel that this closure was wrongly decided. \$\endgroup\$ – 200_success Jul 26 '16 at 20:18
2
\$\begingroup\$

I'm confused as to why you'd want to write

node<dataType::div> d_div;
d_div.addParameter<param::_class>("div1");
d_div.addParameter<param::name>('c');
d_div.addParameter<param::id>(34);

when you could write

node d_div = node("div").setClass("div1").setName("c").setId(34);

What is the point of all the extra <> and :: characters you're typing? I mean, the extra verbosity definitely adds a lot of cost to the development of your program; what is the corresponding benefit that makes the verbosity worthwhile?

(Re "cost", just in case it's unclear: I see at least two costs here. First is the absolute cost in terms of typing all that extra stuff and having to read and maintain it. Second is that adding all that stuff made the code so confusing that you had to post it here to get help with it, even before it was code-complete.)

I suggest rewriting the code from scratch using the "simple", no-templates style; and just see how far you get before you get stuck. If the answer is "Huh. I never got stuck after all"... well, you have your answer. And if the answer is "I get stuck here (points)"... well, you can add that to your question, and it may help me (or someone else) to come up with a more helpful suggestion for you.

EDIT from Quuxplusone: Since this question is currently disabled from other answers, please feel free to edit this answer (adding a new section below) if you'd like to provide a long-form answer to the OP. Comments aren't super useful for long-form answers.

\$\endgroup\$
  • \$\begingroup\$ I would make it more like jQuery: node("div").attr("class", "div1").attr("name", "c").attr("id", "34"); \$\endgroup\$ – 200_success Jul 26 '16 at 6:05
  • 1
    \$\begingroup\$ I wouldn't object to that. :) But he did specifically start with "I don't want to be able to insert [arbitrary attributes] as xml builders allow", so I think the extra rigidity in this case might be a feature, not a bug. For general-purpose HTML, arbitrary attributes are a must, because of technologies such as Web Components that mean the set of "all HTML tags" is no longer a closed set. \$\endgroup\$ – Quuxplusone Jul 26 '16 at 6:16
  • \$\begingroup\$ I've updated the question that addresses your answer. Additionally, having all the extra <> and :: allows me to have better control. Having nodeType will tell what is the tag and will open method addAttribute<attribute> with specific attributes that that tag allows. Finally, defining custom elements is not yet supported across the board, but when it does, implementing it would not be a problem. \$\endgroup\$ – CodeBreaker Jul 26 '16 at 9:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.