Skip to content

Instantly share code, notes, and snippets.

View mortymacs's full-sized avatar

Morteza NourelahiAlamdari mortymacs

View GitHub Profile
@mortymacs
mortymacs / main.cpp
Created September 5, 2025 08:11 — forked from GavinRay97/main.cpp
P2590R2: "Explicit lifetime management" (start_lifetime_as) implementation
// How can we make this program well-defined without sacrificing efficiency? If the destination type
// is a trivially-copyable implicit-lifetime type, this can be accomplished by copying the storage
// elsewhere, using placement new of an array of byte-like type, and copying the storage back to its
// original location, then using std::launder to acquire a pointer to the newly-created object, and
// finally relying on the compiler to optimise away all the copying. However, this would be very
// verbose and hard to get right. For expressivity and optimisability, a combined operation to
// create an object of implicit-lifetime type in-place while preserving the object representation
// may be useful. This is exactly what std::start_lifetime_as is designed to do
template<class T>
@mortymacs
mortymacs / main.py
Last active October 6, 2024 22:11
sample python design pattern
In [5]: from abc import ABC, abstractmethod
...: class Base(ABC):
...: @abstractmethod
...: def get_by_id(self, id):
...: pass
...: class C1(Base):
...: def get_by_id(self, id):
...: return f"hello client1: {id}"
...: class C2(Base):
...: def get_by_id(self, id):
@mortymacs
mortymacs / main.c
Created August 25, 2024 08:12
Sample Unittest in C
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
static void db_connection(void **state)
{
(void) state;
}
@mortymacs
mortymacs / main.c
Created August 25, 2024 08:09
C Pointer playground
#include <stdio.h>
#include <stdlib.h>
int *x()
{
int* a = malloc(sizeof(int));
*a = 19;
return a;
}
@mortymacs
mortymacs / main.c
Created August 25, 2024 08:06
Check file in C
#include <unistd.h>
#include <stdio.h>
int main()
{
if(access("a.sh", F_OK) != -1)
{
printf("Exists!\n");
}
else
@mortymacs
mortymacs / main.c
Created August 25, 2024 08:04
Dictionary by Glib in C
//gcc dict.c `pkg-config --libs --cflags glib-2.0`
#include <glib.h>
#include <stdio.h>
int main()
{
GHashTable* hash = g_hash_table_new(g_str_hash, g_str_equal);
g_hash_table_insert(hash, "name", "Mort");
g_hash_table_insert(hash, "email", "hello@localhost");
@mortymacs
mortymacs / main.c
Created August 25, 2024 08:03
Get GNU/Linux user group in C
#include <stdio.h>
#include <pwd.h>
#include <grp.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
struct passwd *pwd;
struct group *grp;
@mortymacs
mortymacs / main.c
Created August 25, 2024 08:02
Temp file in C
#include <stdio.h>
int main()
{
char b[255];
FILE *f;
f = tmpfile();
fputs("hello mr dj", f);
@mortymacs
mortymacs / main.c
Created August 25, 2024 07:59
Get ENV in C
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *r = getenv("JAVA_HOME");
printf("java home: %s\n", r);
}