Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce read-only types (take two) #1975

Open
2 tasks
nkrkv opened this issue Feb 21, 2020 · 0 comments
Open
2 tasks

Introduce read-only types (take two) #1975

nkrkv opened this issue Feb 21, 2020 · 0 comments
Assignees

Comments

@nkrkv
Copy link
Member

nkrkv commented Feb 21, 2020

@evgenykochetkov already made port type read-only in #1930 but has to revert it in #1964 because of some cases become impossible if port-typed values can no longer be emitted. In particular, dht11-device and dht2x-device nodes hold the port inside C++ struct to be latter used by unpack-dht11-device, for example. The latter node compiles with an error after #1930 because emitValue can no longer be used with ports.

We should find a way to complete the attempt because it brings many benefits:

  • Flash and RAM usage optimization (10% as shown by the Introduce read only types #1930)
  • Compile-time port and other read-only values validation: clear error messages are possible

The implementation requires much exploration, trials, and errors with C++ metaprogramming, so no particular receipt here. Only a few high-level thoughts:

  • High backward-compatibility is a must. The only thing which may break is the emitValue of port type.
  • The custom type definition (Type) has to become generic in C++, parametrized with read-only values
  • As a consequence, the State has to become generic as well. The bad news is that it template parameters depend not only on its own pins but on the Type definitions of upstream nodes
  • Perhaps, it would make sense to move from separate functions and structures in a node implementation to class-based definitions that keep all meta-arguments under a single umbrella:
template <uint8_t x>
struct CustomType {
    char buff[x];
};

template <uint8_t all, size_t readOnly, uint8_t metaArgs>
struct Node : public NodeBase<Node> {
  Number a, b, c;
  uint8_t buffer[readOnly];  // Hey, no explicit `State` is necessary

  using Type = CustomType<metaArgs>;

  // {{ GENERATED_CODE}}  <-- no longer required

  void evaluate(Context ctx) {
    static_assert(isValidPort(all));
    getValue<input_IN>(ctx);
    emitValue<output_OUT>(ctx, 42);
  }
}

Acceptance criteria

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants