Add the operator []

pull/57/head
Hieromon Ikasamo 6 years ago
parent a2c1acc83a
commit 8111ad79ce
  1. 3
      examples/FileUpload/FileUpload.ino
  2. 9
      mkdocs/achandling.md
  3. 12
      mkdocs/apiaux.md

@ -115,8 +115,7 @@ String postUpload(AutoConnectAux& aux, PageArgument& args) {
String content; String content;
// Explicitly cast to the desired element to correctly extract // Explicitly cast to the desired element to correctly extract
// the element using the operator []. // the element using the operator [].
// AutoConnectAux& root_page = *portal.aux("/"); AutoConnectFile& filename = (AutoConnectFile&)auxUpload["filename"];
AutoConnectFile& filename = static_cast<AutoConnectFile&>(auxUpload["filename"]);
AutoConnectText& aux_filename = (AutoConnectText&)aux["filename"]; AutoConnectText& aux_filename = (AutoConnectText&)aux["filename"];
AutoConnectText& aux_size = (AutoConnectText&)aux["size"]; AutoConnectText& aux_size = (AutoConnectText&)aux["size"];
AutoConnectText& aux_contentType = (AutoConnectText&)aux["content_type"]; AutoConnectText& aux_contentType = (AutoConnectText&)aux["content_type"];

@ -101,14 +101,19 @@ AutoConenctText& text = aux->getElement<AutoConnectText>("caption"); // Cast to
Serial.println(text.value); Serial.println(text.value);
``` ```
You can also use the operator **`[]`** as another way to get the desired element. An operator **`[]`** is a shortcut for [getElement](apiaux.me#getelement) function with the reference casting and makes simplify the code. Its argument is the name of the element to be acquired similarly to getElement function. For example, the following sketch code returns the same as reference of AutoConnectText element as `caption`. You can also use the [operator **`[]`** of AutoConnectAux](apiaux.md#operator) as another way to get the desired element. An operator **`[]`** is a shortcut for [getElement](apiaux.me#getelement) function with the reference casting and makes simplify the code. Its argument is the name of the element to be acquired similarly to getElement function. For example, the following sketch code returns the same as reference of AutoConnectText element as `caption`.
```cpp ```cpp hl_lines="4 5"
AutoConnect portal;
portal.load(auxJson);
AutoConnectAux& aux = *portal.aux("/page1"); AutoConnectAux& aux = *portal.aux("/page1");
AutoConnectText& text1 = aux.getElement<AutoConnectElement>("caption"); AutoConnectText& text1 = aux.getElement<AutoConnectElement>("caption");
AutoConnectText& text2 = (AutoConnectText&)aux["caption"]; AutoConnectText& text2 = (AutoConnectText&)aux["caption"];
``` ```
!!! note "Need cast to convert to the actual type"
An operator `[]` returns a referene of an AutoConnectElement. It is necessary to convert the type according to the actual element type.
To get all the AutoConnectElements in an AutoConnectAux object use the [**getElements**](apiaux.md#getelements) function. This function returns the vector of the reference wrapper as **AutoConnectElementVT** to all AutoConnectElements registered in the AutoConnectAux. To get all the AutoConnectElements in an AutoConnectAux object use the [**getElements**](apiaux.md#getelements) function. This function returns the vector of the reference wrapper as **AutoConnectElementVT** to all AutoConnectElements registered in the AutoConnectAux.
```cpp ```cpp

@ -15,6 +15,18 @@ AutoConnectAux(const String& uri = String(""), const String& title = String(""),
## <i class="fa fa-code"></i> Public member functions ## <i class="fa fa-code"></i> Public member functions
### <i class="fa fa-caret-right"></i> operator [ ]
```cpp
AutoConnectElement& operator[](const String& name)
```
Returns a reference to the element specified by **name**. An operator `[]` is a shortcut for [getElement](apiaux.md#getelement) function with the reference casting. Unlike getElement, which returns a pointer to that element, an operator `[]` returns a reference to that element. You also need to cast the return value to the actual type, just like the getElement function.
<dl class="apidl">
<dt>**Parameters**</dt>
<dd><span class="apidef">name</span><span class="apidesc">Name of the AutoConnectElements to be retrieved.</span></dd>
<dt>**Return value**</dt><dd>A reference to AutoConnectElement. It is different from the actual element type.</dd>
</dl>
### <i class="fa fa-caret-right"></i> add ### <i class="fa fa-caret-right"></i> add
```cpp ```cpp

Loading…
Cancel
Save