Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Iterator{
- Boolean(*next) (Iterator* iterator, Any* key, Any* value);
- alignas(std::max_align_t)
- uint8_t payload[4 * 8];
- };
- struct DynamicIterator{
- Dynamic* object;
- Dynamic::map_t::iterator it;
- };
- Boolean Dynamic::iterate(Dynamic* dyn, Iterator* iterator){
- DynamicIterator* iter = (DynamicIterator*)iterator->payload;
- new(&iter->it)Dynamic::map_t::iterator(std::move(dyn->map.begin()));
- iter->object = dyn;
- iterator->next = [](Iterator* iterator, Any* key, Any* value) -> Boolean{
- DynamicIterator* iter = (DynamicIterator*)iterator->payload;
- if ( iter->it == iter->object->map.end() ){
- std::destroy_at(&iter->it); // if it was the last iteration, destroy it
- return false;
- }
- *key = iter->it->first;
- *value = iter->it->second;
- iter->it++;
- return true;
- };
- return true;
- }
- // somewhere in the user code...
- Any object = ...; // <- iterable object
- Iterator iter;
- Any key;
- Any value;
- any_iter(object, &iter); // any iterator does dynamic dispatch and eventually invokes Dynamic::iterate
- while ( iter.next(&iter, &key, &value) ){
- // do something
- if ( something ){
- return; // this leaves without destroying std::map::iterator inside Iterator
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement