How do I make Singleton generic template,
and how can I test it?
Right now I am interested in seeing with my own eyes that 2 threads that invoke get_instance() will get the same pointer to the same singleton.
// g++ singleton.cpp -std=c++2a -pthread -o singleton
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <mutex>
#include <thread>
#include <chrono>
#include <pthread.h>
#include <future>
using namespace std;
template <class T>
struct Singleton
{
Singleton(const Singleton &) = delete;
Singleton &operator=(const Singleton &) = delete;
static T &get_instance()
{
static T _{allow()};
return _;
}
private:
struct allow
{
};
protected:
Singleton(allow) {}
};
class A : public Singleton<A>
{
using Singleton<A>::Singleton;
// Rest of functionality for class A
};
int main()
{
auto &x = Singleton<A>::get_instance();
auto &y = A::get_instance();
printf("%p\n", &x);
printf("%p\n", &y);
void *p1;
void *p2;
// thread worker1(Singleton<A>::get_instance);
// thread worker2(Singleton<A>::get_instance);
// worker1.join();
// worker2.join();
printf("%p\n", p1);
printf("%p\n", p2);
// compiler error
// auto z = A();
}
Related
I am getting attempting to reference a deleted function error which I feel is because of inter dependency between classes.
Location.h
#ifndef A_LOCATION_H
#define A_LOCATION_H
struct location {
double lat;
double lon;
double alt;
};
#endif //A_LOCATION_H
P.h
#ifndef A_P_H
#define A_P_H
#include <vector>
#include <mutex>
#include <memory>
#include "Location.h"
class C;
class P {
std::vector<std::shared_ptr<C>> C_List;
struct location loc {};
public:
P() = default;
~P() = default;
std::mutex mut;
void add_child(const std::string& th_name);
void del();
void set_data(double lat, double lon, double alt);
struct location get_data();
};
#endif //A_P_H
P.cpp
#include <iostream>
#include "C.h"
#include "P.h"
void P::add_child(const std::string& th_name) {
std::lock_guard<std::mutex> lg(mut);
auto& ref = C_List.emplace_back(std::make_shared<C>());
ref->set_name(th_name);
ref->set_P(this);
ref->start();
}
void P::del() {
std::lock_guard<std::mutex> lg(mut);
for (auto& c : C_List)
c->terminate = true;
for (auto& c : C_List)
c->wait();
C_List.clear();
}
struct location P::get_data() {
std::lock_guard<std::mutex> lg(mut);
return loc;
}
void P::set_data(double lat, double lon, double alt) {
std::lock_guard<std::mutex> lg(mut);
loc.lat = lat;
loc.lon = lon;
loc.alt = alt;
}
C.h
#ifndef A_C_H
#define A_C_H
#include <string>
#include <thread>
#include <chrono>
#include <atomic>
class P;
class C {
P *p {};
std::string name {};
std::thread th {};
struct location loc {};
void run();
public:
C() = default;
~C() = default;
void set_P(P* p);
void set_name(const std::string& name);
void start();
void wait();
std::atomic<bool> terminate {false};
};
#endif //A_C_H
C.cpp
#include <iostream>
#include "P.h"
#include "C.h"
void C::run() {
while (!terminate) {
std::cout << name << std::endl;
{
auto loc = p->get_data();
// perform calculation based on P's object location, and it's current location
}
using namespace std::chrono_literals;
std::this_thread::sleep_for(1s);
}
}
void C::set_P(P* p) {
this->p = p;
}
void C::set_name(const std::string& name) {
this->name = name;
}
void C::start() {
th = std::thread(&C::run, this);
}
void C::wait() {
th.join();
}
Main.cpp
#include <iostream>
#include "P.h"
int main() {
P p = P();
p.add_child("C1");
p.add_child("C2");
p.add_child("C3");
char input;
std::cin >> input;
p.del();
}
Also there exists a kind of deadlock that will happen when del function of P's object gets called. I am not getting how to resolve this issue?
This is the short description of the error I'm getting
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\xmemory(671): error C2280: 'C::C(const C &)': attempting to reference a deleted function
C:\Users\HARSHA\Desktop\LC\2022\A\C.h(33): note: compiler has generated 'C::C' here
C:\Users\HARSHA\Desktop\LC\2022\A\C.h(33): note: 'C::C(const C &)': function was implicitly deleted because a data member invokes a deleted or inaccessible function 'std::thread::thread(const std::thread &)'
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\thread(93): note: 'std::thread::thread(const std::thread &)': function was explicitly deleted
std::thread, std::atomic, std::mutex types are not copyable, so the compiler cannot produce the default copy-constructors and copy-asignments.
You must write your own copy constructors for C and P classes.
P(P const& arg){ ... do stuff ... }
I tried to use unique_ptr in c++ in a singelton pattern instead of raw pointer. when I want to assign a unique_ptr to another I got an error. I tried to use std::move to assign but it did not work. the code is as follow:
#include <iostream>
#include <memory>
#include <list>
#include <algorithm>
#include <iterator>
#include <string>
using namespace std;
class ClientDB
{
private:
static unique_ptr<ClientDB> theDB;
ClientDB() {}
list<string> clients;
public:
~ClientDB() {}
static unique_ptr<ClientDB> getInstance()
{
if(theDB==nullptr)
theDB = make_unique<ClientDB>;
return theDB;
}
void addClient(string c) {clients.push_back(c);}
void printClients(ostream& os)
{
copy(clients.cbegin(),clients.cend(),ostream_iterator<string>{os,"\n"});
}
};
int main()
{
unique_ptr<ClientDB> db1{ClientDB::getInstance()};
db1->addClient("Mr. Schultz");
unique_ptr<ClientDB> db2{ClientDB::getInstance()};
db2->addClient("Mrs. James");
unique_ptr<ClientDB> db3{ClientDB::getInstance()};
db3->addClient("Mr. Karajan");
db1->printClients(cout);
}
and the error I got is
error: no match for ‘operator=’ (operand types are ‘std::unique_ptr<ClientDB>’ and ‘<unresolved overloaded function type>’)
theDB = make_unique<ClientDB>;
and another question is if nullptr can be used for unique_ptr.
Finally by help of my teacher, I can solve the problem. there is some points should be considered.
1- for unique_ptr assignment, std::move:: should be used.
2- make_unique has no access to the private constructor, the constructor should be called explicitly:
theDB = unique_ptr<ClientDB>(new ClientDB());
3-The unique-ptr must be initialized outside the class:
unique_ptr<ClientDB> ClientDB::theDB;
4- Three unique pointers in main for the same object can not be used, only one is allowed for uniqueness. references to unique_ptr should be used instead:
unique_ptr<ClientDB>& db1=ClientDB::getInstance();
and finally the code should be like this
#include <iostream>
#include <memory>
#include <list>
#include <algorithm>
#include <iterator>
#include <string>
using namespace std;
class ClientDB
{
private:
static unique_ptr<ClientDB> theDB;
ClientDB() {}
list<string> clients;
public:
~ClientDB() {}
static unique_ptr<ClientDB>& getInstance()
{
if(theDB==nullptr)
//theDB = move(make_unique<ClientDB>());
theDB = unique_ptr<ClientDB>(new ClientDB());
return theDB;
}
void addClient(string c) {clients.push_back(c);}
void printClients(ostream& os)
{
copy(clients.cbegin(),clients.cend(),ostream_iterator<string>{os,"\n"});
}
};
unique_ptr<ClientDB> ClientDB::theDB;
int main()
{
unique_ptr<ClientDB>& db1=ClientDB::getInstance();
db1->addClient("Mr. Schultz");
unique_ptr<ClientDB>& db2=ClientDB::getInstance();
db2->addClient("Mrs. James");
unique_ptr<ClientDB>& db3=ClientDB::getInstance();
db3->addClient("Mr. Karajan");
db1->printClients(cout);
}
This question already has answers here:
Adding Elements to std::vector of an abstract class
(4 answers)
Closed 5 years ago.
I have a problem with my code.
I have three classes, and one of them is a pure abstract class. I don't know why I receive the error:
'note' cannot instantiate abstact class.
It may be because of STL usage, or I have made a mistake and I dont see it.
The problem is I tried without STL and it works, and I don't know what is the problem here because I think it it correct.
#pragma once
class note
{
protected:
int ziua;
int ora;
public:
note();
note(int day,int hour);
virtual void print()=0;
virtual ~note();
};
#include "note.h"
note::note()
{
}
note::note(int day, int hour) :ziua(day), ora(hour)
{
}
note::~note()
{
}
#pragma once
#include "note.h"
#include <iostream>
class apeluri:public note
{
char *numar_telefon;
public:
apeluri();
apeluri(int day, int h, char*phone);
void print()
{
printf("%d %d %s", ziua, ora, numar_telefon);
}
~apeluri();
};
#pragma once
#include <iostream>
#include "apeluri.h"
#include <vector>
#include "note.h"
using namespace std;
class remainder
{
vector<note> t;
public:
remainder();
void addsedinta(int zi, int ora, int durata, char*subi);
void addapel(int zi, int ora, char*phon)
{
apeluri *f;
f = new apeluri(zi, ora, phon);
t.push_back(*f);
}
void show()
{
}
~remainder();
};
In your remainder class, using vector<note> is illegal. note is abstract, so the vector can't create note objects.
Even if note were not abstract, your code would still not work correctly, because it would be affected by object slicing.
To store derived objects in a container of base classes, you must use pointers instead, ie vector<note*>:
#pragma once
#include <iostream>
#include <vector>
#include "note.h"
#include "apeluri.h"
using namespace std;
class remainder
{
private:
vector<note*> t;
remainder(const remainder &) {}
remainder& operator=(const remainder &) { return *this; }
public:
remainder();
~remainder()
{
for(std::vector<note*>::iterator i = t.begin(); i != t.end(); ++i) {
delete *i;
}
}
void addsedinta(int zi, int ora, int durata, char*subi);
void addapel(int zi, int ora, char*phon)
{
apeluri *f = new apeluri(zi, ora, phon);
t.push_back(f);
}
void show()
{
}
};
If you are using C++11 or later, this would be better written as this instead:
#pragma once
#include <iostream>
#include <vector>
#include <memory>
#include "note.h"
#include "apeluri.h"
using namespace std;
class remainder
{
private:
vector<unique_ptr<note>> t;
public:
remainder();
remainder(const remainder &) = delete;
remainder& operator=(const remainder &) = delete;
void addsedinta(int zi, int ora, int durata, char*subi);
void addapel(int zi, int ora, char*phon)
{
t.push_back(std::unique_ptr<apeluri>(new apeluri(zi, ora, phon)));
// in C++14 and later, use this instead:
// t.push_back(std::make_unique<apeluri>(zi, ora, phon));
}
void show()
{
}
};
I am experiencing problems serializing a polymorphic type. Actually I just split the example in: http://uscilab.github.io/cereal/polymorphism.html in several files. It compiles just fine but in runtime i get an exception telling me that I cannot serialize a polymorphic type which is not registered when reaching this line in the code:
oarchive( ptr1, ptr2 );
which is supposed to serialize the contents of ptr1 and ptr2 to an stream.
I attach the files so that anybody can see what's going on.
Thanks in advance for your time!
Best,
Roger.
////////////// IBaseClass.h
#ifndef _IBASECLASS_H_
#define _IBASECLASS_H_
// A pure virtual base class
class IBaseClass
{
public:
virtual void sayType() = 0;
};
#endif
////////////// DerivedClass.h
#ifndef DERIVEDCLASS_H_
#define DERIVEDCLASS_H_
#include "IBaseClass.h"
#include <cereal/types/polymorphic.hpp>
class DerivedClass : public IBaseClass {
void sayType();
int x;
template<class Archive>
void serialize( Archive & ar )
{ ar( x ); }
};
#include <cereal/archives/binary.hpp>
#include <cereal/archives/xml.hpp>
#include <cereal/archives/json.hpp>
// Register DerivedClassOne
CEREAL_REGISTER_TYPE(DerivedClass);
#endif /* DERIVEDCLASS_H_ */
////////////// DerivedClass2.h
#ifndef DERIVEDCLASS2_H_
#define DERIVEDCLASS2_H_
#include "IBaseClass.h"
#include <cereal/types/polymorphic.hpp>
class DerivedClass2 : public IBaseClass {
void sayType();
float y;
template<class Archive>
void serialize( Archive & ar )
{ ar( y ); }
};
#include <cereal/archives/binary.hpp>
#include <cereal/archives/xml.hpp>
#include <cereal/archives/json.hpp>
CEREAL_REGISTER_TYPE(DerivedClass2);
////////////// main.cpp
#include "DerivedClass.h"
#include "DerivedClass2.h"
#include <iostream>
#include <fstream>
#include <memory>
#include <cereal/archives/xml.hpp>
#include <cereal/types/polymorphic.hpp>
int main(int argc, char* argv[])
{
{
std::ofstream os( "polymorphism_test.xml" );
cereal::XMLOutputArchive oarchive( os );
// Create instances of the derived classes, but only keep base class pointers
std::shared_ptr<IBaseClass> ptr1 = std::make_shared<DerivedClass>();
std::shared_ptr<IBaseClass> ptr2 = std::make_shared<DerivedClass2>();
oarchive( ptr1, ptr2 );
}
{
std::ifstream is( "polymorphism_test.xml" );
cereal::XMLInputArchive iarchive( is );
// De-serialize the data as base class pointers, and watch as they are
// re-instantiated as derived classes
std::shared_ptr<IBaseClass> ptr1;
std::shared_ptr<IBaseClass> ptr2;
iarchive( ptr1, ptr2 );
// Ta-da! This should output:
ptr1->sayType(); // "DerivedClassOne"
ptr2->sayType(); // "EmbarrassingDerivedClass. Wait.. I mean DerivedClassTwo!"
}
return 0;
}
https://uscilab.github.io/cereal/polymorphism.html
As you are not doing any serialisation on cereal::base_class(this), there is no path from your derived classes to the base classes. Try adding:
CEREAL_REGISTER_POLYMORPHIC_RELATION(BaseClass, DerivedClassOne)
CEREAL_REGISTER_POLYMORPHIC_RELATION(BaseClass, EmbarrassingDerivedClass)
I'm new with the Boost library, and I got a problam a bit complex for me.
I tried to reformulate it with an example found in previous question that might fit well my problem.
(The previous question is here)
#include <boost/bind.hpp>
#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
class Base
: public boost::enable_shared_from_this<Base>,
private boost::noncopyable
{
public:
virtual void test() = 0;
protected:
virtual void foo(int i) = 0;
};
class Derived
: public Base
{
protected:
void foo(int i)
{ std::cout << "Base: " << i << std::endl; }
std::map<int, int> data;
public:
Derived()
{
data[0] = 5;
data[1] = 6;
data[2] = 7;
}
void test()
{
std::for_each(data.begin(), data.end(),
boost::bind(&Derived::foo, shared_from_this(),
boost::bind(&std::map<int, int>::value_type::second, _1)));
}
};
typedef boost::shared_ptr<Base> Base_ptr;
int main(int, const char**)
{
std::set<Base_ptr> Bases_;
Base_ptr derived(new Derived());
Bases_.insert(derived);
derived->test();
return 0;
}
I have a base object which is contained in a set, and different derived objects (in this example, only one).
The derived object should call his own protected method with a boost::bind.
In the real problem, the boost::bind generate a callback method for an asynchronous operation, it's why (I think) I need a shared_ptr.
Otherwise, using the pointer this instead of shared_from_this() resolve the problem.
When I compile this code, I got a long error message ended with (which I think is the most significant part):
bind_test.cpp:43:78: instantiated from here
/usr/include/boost/bind/mem_fn_template.hpp:156:53: error: pointer to member type ‘void (Derived::)(int)’ incompatible with object type ‘Base’
/usr/include/boost/bind/mem_fn_template.hpp:156:53: error: return-statement with a value, in function returning 'void'
I tried to manage with more inheritance from enable_shared_from_this, and some static cast :
#include <boost/bind.hpp>
#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
class Base
: public boost::enable_shared_from_this<Base>,
private boost::noncopyable
{
public:
virtual void test() = 0;
protected:
virtual void foo(int i) = 0;
};
class Derived
: public boost::enable_shared_from_this<Derived>,
public Base
{
protected:
void foo(int i)
{ std::cout << "Base: " << i << std::endl; }
std::map<int, int> data;
public:
Derived()
{
data[0] = 5;
data[1] = 6;
data[2] = 7;
}
void test()
{
std::for_each(data.begin(), data.end(),
boost::bind(&Derived::foo, boost::enable_shared_from_this<Derived>::shared_from_this(),
boost::bind(&std::map<int, int>::value_type::second, _1)));
}
};
typedef boost::shared_ptr<Base> Base_ptr;
int main(int, const char**)
{
std::set<Base_ptr> Bases_;
Base_ptr derived(new Derived());
Bases_.insert(derived);
derived->test();
return 0;
}
But I got an error at run-time :
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_weak_ptr> >'
what(): tr1::bad_weak_ptr
Might someone have a clue about how to manage that ?
Thanks.
Etienne.
It works with this workaround, but I'm not satisfied with it, so if someone find a better solution, go ahead.
#include <boost/bind.hpp>
#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
class Base
: public boost::enable_shared_from_this<Base>,
private boost::noncopyable
{
public:
virtual void test() = 0;
//protected:
virtual void foo(int i) = 0;
};
class Derived
: public Base
{
protected:
void foo(int i)
{ std::cout << "Base: " << i << std::endl; }
std::map<int, int> data;
public:
Derived()
{
data[0] = 5;
data[1] = 6;
data[2] = 7;
}
void test()
{
std::for_each(data.begin(), data.end(),
boost::bind(&Base::foo, shared_from_this(),
boost::bind(&std::map<int, int>::value_type::second, _1)));
}
};
typedef boost::shared_ptr<Base> Base_ptr;
int main(int, const char**)
{
std::set<Base_ptr> Bases_;
Base_ptr derived(new Derived());
Bases_.insert(derived);
derived->test();
return 0;
}