12 Kasım 2016 Cumartesi

std::isalnum metodu

Giriş
Bu metod C'den geliyor. Global locale nesnesini kullanarak bir karakterin alfa nümerik olup olmadığını döner.

Dönüş tipi bool değil int'tir. Şöyle yaparız.
char val = ...;
if ( std::isalnum(val) == 0) {...}

10 Kasım 2016 Perşembe

std::atomic_bool Sınıfı

Giriş
Şu satırı dahil ederiz.
#include <atomic>
C++'ta atomic bool olarak kullanılabilecek iki sınıf var. Bunlar std::atomic_bool ve std::atomic_flag

Constructor
Şöyle yaparız.
std::atomic<bool> consumerLock (false);
exchange metodu - spinlock için
Spinlock yerine kullanılabilir. Nesne false ile başlar. While döngüsüne ilk gelen değeri true atar, eski değer olan false while'dan çıkmamıza sebep olur. İşimiz bitinceye kadar diğer thread'ler true değer atasa bile eski değer de halan true olduğu için döngü beklerler. Biz işimizi bitirip false değerini atayınca arkadan gelen bir başka thread döngüye girebilir.
std::atomic<bool> consumerLock (false);

{  // the critical section
  while (consumerLock.exchange(true)) { } // this is the spinlock

  // do something useful

  consumerLock = false; // unlock
}
load ve store - spinlock için
Exchange metodu yerine şöyle yaparız.
std::atomic<bool> setup_ready(false);

{
  // thread 1
  while (!setup_ready.load()) std::this_thread::yield();

  // do something
}

{
  // thread 2
  // perform some setup
  setup_ready.store(true);
}
load ve store - flag için
Thread pool'da kapatma bayrağı için kullanılabilir. Şöyle bir değişken tanımlarız.
std::atomic<bool> shutdown_flag;
Değişkeni şöyle ilkendiririz.
shutdown_flag(false),
veya
shutdown_flag.store(false);
veya
shutdown_flag = false;
Daha sonra değişkeni şöyle kontrol ederiz.
while (!shutdown_flag.load())
{...}
operator = - flag için
load ve store ile aynıdır. Thread pool'da kapatma bayrağı için kullanılabilir.
Örnek
Şöyle bir değişken tanımlarız.
std::atomic<bool> isRunning;
Değişkeni şöyle ilklendiririz.
isRunning = false;
operator bool - flag için
Örnek
Değişkeni şöyle kontrol etmek için şöyle yaparız.
while(isRunning) {
{...}
Veya şöyle yaparız.
if(isRunning) {...}

7 Kasım 2016 Pazartesi

fwide metodu

Giriş
Şu satırı dahil ederiz.
#include <wchar.h>
Wide oriented için şöyle yaparız.
fwide(stderr, 1);
Wide oriented için şöyle yaparız.
fwide(stderr, -1);
Byte oriented için şöyle yaparız.
fwide(stderr, 0)

2 Kasım 2016 Çarşamba

freopen metodu

Giriş
Şu satırı dahil ederiz.
#include <stdio.h>
stdin'i dosyaya yönlendirme
Şöyle yaparız.
FILE * fp = freopen("in.txt","r",stdin);
stdout'u dosyaya yönlendirme
Şöyle yaparız.
FILE * fp = freopen("out.txt","w",stdout);
Uygulamadan çıkmadan önce dosyayı kapatmak isteyebiliriz. Şöyle yaparız.
FILE * fp = freopen("out.txt","w",stdout);...
fclose (fp);

31 Ekim 2016 Pazartesi

std::thread::id Sınıfı

Constructor
Şöyle yaparız.
std::thread::id t_id;
operator != metodu
Şöyle yaparız
if (t_id != std::this_thread::get_id()) {
  ...
}


19 Ekim 2016 Çarşamba

std::mktime metodu

Giriş
C ile şu satırı dahil ederiz.
#include <time.h>
C++ ile şu satırı dahil ederiz.
#include <ctime>
Ne İşe Yarar
Broken time yani std::tm yapısından saniye yani std::time_t yapısına geçişi sağlar. Açıklaması şöyle
This function performs the reverse translation that localtime does.
İmzası şöyle
time_t mktime (struct tm *timeptr);
Yani localtime() metodu ile elde edilen yerel saati tekrar time_t 'ye çeviriyor. Eğer tm veriyapısını elle dolduracaksak yerel saati vermemiz lazım. Şöyle yaparız. Dikkat edilmesi gereken nokta year alanı 1900'den başlayacak şekilde doldurulur. Ayrıca month alanı da 0'an başlar. Alanların değeri için struct tm yazısına bakınız.
struct tm info;

// 16.06.2014
info.tm_mday = 16;
info.tm_mon = 5;
info.tm_year = 114; // Years since 1900

// 08:00:00 Uhr
info.tm_hour = 8;
info.tm_min = 0;
info.tm_sec = 0;

// Convert to Unix timestamp
info.tm_isdst = -1;
time_t timestamp = mktime(&info);
printf("Timestamp: %i", timestamp); //1402898400 verir
UTC Saat
Bu metod gmtime() ile elde edilen yerel saati tekrar time_t 'ye çeviremez.
std::mktime and timezone info başlıklı soruda UTC saatin mktime() kullanılarak time_t veriyapısına döndürülmesi ile ilgili bir kaç nokta var.

struct tm yapısını değiştirir
Açıklaması şöyle
If the conversion is successful, the time object is modified. All fields of time are updated to fit their proper ranges. time->tm_wday and time->tm_yday are recalculated using information available in other fields.
Şöyle yaparız.
struct tm t = ...
struct tcopy = t;  // store original  to compare later
std::time_t seconds1 = std::mktime( &t);
if(t.tm_year != tcopy.tm_year  // compare with original
  || t.tm_mon != tcopy.tm_mon  
  || t.tm_mday != tcopy.tm_mday  
  || t.tm_hour != tcopy.tm_hour  
  || t.tm_min != tcopy.tm_min
  || t.tm_sec != tcopy.tm_sec
) {
  std::cout << "invalid" << std::endl;
}


16 Ekim 2016 Pazar

Copy Elision

Giriş
Copy Elision notlarım aşağıda. Copy Elision'ın özel bir hali de Return Value Optimization

Copy Elision ve Yan Etkileri
Copy Elision yan etkilere sahiptir, mesela copy constructor, destructor gibi metodların çağrılmamasını sağlar.

Copy Elision Yapılmayabilir - C++17'den önce
Copy Elision bazı durumlarda derleyici tarafından dikkate alınmayabilir.
Copy elision is the only allowed form of optimization that can change the observable side-effects. Because some compilers do not perform copy elision in every situation where it is allowed (e.g., in debug mode), programs that rely on the side-effects of copy/move constructors and destructors are not portable.
Copy Elision - C++17
Açıklaması şöyle
Under the following circumstances, the compilers are required to omit the copy- and move- constructors of class objects even if copy/move constructor and the destructor have observable side-effects:
  • In initialization, if the initializer expression is a prvalue and the cv-unqualified version of the source type is the same class as the class of the destination, the initializer expression is used to initialize the destination object:
    T x = T(T(T())); // only one call to default constructor of T, to initialize x
Copy Constructor
Elimizde şöyle bir sınıf olsun
Server::Server(int port) {
    // initialize some class variables
    port_ = port;
    //...
}
Copy Elision sayesinde Copy Constructor'ın çağrılmasına gerek kalmadan şöyle yaparız.
int port = 3000;
Server server = Server(port);
Copy Elision ve Copy/Move Constructor - C++17'den önce
Burada dikkat edilmesi gereken nokta şu. Copy constructor veya Move Constructor çağrılmasa bile erişilebilir olmalıdır.
.... The last step is usually optimized out and the result of the conversion is constructed directly in the memory allocated for the target object, but the appropriate constructor (move or copy) is required to be accessible even though it's not used.