I've been recently working on an approach to make it safer and easier to write code with many types implementing different combinations of the same few interfaces.
In my (C++17) environment:
RTTI is disabled
There are 5-10 interfaces an object may derive from (any number of them)
There are <100 objects at any given time
The idea is to create a type that holds a pointer for each base class that an object may implement:
template
class poly_view {
std::tuple _ptrs;
public:
// Initialize tuple with either valid ptr or nullptr based on
// if Derived derives from each base
template
constexpr explicit poly_view(Derived *obj)
: _ptrs{ ([obj](){
if constexpr (std::is_base_of_v)
return static_cast(obj);
else
return nullptr;
}())... } {}
// Retrieve a pointer
template
auto *get() const { return std::get(_ptrs); }
};
(simplest implementation)
A companion class poly_ref stores references instead of pointers, so every class in PossibleBases has a valid reference stored. A poly_ref can be initialized from a poly_view if the view holds a valid A* and B*.
Example of how the types/helper functions are used:
// Objects in a Digital Audio Workstation that may
// be Recordable, Playable, or Quantizable
struct AudioTrack : public Recordable,
public Playable {
void record() override { std::cout << "recording audio\n"; }
void play() override { std::cout << "playing audio\n"; }
};
struct MidiTrack : public Recordable,
public Playable,
public Quantizable {
void quantize() override { std::cout << "quantizing midi\n"; }
void record() override { std::cout << "recording midi\n"; }
void play() override { std::cout << "playing midi\n"; }
};
struct AudioClip : public Playable {
void play() override { std::cout << "playing audio clip\n"; }
};
struct MidiClip : public Playable,
public Quantizable {
void play() override { std::cout << "playing midi clip\n"; }
void quantize() override { std::cout << "quantizing midi clip\n"; }
};
void record_then_play(nst::poly_ref obj,
std::chrono::milliseconds delay_ms) {
obj.as().record();
std::this_thread::sleep_for(delay_ms);
obj.as().play();
}
int main() {
using namespace nst;
using DAWObjView = poly_view;
std::vector obj_views;
// Create poly_views to objects
obj_views.emplace_back(new AudioTrack);
obj_views.emplace_back(new AudioClip);
obj_views.emplace_back(new MidiTrack);
obj_views.emplace_back(new MidiClip);
// If object at index 0 implements Recordable, record it
const auto &obj_view = obj_views[0];
if (obj_view.has_base_of()) {
// normally unsafe, but we checked
static_cast(obj_view)->record();
}
// Does nothing if obj_views[2] is not Playable.
invoke_if(obj_views[2], [](auto &obj){ obj.play(); });
// Quantize every quantizable object
for_each(obj_views, [](auto &obj){ obj.quantize(); });
// for each object that is both Recordable and Playable,
// record it, wait 1 sec, then play
for_each(obj_views, [](auto obj){
record_then_play(obj, std::chrono::milliseconds{1000});
});
}
It makes it easy to wrap the unsafe operations and to get references out instead of dealing with pointers and null checks. poly_refs and poly_views are convertible to their subset types, so record_then_play can ask for just a poly_ref
Wondering if anyone has other approaches or ideas to improve this!