Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / UniquePtr.cpp
Created September 27, 2013 12:01
std::unique_ptr
std::unique_ptr<int> p1(new int(5));
std::unique_ptr<int> p2 = p1; //Compile error.
std::unique_ptr<int> p3 = std::move(p1); //Transfers ownership. p3 now owns the memory and p1 is rendered invalid.
p3.reset(); //Deletes the memory.
p1.reset(); //Does nothing.
@RklAlx
RklAlx / CC.cpp
Created September 27, 2013 12:00
Private Constructor and make_shared: Solution: Using Pass-Key-Idiom Pattern URL: http://anthony-arnold.com/2012/04/05/three-fun-cpp-techniques/
#include "C.h"
std::shared_ptr<CC> CC::CreateCC(int x)
{
return std::make_shared<CC>(CC::Key(),x);
}
CC::CC(const Key& rk, int xy)
{
y = xy;
@RklAlx
RklAlx / SocketClient.cpp
Created September 27, 2013 11:55
Socket Client
CMyClient::CMyClient(const char* pIP, unsigned int port)
{
m_Socket = INVALID_SOCKET;
_snprintf_s(this->m_IP,IP_SIZE,_TRUNCATE,pIP);
this->InitSockAddrIn();
}
void CMyClient::InitSockAddrIn()
{
@RklAlx
RklAlx / ShowInfo.cpp
Created September 27, 2013 11:49
Hostname
/*NOTA:
1) tem que se garantir que o windows.h é compilado sempre após o ws2tcpip.h,
caso contrário começa a indicar que há referencias duplicadas.
2) Quando se trabalha com sockets tem sempre que se inicializar a WinSockDLL -> tag WSAStartup */
#include <Ws2tcpip.h>
void ShowInfo(struct addrinfo *res)
@RklAlx
RklAlx / Sender.cpp
Created September 27, 2013 11:47
Sender
/*NOTA:
1) tem que se garantir que o windows.h é compilado sempre após o ws2tcpip.h,
caso contrário começa a indicar que há referencias duplicadas.
2) Quando se trabalha com sockets tem sempre que se inicializar a WinSockDLL -> tag WSAStartup
Send function:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms740149%28v=vs.85%29.aspx
If no error occurs, send returns the total number of bytes sent, which can be *less*
than the number requested to be sent in the len parameter.
@RklAlx
RklAlx / Receiver.cpp
Created September 27, 2013 11:46
Receiver
/*NOTA:
1) tem que se garantir que o windows.h é compilado sempre após o ws2tcpip.h,
caso contrário começa a indicar que há referencias duplicadas.
2) Quando se trabalha com sockets tem sempre que se inicializar a WinSockDLL -> tag WSAStartup */
#include <ws2tcpip.h>
#include <windows.h>
DWORD WINAPI CMyObjec::Receiver(LPVOID arg) // Receive From Client
@RklAlx
RklAlx / SocketServer.cpp
Created September 27, 2013 11:45
Socket Listener
#include "StdAfx.h"
#include "SocketServer.h"
#include <iostream>
using namespace std;
SocketServer::SocketServer()
: m_AccepterSocket(INVALID_SOCKET),
m_nPort(0),
m_dwAccepterThreadID(0)
@RklAlx
RklAlx / InitWinsockDll.cpp
Created September 27, 2013 11:04
WSAStart Initialize WinsockDll
#include <winsock2.h>
#include <iostream>
using namespace std;
bool InitWinsockDLL()
{
WSADATA wsaData;
//
///* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */