Good way to create observer design pattern in C++ - c++

I am trying to implement observer design pattern in C++ as below
#include <iostream>
#include <vector>
using namespace std;
class observer
{
public:
observer() = default;
~observer() = default;
virtual void notify() = 0;
};
class subject
{
vector <observer *> vec;
public:
subject() = default;
~subject() = default;
void _register(observer *obj)
{
vec.push_back(obj);
}
void unregister(observer *obj)
{
int i;
for(i = 0; i < vec.size(); i++)
{
if(vec[i] == obj)
{
cout << "found elem. unregistering" << endl;
vec.erase(vec.begin() + i);
break;
}
}
if(i == vec.size())
{
cout << "elem not found to unregister" << endl;
}
}
void notify()
{
vector <observer *>::iterator it = vec.begin();
while(it != vec.end())
{
(*it)->notify();
it ++;
}
}
};
class obsone : public observer
{
void notify()
{
cout << "in obsone notify" << endl;
}
};
class obstwo : public observer
{
void notify()
{
cout << "in obstwo notify" << endl;
}
};
int main()
{
subject sub;
obsone *one = new obsone();
obstwo *two = new obstwo();
sub._register(one);
sub._register(two);
sub.notify();
sub.unregister(one);
sub.notify();
//delete two;
//sub.notify();
return 0;
}
I am registering the objects with the subject explicitly. Is it the correct way of doing it or do I need to register through observer class only. Are there any problems with the above approach?

Here's an example of doing the callbacks with lambdas and function objects in the callback collection.
The details can vary greatly! So, this code is not “the” way, but just your code rewritten in one specific way, out of a myriad possibilities. But it hopefully shows the general idea in modern C++.
#include <iostream>
#include <functional> // std::function
#include <stdint.h> // uint64_t
#include <unordered_map> // std::unordered_map
#include <utility> // std::move
#include <vector> // std::vector
using namespace std;
namespace my
{
using Callback = function<void()>;
template< class Key, class Value > using Map_ = unordered_map<Key, Value>;
class Subject
{
public:
enum Id: uint64_t {};
private:
Map_<uint64_t, Callback> m_callbacks;
static auto id_value()
-> uint64_t&
{
static uint64_t the_id;
return the_id;
}
public:
auto add_listener( Callback cb )
-> Id
{
const auto id = Id( ++id_value() );
m_callbacks.emplace( id, move( cb ) );
return id;
}
auto remove_listener( const Id id )
-> bool
{
const auto it = m_callbacks.find( id );
if( it == m_callbacks.end() )
{
return false;
}
m_callbacks.erase( it );
return true;
}
void notify_all() const
{
for( const auto& pair : m_callbacks )
{
pair.second();
}
}
};
}
struct Observer_1
{
void notify() { cout << "Observer_1::notify() called." << endl; }
};
struct Observer_2
{
void notify() { cout << "Observer_2::notify() called." << endl; }
};
auto main()
-> int
{
my::Subject subject;
Observer_1 one;
Observer_2 two;
using Id = my::Subject::Id;
const Id listener_id_1 = subject.add_listener( [&]{ one.notify(); } );
const Id listener_id_2 = subject.add_listener( [&]{ two.notify(); } );
cout << "After adding two listeners:" << endl;
subject.notify_all();
cout << endl;
subject.remove_listener( listener_id_1 )
and (cout << "Removed listener 1." << endl)
or (cout << "Did not find registration of listener 1." << endl);
cout << endl;
cout << "After removing or attempting to remove listener 1:" << endl;
subject.notify_all();
}

Related

Working with std::unique_ptr and std::queue

Maybe it's my sinuses and that I fact that I just started learning about smart pointers today I'm trying to do the following:
Push to the queue
Get the element in the front
Pop the element (I think it will automatically deque once the address out of scope)
Here is the error
main.cpp:50:25: error: cannot convert ‘std::remove_reference&>::type’ {aka ‘std::unique_ptr’} to ‘std::unique_ptr*’ in assignment
50 | inputFrame = std::move(PacketQueue.front());
| ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
| |
| std::remove_reference<std::unique_ptr<MyObject::Packet>&>::type {aka std::unique_ptr<MyObject::Packet>}
Here is the code
#include <iostream>
#include <memory>
#include <queue>
using namespace std;
class MyObject
{
public:
struct Packet
{
uint8_t message;
uint8_t index;
};
void pushToQueue(void);
void FrontOfQueue(std::unique_ptr<Packet> *inputFrame);
private:
std::queue<std::unique_ptr<Packet>> PacketQueue;
};
void MyObject::pushToQueue(void)
{
Packet frame;
static int counter = 1;
frame.message = counter;
frame.index =counter;
counter++;
std::unique_ptr<Packet> passthru_ptr = std::make_unique<Packet>(std::move(frame));
PacketQueue.push(std::move(passthru_ptr));
cout<<"Pushed to queue\n" ;
}
void MyObject::FrontOfQueue(std::unique_ptr<Packet> *inputFrame)
{
inputFrame = std::move(PacketQueue.front());
}
int main()
{
cout<<"Hello World\n";
MyObject object;
object.pushToQueue();
object.pushToQueue();
{
// Scope
std::unique_ptr<MyObject::Packet> *frame;
object.FrontOfQueue(frame);
cout<< frame << endl;
}
{
// Scope
std::unique_ptr<MyObject::Packet> *frame2;
object.FrontOfQueue(frame2);
cout<< frame2 << endl;
}
return 0;
}
Link to the code (Online Compiler)
If I got your aim correctly, you definitely want
std::unique_ptr<MyObject::Packet> MyObject::FrontOfQueue()
{
auto rv = std::move(PacketQueue.front());
PacketQueue.pop();
return rv;
}
// ...
std::unique_ptr<MyObject::Packet> frame = object.FrontOfQueue();
Notice, no raw pointers are used.
I think it will automatically deque once the address out of scope.
This assumption is wrong. Nothing is dequeued until .pop() is called.
Here is my example with some extra logging to show whats going on.
includes an introduction of returning const references as well.
Live demo : https://onlinegdb.com/P2nFkdMy0
#include <iostream>
#include <memory>
#include <queue>
#include <string>
//-----------------------------------------------------------------------------
// do NOT use : using namespace std;
//-----------------------------------------------------------------------------
struct Packet
{
// moved to uint32_t for std::cout reasons.
// uint8_t is displayed as(special) characters
std::uint32_t index;
std::uint32_t message;
Packet() :
index{ next_index() },
message{ index }
{
std::cout << "created packet : " << index << "\n";
}
~Packet()
{
std::cout << "destroyed packet : " << index << "\n";
}
// small helper to not have to declare the static variable seperatly
static std::uint8_t next_index()
{
static int counter;
return counter++;
}
};
//-----------------------------------------------------------------------------
class MyObject
{
public:
void push_packet();
std::unique_ptr<Packet> pop_packet();
// this function returns a const reference (observation only)
// of the packet at the front of the queue
// while leaving the unique pointer on the queue (no moves needed
// packet will still be owned by the queue)
const Packet& front();
private:
std::queue<std::unique_ptr<Packet>> m_queue;
};
void MyObject::push_packet()
{
std::cout << "push_packet\n";
// push a packet
m_queue.push(std::make_unique<Packet>());
std::cout << "push_packet done...\n";
}
std::unique_ptr<Packet> MyObject::pop_packet()
{
std::unique_ptr<Packet> packet = std::move(m_queue.front());
m_queue.pop();
return packet;
}
const Packet& MyObject::front()
{
return *m_queue.front();
}
//-----------------------------------------------------------------------------
int main()
{
const std::size_t n_packets = 3ul;
MyObject object;
for (std::size_t n = 0; n < n_packets; ++n)
{
std::cout << "pushing packet\n";
object.push_packet();
}
for (std::size_t n = 0; n < n_packets; ++n)
{
std::cout << "packet at front : ";
std::cout << object.front().index << "\n";
std::cout << "popping front\n";
auto packet_ptr = object.pop_packet();
std::cout << "popped packet : " << packet_ptr->index << "\n";
}
return 0;
}

Is it possible to use std::make_unique in a class constructor and `this` pointer?

I am trying to use unique_ptr instead of raw pointers for components that are assigned to an actor class.
#include <iostream>
#include <memory>
#include <utility>
#include <algorithm>
#include <vector>
class Component;
class Actor
{
public:
void AddComponent(std::unique_ptr<Component> component)
{
std::cout << "Add component\n";
mComponents.push_back(std::move(component));
}
void RemoveComponent(Component* component)
{
std::cout << "Removing Component...\n";
auto iter = std::find_if(mComponents.begin(), mComponents.end(),[&](const auto& e){ return e.get() == component;});
if(iter != mComponents.end())
{
mComponents.erase(iter);
std::cout << "Removed from vector \n";
}
}
void ListComponents()
{
for(const auto& element : mComponents)
{
std::cout << "Component:" << element.get() << "\n";
}
}
private:
std::vector<std::unique_ptr<Component> > mComponents;
};
class Component
{
public:
Component(Actor* actor) : mOwner(actor) {actor->AddComponent(std::make_unique<Component>(*this));}
virtual ~Component() { std::cout << this << ":Destroyed \n"; }
private:
Actor* mOwner = nullptr;
};
int main(int argc, char* argv[])
{
Actor Actor1;
auto c1 = Component(&Actor1);
auto c2 = Component(&Actor1);
Actor1.ListComponents();
}
Is there a way to assign the this pointer to a unique_ptr, or am I right that I need to use a factory function of some kind?
Thanks!
EDIT: Corrected Code:
#include <iostream>
#include <memory>
#include <utility>
#include <algorithm>
#include <vector>
class Component;
class Actor
{
public:
void AddComponent(std::unique_ptr<Component> component)
{
std::cout << "Add component\n";
mComponents.push_back(std::move(component));
}
void RemoveComponent(Component* component)
{
std::cout << "Removing Component...\n";
auto iter = std::find_if(mComponents.begin(), mComponents.end(),[&](const auto& e){ return e.get() == component;});
if(iter != mComponents.end())
{
mComponents.erase(iter);
std::cout << "Removed from vector \n";
}
}
void ListComponents()
{
for(const auto& element : mComponents)
{
std::cout << "Component:" << element.get() << "\n";
}
}
private:
std::vector<std::unique_ptr<Component> > mComponents;
};
class Component
{
public:
Component(Actor* actor) : mOwner(actor) {actor->AddComponent(std::unique_ptr<Component>(this));}
virtual ~Component() { std::cout << this << ":Destroyed \n"; }
private:
Actor* mOwner = nullptr;
};
int main(int argc, char* argv[])
{
Actor Actor1;
auto c1 = new Component(&Actor1);
auto c2 = new Component(&Actor1);
Actor1.ListComponents();
}
There are at least three issues here.
There are various syntax errors (missing declaration of Component before the definition of Actor, typos, ... I'll ignore them now they are mentionned.
The if you have any pointer, you can build a unique_ptr from it which will handle the lifetime of the object. That's not made with make_unique but with the constructor:
owner->AddComponent(std::unique_ptr<Component>(this));
You can do that but the unique_pointer will manage the lifetime of the Component. So that means that nothing else should and you have to avoid that, especially temporaries and stack allocated one. So you'll have to replace:
c1 = Component(&Actor1);
c2 = Component(&Actor1);
by something like:
new Component(&Actor1);
new Component(&Actor1);
There are various way to enforce that so that mistakes are detected at compilation, but I'll not go into them.

Make a collection of an abstract class type, Abstract Class vector of shared_ptr

Error
e/c++/v1/algorithm:642:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/utility:321:9: error:
field type 'Space' is an abstract class
_T2 second;
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/map:624:16: note:
Question
How can I define a std::vector of type Space which is an abstract class and then fill this vector with instances of the derived classes Empty, Snake, Ladder.
Context
I know abstract classes in C++ can not be instantiated. Instead I've read in several posts on this and other sites that you can create a collection of an abstract type if it the type is defined as a star * pointer or any of the <memory> managed pointer data types like std::unqiue_ptr<T>. I've tried to used shared_ptr<Space> in my case, but still unable to define the collection properly. I am compiled my code using g++ -std=c++17 main.cpp && ./a.out.
Code
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <map>
#include <memory>
#include <typeinfo>
#include <queue>
#include <string>
#include <vector>
class Player
{
private:
int m_current_space = 1;
public:
Player() {}
void role_dice() {
m_current_space += floor( (rand()%10 + 1) / 3 );
}
int const get_current_space() {
return m_current_space;
}
void set_current_space(int current_space) {
m_current_space = current_space;
}
};
class Space
{
protected:
int m_id;
std::vector<Space> m_paths;
public:
Space() {} // requied to use [] operator in map
Space(int id) : m_id(id) {}
void add_path(Space& s) {
m_paths.push_back(s);
}
int get_id() {
return m_id;
}
virtual std::string class_type() = 0;
};
class Empty : public Space
{
public:
Empty(int id) : Space(id) {}
std::string class_type() {
return "Empty";
}
};
class Ladder : public Space
{
public:
Ladder(int id) : Space(id) {}
virtual void event(Player& p) {
p.set_current_space(1);
}
std::string class_type() {
return "Ladder";
}
};
class Snake : public Space
{
public:
Snake(int id) : Space(id) {}
virtual void event(Player& p) {
p.set_current_space(4);
}
std::string class_type() {
return "Snake";
}
};
class Board
{
private:
std::map<int, Space> m_board;
public:
void add_space(Space& s) {
m_board[s.get_id()] = s;
}
void draw_board() {
int i = 1;
for(auto const& [space_key, space] : m_board) {
if(i%3 == 0) {
std::cout << "○\n";
}
else if(typeid(space) == typeid(Snake)) {
std::cout << "○-";
}
else {
std::cout << "○ ";
}
++i;
}
}
void update_player_on_board(int position) {
int i = 1;
for(auto const& [space_key, space] : m_board) {
if(i%3 == 0) {
if (space_key == position) {
std::cout << "●\n";
}
else {
std::cout << "○\n";
}
}
else if(typeid(space) == typeid(Snake)) {
std::cout << "○-";
}
else {
if (space_key == position) {
std::cout << "● ";
}
else {
std::cout << "○ ";
}
}
++i;
}
}
const std::map<int, Space> get_board() {
return m_board;
}
friend std::ostream &operator<<(std::ostream& os, const Board& b) {
return os;
}
};
class GameStateManager
{
private:
std::string m_state = "game over";
bool m_playing = false;
public:
std::string const get_state() {
return m_state;
}
void set_state(std::string state) {
m_state = state;
}
};
int main()
{
std::cout << "Welcome to Bowser's 9 board game\n";
std::cout << "Start? y(yes) n(no)\n";
GameStateManager game_manager;
game_manager.set_state("playing");
auto space1 = std::make_shared<Space>(1);
auto space2 = std::make_shared<Space>(2);
auto space3 = std::make_shared<Space>(3);
auto space4 = std::make_shared<Space>(4);
auto space5 = std::make_shared<Space>(5);
auto space6 = std::make_shared<Space>(6);
auto space7 = std::make_shared<Space>(7);
auto space8 = std::make_shared<Space>(8);
auto space9 = std::make_shared<Space>(9);
std::vector<std::shared_ptr<Space>> v {
space1, space2, space3,
space4, space5, space6,
space7, space8, space9
};
Board bowsers_bigbad_laddersnake;
for(int i = 0; i < 10; ++i) {
bowsers_bigbad_laddersnake.add_space(*(v[i]));
}
bowsers_bigbad_laddersnake.draw_board();
Player mario;
int turn = 0;
while(game_manager.get_state() == "playing") {
std::cin.get();
std::cout << "-- Turn " << ++turn << " --" << '\n';
mario.role_dice();
bowsers_bigbad_laddersnake.update_player_on_board(mario.get_current_space());
if (mario.get_current_space() >= 9) {
game_manager.set_state("game over");
}
}
std::cout << "Thanks a so much for to playing!\nPress any key to continue . . .\n";
std::cin.get();
return 0;
}
You seem to have removed a lot of code to get into details here.
Have a Space pointer (smart or raw). Instantiate the specific space that you want, point to it with your pointer of type Space. Example std::shared_ptr<Space> pointerToSpace = std::make_shared<Snake> ("I'm a snake"); Now, without loss of generality, you can print the contents (of concrete type) with just the pointer to the space pointerToSpace->class_type(). Yes, you can have a collection of shared_ptrs in a container.

Inheritance , how can i make two classes share the same base class contents?

I have an Abstract Class operations that inherits from VAR Class , which then all the operations derived class(out,sleep,Add) inherit from the operations class. FSM Class inherits from Var also, so That I want one instance of VAR class inside my program.
I am trying to make vector < pair< string, int>> var as a shared data between the FSM class and the Operations class and its deviates . I initialized the var in the main through the FSM class .
Each time we call the exist function in VAR through Class operation , it returns it doesn't exits cause it is empty ! How can I overcome this?
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class VAR
{
public:
vector<pair<string, int>> var;
VAR()
{
cout << "created VAR" << endl;
}
~VAR(){ cout << "Destrioed VAR" << endl; }
void createVar(string x,int y)
{
pair<string, int>t;
t.first = x;
t.second = y;
var.push_back(t);
}
int getVarValue(string x)
{
for (int i = 0; i<var.size(); i++)
{
if (var[i].first == x)
{
return var[i].second;
}
}
}
void setVarValue(string& x, int y)
{
for (int i = 0; i<var.size(); i++)
{
if (var[i].first == x)
{
var[i].second = y;
i = var.size();
}
}
}
bool exits(string& name)
{
for (int i = 0; i<var.size(); i++)
{
if (var[i].first == name)
return true;
}
return false;
}
};
class operations : virtual public VAR
{
public:
operations()
{
cout << "operations created" << endl;
}
~operations()
{
cout << "operations Destroied" << endl;
}
void virtual excute() = 0;
};
class Out :public virtual operations
{
public:
string s;
Out(string xx = "") :s(xx)
{
cout << "Out created" << endl;
}
~Out()
{
cout << "Out Destroied" << endl;
}
void virtual excute()
{
cout << "out Class" << endl;
if (exits(s))
cout<<"it never reach here, WHY !"<<endl;
}
};
class Add :public virtual operations
{
public:
string s;
Add(string ss = "") :s(ss)
{
cout << "ADD created" << endl;
}
~Add()
{
cout << "Add Destroied" << endl;
}
void virtual excute()
{
string ex1 = s.substr(s.find('=') + 1, s.find('+')), ex2 = s.substr(s.find('+') + 1);
if (exits(ex1))
cout<<"it never reach here, WHY !"<<endl;
else
result = atoi(ex1.c_str());
if (exits(ex2))
cout<<"it never reach here, WHY !"<<endl;
}
};
class state
{
public:
vector<operations*> instructionList;
string name;
void exec_all()
{
for (int x = 0; x < instructionList.size(); x++)
instructionList[x]->excute();
}
};
class transition
{
public:
vector < pair<state, vector<pair<state, int>>>> trans;
static int currentState;
};
class FSM :public virtual VAR, public virtual transition
{
public:
FSM()
{
cout << "FSM" << endl;
}
void intialize()
{
createVar("X", 1);
createVar("Y", 5);
}
};
void main()
{
FSM x;
pair<state, vector<pair<state, int>>> p1;
pair<state, int>p2;
x.intialize();
p2.first.name = "b";
p2.second = 3;
p1.first.name = "a";
p1.second.push_back(p2);
x.trans.push_back(p1);
x.trans[0].first.instructionList.push_back(new Add("X=X+Y"));
x.trans[0].first.instructionList.push_back(new Out("X"));
x.trans[0].first.exec_all();//wrong output cause exist() returns false
}
A minimal complete example looks something like this:
#include <iostream>
using namespace std;
class VAR
{
public:
int var;
virtual ~VAR()
{}
void setVar(int n)
{var=n;}
};
class Out :public VAR
{};
class FSM :public VAR
{};
int main()
{
FSM x;
x.setVar(5);
Out OP;
if (x.var==OP.var)
cout<<"it reaches here now" << endl;
else
cout << "it fails" << endl;
return(0);
}
And one way to fix it is like this:
class VAR
{
public:
static int var;
int var;
virtual ~VAR()
{}
void setVar(int n)
{var=n;}
};
int VAR::var=0;

c++ How can I access the Vector Data created by Composition?

I'll go straight to an example, I think it is easier to underestand.
Music Cd has Tracks. How can I access A TrackInfo vector (XTrackInfo) data "inside" Music Cd class?
I want to print and even change values, I don't figure out how.
Thanks
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <numeric>
class XTrackInfo
{
std::string m_TrackName;
int m_Length;
public:
XTrackInfo() {}
XTrackInfo(std::string TrackName, int Length):
m_TrackName(std::move(TrackName)),
m_Length(Length)
{}
void SetTrackName(std::string TrackName) { m_TrackName = std::move(TrackName); }
void SetTrackLength(int Length) { m_Length = Length; }
const std::string& GetTrackName() const { return m_TrackName; }
int GetTrackLength() const { return m_Length; }
};
class XMusicCd
{
private:
std::string m_Author;
std::vector<XTrackInfo> m_TrackInfo;
public:
XMusicCd() {}
XMusicCd(std::string Author, std::vector<XTrackInfo> Tracks):
m_Author(std::move(Author)),
m_TrackInfo(std::move(Tracks))
{}
void SetAuthor(std::string Author) { m_Author = std::move(Author); }
const std::string& GetAuthor() const { return m_Author; }
const std::vector<XTrackInfo> GetTracks() const { return m_TrackInfo;}
int GetLength() const; // Left incomplete on purpose; you will implement it later
void AddTrack(XTrackInfo NewTrack){
m_TrackInfo.emplace_back(std::move(NewTrack));
}
};
void PrintCdContents(const XMusicCd& Cd)
{
std::cout << "Author : " << Cd.GetAuthor() << "\n";
std::cout << "\n" << std::endl;
std::cout << "Track Info" << std::endl;
//problems here :)
}
int main()
{
// You may not change this function
XMusicCd MyCd;
MyCd.SetAuthor("Hello World");
MyCd.AddTrack(XTrackInfo("This is a test", 100));
MyCd.AddTrack(XTrackInfo("This is a test 2", 200));
PrintCdContents(MyCd);
}
Use iterators:
std::vector<XTrackInfo> tracks = Cd.GetTracks();
for (std::vector<XTrackInfo>::const_iterator it = tracks.begin(); it != tracks.end(); ++it) {
std::cout << it->GetTrackName() << std::endl;
}
Or indexes:
std::vector<XTrackInfo> tracks = Cd.GetTracks();
for (unsigned i = 0; i < tracks.size(); ++i) {
std::cout << tracks.at(i).GetTrackName() << std::endl;
}