Skip to content

Instantly share code, notes, and snippets.

@RklAlx
RklAlx / SharePtr.cpp
Created September 27, 2013 12:01
std::shared_ptr
std::shared_ptr<int> p1(new int(5));
std::shared_ptr<int> p2 = p1; //Both now own the memory.
p1.reset(); //Memory still exists, due to p2.
p2.reset(); //Deletes the memory, since no one else owns the memory.
int value = p1.get();
@RklAlx
RklAlx / WeakPtr.cpp
Created September 27, 2013 12:02
std::weak_ptr
/* A ​std::shared_ptr​ uses reference counting, so circular references are potentially a problem.
To break up cycles, ​std::weak_ptr​ can be used to access the stored object.
The stored object will be deleted if the only references to the object are ​weak_ptr​ references.
​weak_ptr​ therefore does not ensure that the object will continue to exist, but it can ask for the resource. */
std::shared_ptr<int> p1(new int(5));
std::weak_ptr<int> wp1 = p1; //p1 owns the memory.
{
std::shared_ptr<int> p2 = wp1.lock(); //Now p1 and p2 own the memory.
if(p2) //Always check to see if the memory still exists
@RklAlx
RklAlx / Max.cpp
Created September 27, 2013 12:04
std::for_each
//MAX using Lambda
std::vector<int> m_MsgTypeMaximumSize;
m_MsgTypeMaximumSize.push_back(7);
m_MsgTypeMaximumSize.push_back(5);
m_MsgTypeMaximumSize.push_back(20);
m_MsgTypeMaximumSize.push_back(1);
m_MsgTypeMaximumSize.push_back(3);
int max = 0;
@RklAlx
RklAlx / Sync.cpp
Created September 27, 2013 12:07
Critical Section
#include "Sync.h"
Sync::Sync()
{
InitializeCriticalSection(&m_cs);
}
Sync::~Sync()
{
DeleteCriticalSection(&m_cs);
@RklAlx
RklAlx / CThreadObj.cpp
Created September 27, 2013 12:09
Threads
bool CMyObj::StartThread()
{
m_Running = true;
m_hThreadHandle = CreateThread(NULL,0,ThreadFunction,this,0,&this->m_ThreadID);
if(m_hThreadHandle == nullptr)
m_Running = false;
return m_Running
}
@RklAlx
RklAlx / Compare.cpp
Created September 27, 2013 12:11
InterlockedCompare
/*
Syntax:
LONG __cdecl InterlockedCompareExchange(__inout LONG volatile *Destination,
__in LONG Exchange,
__in LONG Comparand
);
Returns the initial value of 'Destination'.
Note:
@RklAlx
RklAlx / Sync.cpp
Created September 27, 2013 14:50
Critical Section for Linux
// ---
// - Creating critical section variable -
// ---
pthread_mutex_t m;
// ---
// - Initializing -
//--
@RklAlx
RklAlx / strtrim.h
Created October 16, 2013 09:14
std::string trim
static inline std::string &ltrim(std::string &s){
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
static inline std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
@RklAlx
RklAlx / map.cpp
Created November 4, 2013 16:46
std::map
#include <map>
#include <string>
using std::map;
using std::string;
class CTeste
{
public:
CTeste(){}
@RklAlx
RklAlx / showstoredproc.sql
Created February 18, 2014 09:04
Showing a stored procedure
SELECT prosrc FROM pg_proc WHERE proname = <function name>;