Browse Source

doc: mention aggregate initialization usage in registry::emplace, refs #592 (#593)

* update example to explain aggregate initialization usage, refs #592
* cleanup example; add details about parameterized constructors and aggregate initialization

Co-authored-by: kthrose <kthrose@netpoetica.com>
Keith Rosenberg 5 years ago
parent
commit
7154678347
3 changed files with 41 additions and 0 deletions
  1. 1 0
      AUTHORS
  2. 5 0
      README.md
  3. 35 0
      docs/md/entity.md

+ 1 - 0
AUTHORS

@@ -33,6 +33,7 @@ mhammerc
 Milerius
 morbo84
 m-waka
+netpoetica
 NixAJ
 Oortonaut
 Paolo-Oliverio

+ 5 - 0
README.md

@@ -117,6 +117,9 @@ mind.
 struct position {
     float x;
     float y;
+    position(float x, float y)
+      : x(x), y(y)
+    {}
 };
 
 struct velocity {
@@ -156,7 +159,9 @@ int main() {
 
     for(auto i = 0; i < 10; ++i) {
         auto entity = registry.create();
+        // uses parameterized constructor ...
         registry.emplace<position>(entity, i * 1.f, i * 1.f);
+        // uses aggregate initialization ...
         if(i % 2 == 0) { registry.emplace<velocity>(entity, i * .1f, i * .1f); }
     }
 

+ 35 - 0
docs/md/entity.md

@@ -230,6 +230,41 @@ vel.dx = 0.;
 vel.dy = 0.;
 ```
 
+The `emplace` member function utilizes [std::forward](https://en.cppreference.com/w/cpp/utility/forward)
+to allow you to pass your component's parameterized constructor's arguments:
+
+```cpp
+struct position {
+    float x;	    
+    float y;
+    position(float x, float y)
+      : x(x), y(y)
+    {}
+};
+
+
+// ...
+
+auto &vel = registry.emplace<position>(entity,  100.0f, 100.0f);
+```
+
+Using `emplace`, you can take advantage of [aggregate initialization](https://en.cppreference.com/w/cpp/language/aggregate_initialization). As long as your component structs adhere to 
+the rules of aggregate types, registry.emplace will utilize aggregate
+initializatiion which sets property values in the order in which they are 
+declared in the structure.
+
+```cpp
+struct velocity {
+    float dx;
+    float dy;
+}
+
+// ...
+
+auto &vel = registry.emplace<velocity>(entity,  0.0f, 0.0f);
+```
+
+
 Similarly, `insert` does it for multiple entities and accepts a range rather
 than a single entity in order to: