Skip to content

Instantly share code, notes, and snippets.

@kumailxp
Created March 31, 2024 15:20
Show Gist options
  • Save kumailxp/b577da88e21bc2c617f3b1726e539a8a to your computer and use it in GitHub Desktop.
Save kumailxp/b577da88e21bc2c617f3b1726e539a8a to your computer and use it in GitHub Desktop.
different unique pointer tests
#include <string>
#include <memory>
#include <iostream>
#include <vector>
#include <optional>
#include <mutex>
#include <chrono>
#include <ctime>
#include <thread>
using namespace std;
std::mutex mtx;
struct Student {
string name;
int age;
int height;
int weight;
};
std::unique_ptr<Student> createStudent(string name, int age, int height, int weight) {
std::unique_ptr<Student> student = std::make_unique<Student>();
student->name = name;
student->age = age;
student->height = height;
student->weight = weight;
return student;
}
std::ostream& operator<<(std::ostream& os, const Student& student) {
os << "Name: " << student.name << ", Age: " << student.age
<< ", Height: " << student.height << ", Weight: " << student.weight;
return os;
}
struct School {
string name;
string address;
};
std::unique_ptr<School> createSchool(string name, string address) {
std::unique_ptr<School> school = std::make_unique<School>();
school->name = name;
school->address = address;
return school;
}
std::ostream& operator<<(std::ostream& os, const School& school) {
os << "Name: " << school.name << ", Address: " << school.address;
return os;
}
using Student_t = std::vector<std::unique_ptr<Student>>;
template<typename... Args>
Student_t createStudentList(Args&&... students) {
Student_t studentList;
studentList.reserve(3);
// for(const auto& student : {std::forward<Args>(std::move(students))...})
// studentList.emplace_back(std::move(student));
(studentList.emplace_back(std::move(std::forward<Args>(students))), ...);
return studentList;
}
struct SchoolDirectory {
std::unique_ptr<School> school;
Student_t student;
};
std::ostream& operator<<(std::ostream& os, const SchoolDirectory& directory) {
os << "School: " << *(directory.school) << std::endl;
for (const auto& student : directory.student) {
os << *student << std::endl;
}
return os;
}
template<typename... Args>
SchoolDirectory createSchoolDirectory1(std::unique_ptr<School> school, Args&&... students) {
SchoolDirectory directory;
directory.school = std::move(school);
auto pT = createStudentList(std::forward<Args>(students)...);
directory.student = std::move(pT);
return directory;
}
SchoolDirectory createSchoolDirectory(std::unique_ptr<School> school, Student_t students) {
SchoolDirectory directory;
directory.school = std::move(school);
directory.student = std::move(students);
return directory;
}
SchoolDirectory sd;
void addNewStudents(int threadId) {
int i = 0;
sd.school = createSchool("School19", "Address19");
while (i < 3)
{
std::lock_guard<std::mutex> lock(mtx); // Lock the mutex
sd.student.emplace_back(createStudent("Student9", 10+i, 100, 1000));
std::this_thread::sleep_for(std::chrono::seconds(1));
i++;
}
}
struct Base {
virtual int f() = 0;
virtual ~Base() {}
};
struct A : public Base {
A() = default;
int f() override { return 1;}
virtual ~A() {}
};
optional<shared_ptr<Base>> create() { return make_shared<A>(); }
int main() {
auto aa = create();
cout << (*aa)->f() << endl;
/*
Student_t listOfStudents;
//= {createStudent("Student1", 10, 100, 50),
// createStudent("Student2", 10, 100, 50),
// createStudent("Student3", 10, 100, 50)};
auto a = createStudent("Student1", 10, 100, 500);
//listOfStudents.emplace_back(std::move(a));
listOfStudents.emplace_back(createStudent("Student2", 10, 100, 1000));
listOfStudents.emplace_back(createStudent("Student3", 10, 100, 50));
Student_t ss = createStudentList(createStudent("Student7", 10, 100, 1000),createStudent("Student8", 10, 100, 700));
for (const auto &student : ss)
std::cout << *student << std::endl;
auto school = createSchool("School1", "Address1");
auto studentInfo = createSchoolDirectory(std::move(school), std::move(listOfStudents));
std::cout << studentInfo << std::endl;
std::cout << *a << std::endl;
std::cout << *a << std::endl;
auto school2 = createSchool("School1", "Address1");
SchoolDirectory studentInfo1 = createSchoolDirectory1(std::move(school2), createStudent("Student9", 10, 100, 1000), createStudent("Student19", 10, 100, 1000));
std::cout << studentInfo1 << std::endl;
studentInfo1.student[1]->name = "Ali";
std::cout << studentInfo1 << std::endl;
auto school3 = createSchool("School3", "Address3");
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment