Brunot
Loading...
Searching...
No Matches
Path.h
Go to the documentation of this file.
1
11// ____ __ __ __
12// /\__ _\/\ \ /\ \/\ \
13// \/_/\ \/\ \ \___ __ \ \ \_\ \ ___ __ __ ____ __
14// \ \ \ \ \ _ `\ /'__`\ \ \ _ \ / __`\/\ \/\ \ /',__\ /'__`\
15// \ \ \ \ \ \ \ \/\ __/ \ \ \ \ \/\ \L\ \ \ \_\ \/\__, `\/\ __/
16// \ \_\ \ \_\ \_\ \____\ \ \_\ \_\ \____/\ \____/\/\____/\ \____\
17// \/_/ \/_/\/_/\/____/ \/_/\/_/\/___/ \/___/ \/___/ \/____/
18
19
20#pragma once
21#include <string>
22
23#include "Framework/Engine.h"
25
26#include "System/SceneManager.h"
27
36
37
38namespace gobj
39{
40
324template <typename Gobj = GameObject>
325class Path
326{
327public:
328 static auto bindLuaTypes(sol::state& lua) -> void
329 {
330 }
331
332 using value_type = Gobj;
333
334
335
342 explicit Path(std::string pathString)
343 : path(std::make_unique<PathRoot>())
344 {
345 switch (pathString[0])
346 {
347 case '~':
348 {
349 path = std::make_shared<PathBase>(*Engine::getSystem<sys::SceneManager>()->getCurrentScene());
350 pathString = pathString.substr(1);
351 break;
352 }
353 case '^':
354 {
356 pathString = pathString.substr(1);
357 break;
358 }
359 case '/':
360 {
361 path = std::make_shared<PathRoot>();
362 break;
363 }
364 case '.':
365 {
366 // do nothing
367 assert(false && "Cannot create a path that starts with . without passing a gameObject as base");
368 break;
369 }
370 default:
371 {
372 break;
373 }
374 }
375
376 // parse path, getting rid of the first 2 characters
377 parsePath(pathString);
378
379
380 }
381
382
390 Path(std::string pathString, GameObject& rootObject)
391 {
392 assert(pathString[0] == '.');
393 path = std::make_shared<PathBase>(rootObject);
394 // remove the first character, so we don't have an extra directory marker
395 // (the other dir marker will be removed at the end of this function)
396 pathString = pathString.substr(1);
397
398 parsePath(pathString);
399 }
400
401
405 operator Gobj&()
406 {
407 return *begin();
408 }
409
413 operator Gobj*()
414 {
415 return get();
416 }
417
421 operator const Gobj&()
422 {
423 return *begin();
424 }
425
426private:
427 // path objects
428
429 // possibly cache begin and end to optimize
430 // begin_
431 // end_
432
434 std::shared_ptr<PathNode> path;
435
436public:
442 {
443
449 : itr(std::move(itr))
450 {
451 }
452
453 public:
454 friend Path;
455
456 // Part of making iterators stl compliant is declaring public types
457 using difference_type = std::ptrdiff_t;
458 using value_type = Gobj;
459
463 iterator() = default;
464
469 auto get() const -> Gobj*
470 {
471 auto result = dynamic_cast<Gobj*>(itr.get());
472 if (!result)
473 {
474 throw PathException("Path iterator called get() on GameObject that isn't same type as iterator");
475 }
476 return result;
477 }
478
486 {
487 ++itr;
488 return *this;
489 }
490
496 auto operator++(int) -> iterator
497 {
498 auto copy = *this;
499 ++*this;
500 return copy;
501 }
502
508 auto operator==(iterator other) const -> bool
509 {
510 return itr == other.itr;
511 }
512
518 auto operator*() const -> std::add_lvalue_reference_t<Gobj>
519 {
520 auto result = dynamic_cast<Gobj*>(itr.get());
521 if (result == nullptr)
522 {
523 throw PathException("Path iterator dereferenced GameObject that isn't same type as iterator");
524 }
525 return *result;
526 }
527
532 auto operator->() const -> Gobj*
533 {
534 return get();
535 }
536
537 private:
540
541 };
542
543 // Check to make sure Path::iterator is stl compliant
544 static_assert(std::input_iterator<iterator>);
545 static_assert(std::sentinel_for<iterator, iterator>);
546
547
553 auto begin() const -> iterator
554 {
555 return static_cast<iterator>(path->begin());
556 }
557
558
563 auto end() const -> iterator
564 {
565 return static_cast<iterator>(path->end());
566 }
567
573 auto get() const -> Gobj*
574 {
575 return begin().get();
576 }
577
582 auto operator->() const -> Gobj*
583 {
584 return get();
585 }
586
587 // helper functions
588private:
595 static auto splitString(const std::string& fullString, const std::string& delimiter) -> std::vector<std::string>
596 {
597 size_t start = 0;
598 auto end = delimiter.length();
599 auto delimiterLength = delimiter.length();
600 std::string token;
601 std::vector<std::string> result;
602
603 while ((end = fullString.find(delimiter, start)) != std::string::npos)
604 {
605 token = fullString.substr(start, end - start);
606 start = end + delimiterLength;
607 result.push_back(token);
608 }
609
610 result.push_back(fullString.substr(start));
611 return result;
612 }
613
615 {
616 // if we are Component path
617 if (std::is_same_v<Gobj, Component>)
618 {
619 path->appendPathNode(std::make_shared<PathFilter>(
620 [](const GameObject& g)
621 {
622 return g.isType(Type::Component);
623 }
624 ));
625 }
626 // if we are an entity path
627 else if (std::is_same_v<Gobj, Entity>)
628 {
629 path->appendPathNode(std::make_shared<PathFilter>(
630 [](const GameObject& g)
631 {
632 return g.isType(Type::Entity);
633 }
634 ));
635 }
636 // if we are a System path
637 else if (std::is_same_v<Gobj, System>)
638 {
639 path->appendPathNode(std::make_shared<PathFilter>(
640 [](const GameObject& g)
641 {
642 return g.isType(Type::System);
643 }
644 ));
645 }
646 // if we are a path to a something other than a general GameObject, or general System
647 // (so any specific Component, or any specific System)
648 // (also some other things, but you can't make Paths to them)
649 else if (!std::is_same_v<Gobj, GameObject> && !std::is_same_v<Gobj, System>)
650 {
651 path->appendPathNode(std::make_shared<PathFilter>(
652 [](const GameObject& g)
653 {
654 // return true if `g` is of type Gobj
655 return !!(dynamic_cast<const Gobj*>(&g));
656 }
657 ));
658 }
659 }
660
665 auto parsePath(const std::string& pathString) -> void
666 {
667 // get rid of first token
668 std::string dir = "/";
669
670 auto nodes = splitString(pathString, dir);
671
672 for (auto itr = nodes.begin(); itr != nodes.end(); ++itr)
673 {
674 // don't do this the first time, or if we are going to add an Up
675 if (itr != nodes.begin() && *itr != "..")
676 {
677 path->appendPathNode(std::make_shared<PathDir>());
678
679 }
680 if (*itr == "..")
681 {
682 path->appendPathNode(std::make_shared<PathUp>());
683 }
684 else if (*itr == "")
685 {
686 }
687 else
688 {
689 path->appendPathNode(std::make_shared<PathName>(*itr));
690 }
691 }
692
693 // add a filter to the end to only look at the objects we care about
694 // if we have a range, ie a `/` at the end
695 if (nodes.back().empty())
696 {
698 }
699 }
700
701};
702
712template<std::derived_from<GameObject> Gobj, typename ... Args>
713constexpr auto FormatPath(const std::format_string<Args...>&& fmt_path, Args&&... args) -> Path<Gobj>
714{
715 return Path<Gobj>(std::format(fmt_path, std::forward<Args>(args)...));
716}
717
718
729template<std::derived_from<GameObject> Gobj, typename ... Args>
730constexpr auto FormatPath(GameObject& rootObject, const std::format_string<Args...>&& fmt_path, Args&&... args) -> Path<Gobj>
731{
732 return Path<Gobj>(std::format(fmt_path, std::forward<Args>(args)...), rootObject);
733}
734
735
741
742}
static FMOD_RESULT result
Definition AudioObject.cpp:27
The class that holds the top node, and manages the main game loop, as well as startup and shutdown.
the base class for the engine, most things inherit from this.
One of the beginnings of a Path, where the Path starts at an arbitrary GameObject.
Given a range of GameObjects, generates a range of all of those GameObjects children.
excption emittited by Path s and their associated classes
Internal PathNode that takes a range of GameObjects on the left, and filters the range to only GameOb...
Internal PathNode that takes a range of GameObjects on the left, and filters the range to only GameOb...
Base class for a linked list of Path sections, which altogether represent the logic of a Path,...
Path Node that represents the Root object in the Engine, and provides a starting point for a Path.
Internal Path Node that takes a range of GameObjects on the left, and transforms the range into the p...
static auto getSystem() -> system *
A function to get a system.
Definition Engine.h:50
the base class for the engine, most things inherit from this.
Definition GameObject.h:83
auto isType(gobj::Type otherType) const -> bool
check whether the GameObject is the same as a given type.
Definition GameObject.cpp:271
Definition PathException.h:24
iterator type, refers to a GameObject, and holds onto a reference to a PathNode to determine its rang...
Definition PathNode.h:45
iterator member class for Path.
Definition Path.h:442
PathNode::path_iterator itr
Internal iterator type that is used by PathNode, and only refers to GameObjects as GameObjects.
Definition Path.h:539
auto operator*() const -> std::add_lvalue_reference_t< Gobj >
Gets a reference to the GameObject the iterator refers to, and casts it to Gobj as well.
Definition Path.h:518
iterator(PathNode::path_iterator itr)
constructs a Path::iterator from a PathNode::path_iterator, which is an internal type used for logic
Definition Path.h:448
auto get() const -> Gobj *
Definition Path.h:469
iterator()=default
defaults initializes iterator as a sentinel "Past the End" iterator
friend Path
Definition Path.h:454
auto operator++(int) -> iterator
post-increments the iterator, getting the next GameObject in the range
Definition Path.h:496
Gobj value_type
Definition Path.h:458
auto operator==(iterator other) const -> bool
compares if two iterators point to the same object.
Definition Path.h:508
std::ptrdiff_t difference_type
Definition Path.h:457
auto operator->() const -> Gobj *
allows for calling functions on the objects an iterator refers to.
Definition Path.h:532
auto operator++() -> iterator &
pre-increments the iterator, getting the next GameObject in the range.
Definition Path.h:485
An object that represents where a GameObject or a range of GameObjects exists in the Engine.
Definition Path.h:326
void addFilterBasedOnType()
Definition Path.h:614
Path(std::string pathString)
constructs a Path by parsing a string
Definition Path.h:342
Gobj value_type
Definition Path.h:332
auto begin() const -> iterator
Definition Path.h:553
auto end() const -> iterator
Definition Path.h:563
std::shared_ptr< PathNode > path
a pointer to the linked list of PathNodes that handles the logic for Path deduction
Definition Path.h:434
static auto bindLuaTypes(sol::state &lua) -> void
Definition Path.h:328
auto parsePath(const std::string &pathString) -> void
parses a Path, constructing the linked list of PathNodes, which calculate the range that a Path refer...
Definition Path.h:665
static auto splitString(const std::string &fullString, const std::string &delimiter) -> std::vector< std::string >
splits a string into many smaller strings.
Definition Path.h:595
Path(std::string pathString, GameObject &rootObject)
constructs a Path by parsing a string
Definition Path.h:390
auto operator->() const -> Gobj *
Definition Path.h:582
auto get() const -> Gobj *
Definition Path.h:573
namespace for things relating to GameObject, other than GameObject itself
Definition Entity.h:440
constexpr auto FormatPath(const std::format_string< Args... > &&fmt_path, Args &&... args) -> Path< Gobj >
Constructs a Path by parsing a string, and uses string formatting.
Definition Path.h:713
@ Entity
Definition GameObject.h:58
@ Component
Definition GameObject.h:59
@ System
Definition GameObject.h:57
Path Node that represents the Root object in the Engine, and provides a starting point for a Path.
Definition PathRoot.h:28