33#define CPPSPEC_CHILD_HPP
44#pragma once
55
6+ #ifndef CPPSPEC_DEBUG
7+ #define CPPSPEC_DEBUG false
8+ #endif
9+
610#include < string>
11+ #include < memory>
712#include < iostream>
813#include < typeinfo>
914
@@ -41,7 +46,6 @@ class Child {
4146 // All instances of Child start out healthy.
4247 bool status = true ;
4348
44- // TODO: Change this to a std::unique_ptr
4549 Formatters::BaseFormatter *formatter = nullptr ;
4650
4751 public:
@@ -57,39 +61,55 @@ class Child {
5761 Child &operator =(const Child &) = default ;
5862
5963 // Custom constructors
60- explicit Child (Child &parent) : parent(&parent) {}
61- explicit Child (Child *parent) : parent(parent) {}
62- explicit Child (const Child *parent) : parent(const_cast <Child *>(parent)) {}
64+ explicit Child (Child &parent) noexcept : parent(&parent) {}
65+ explicit Child (Child *parent) noexcept : parent(parent) {}
66+ explicit Child (const Child *parent) noexcept
67+ : parent(const_cast <Child *>(parent)) {}
6368
6469 /* --------- Parent helper functions -------------*/
6570
6671 /* * @brief Check to see if the Child has a parent. */
67- const bool has_parent () { return parent != nullptr ; }
72+ const bool has_parent () noexcept { return parent != nullptr ; }
6873
6974 // TODO: Look in to making these references instead of pointer returns
7075 /* * @brief Get the Child's parent. */
71- Child *get_parent () { return parent; }
72- const Child *get_parent () const { return const_cast <Child *>(parent); }
76+ constexpr Child *get_parent () noexcept { return parent; }
77+ constexpr const Child *get_parent () const noexcept {
78+ return const_cast <Child *>(parent);
79+ }
80+
7381 template <class C >
74- C get_parent_as ();
82+ constexpr C get_parent_as () noexcept {
83+ return static_cast <C>(get_parent ());
84+ }
7585
7686 /* * @brief Set the Child's parent */
77- void set_parent (Child *parent) { this ->parent = parent; }
87+ constexpr void set_parent (Child *parent) noexcept { this ->parent = parent; }
88+ constexpr void set_parent (const Child *parent) noexcept {
89+ this ->parent = const_cast <Child *>(parent);
90+ }
7891
7992 /* --------- Formatter helper functions -----------*/
80- const bool has_formatter (); // Check to see if the tree has a printer
81- Formatters::BaseFormatter &get_formatter (); // Get the printer from the tree
82- void set_printer (Formatters::BaseFormatter &formatter) {
83- this ->formatter = &formatter;
93+ // Check to see if the tree has a printer
94+ constexpr const bool has_formatter () noexcept ;
95+
96+ // Get the printer from the tree
97+ constexpr Formatters::BaseFormatter &get_formatter () noexcept ;
98+
99+ constexpr void set_printer (const Formatters::BaseFormatter &formatter) {
100+ this ->formatter = &const_cast <Formatters::BaseFormatter &>(formatter);
84101 }
85102
86103 /* --------- Primary member functions -------------*/
87104
88105 /* * @brief Get the status of the object (success/failure) */
89- const bool get_status () { return this ->status ; }
106+ constexpr const bool get_status () noexcept { return this ->status ; }
107+ constexpr const bool get_status () const noexcept { return this ->status ; }
108+
109+ constexpr void failed () noexcept ; // Report failure to the object.
90110
91- void failed (); // Report failure to the object.
92- std::string padding (); // Calculate the padding for printing this object
111+ // Calculate the padding for printing this object
112+ std::string padding () noexcept ;
93113};
94114
95115/* >>>>>>>>>>>>>>>>>>>> Child IMPLEMENTATION <<<<<<<<<<<<<<<<<<<<<<<<<*/
@@ -100,7 +120,7 @@ class Child {
100120 * This is propogated up the parent/child tree, so that when a child object
101121 * fails, the parent object is immediately updated to reflect that as well.
102122 */
103- inline void Child::failed () {
123+ constexpr inline void Child::failed () noexcept {
104124 this ->status = false ;
105125 // propogates the failure up the tree
106126 if (has_parent ()) this ->get_parent ()->failed ();
@@ -110,36 +130,19 @@ inline void Child::failed() {
110130 * @brief Generate padding (indentation) fore the current object.
111131 * @return A string of spaces for use in pretty-printing.
112132 */
113- inline std::string Child::padding () {
133+ inline std::string Child::padding () noexcept {
114134 return has_parent () ? get_parent ()->padding () + " " : " " ;
115135}
116136
117- template <class C >
118- inline C Child::get_parent_as () {
119- // rejected branch should get optimized out at compile-time
120- if (CPPSPEC_DEBUG) {
121- if (C casted = dynamic_cast <C>(get_parent ()))
122- return casted;
123- else
124- throw (std::bad_cast ());
125- } else {
126- return static_cast <C>(get_parent ());
127- }
128- }
129-
130- inline const bool Child::has_formatter () {
137+ constexpr inline const bool Child::has_formatter () noexcept {
131138 if (this ->formatter != nullptr ) return true ;
132139 if (!this ->has_parent ()) return false ; // base case;
133140 return parent->has_formatter ();
134141}
135142
136- inline Formatters::BaseFormatter &Child::get_formatter () {
137- if (this ->formatter != nullptr ) return *formatter;
138- if (!this ->has_parent ()) {
139- std::cout << " Couldn't get printer!" << std::endl;
140- // base case. This should never *ever* happen
141- throw " Couldn't get printer!" ;
142- }
143+ constexpr inline Formatters::BaseFormatter &Child::get_formatter () noexcept {
144+ if (this ->formatter ) return *formatter;
145+ if (!this ->has_parent ()) std::terminate ();
143146 return parent->get_formatter ();
144147}
145148
0 commit comments