22 ก.ย. 2557

std::vector splice ด้วย initialize_list กับ parameter pack

// testfunc.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

enum class Operation {
 DELETE, INSERT, EQUAL
};


class Diff
{
public:
 Diff()
 {

 }

 Diff(Operation oper, std::string & str)
 {
  operation = oper;
  text = str;
 }

 inline bool operator==(Diff & rhs)
 {
  return rhs.operation == operation && rhs.text == text;
 }

 Operation operation;
 std::string text;
};

template<class T>
std::vector<T> * SpliceIL(std::vector<T> * input, int start, int count, std::initializer_list<T> objects = {})
{
 std::vector<T> * delRange = new std::vector<T>(input->begin() + start, input->begin() + start + count);
 while (count > 0)
 {
  input->erase(input->begin() + start);
  count--;
 }
 for (auto i : objects)
 {
  input->insert(input->begin() + start++, i);
 }

 return delRange;
}

template<class T,typename... Args>
std::vector<T> * SplicePP(std::vector<T> * input, int start, int count, Args...objects)
{
 std::vector<T> * delRange = new std::vector<T>(input->begin() + start, input->begin() + start + count);
 std::vector<T> newVec = { objects... };
 while (count > 0)
 {
  input->erase(input->begin() + start);
  count--;
 }
 for (auto i : newVec)
 {
  input->insert(input->begin() + start++, i);
 }

 return delRange;
}

int _tmain(int argc, _TCHAR* argv[])
{
 std::vector<Diff*> * r1 = new std::vector<Diff*>();
 r1->push_back(new Diff(Operation::EQUAL,std::string("1")));
 r1->push_back(new Diff(Operation::EQUAL, std::string("2")));
 r1->push_back(new Diff(Operation::EQUAL, std::string("3")));
 r1->push_back(new Diff(Operation::EQUAL, std::string("4")));
 r1->push_back(new Diff(Operation::EQUAL, std::string("5")));
 r1->push_back(new Diff(Operation::EQUAL, std::string("6")));
 r1->push_back(new Diff(Operation::EQUAL, std::string("7")));
 r1->push_back(new Diff(Operation::EQUAL, std::string("8")));

 std::vector<Diff*> * r2 = SpliceIL<Diff*>(r1, 1, 2, 
        { new Diff(Operation::EQUAL, std::string("9")), 
         new Diff(Operation::EQUAL, std::string("10")) 
        } ); //!--pass
 std::vector<Diff*> * r4 = SpliceIL<Diff*>(r1, 1, 2, {}); //!--pass
 std::vector<Diff*> * r5 = SpliceIL<Diff*>(r1, 1, 2); //!--pass

 std::vector<Diff*> * r3 = SplicePP<Diff*, Diff*>(r1, 1, 2, 
        new Diff(Operation::EQUAL, std::string("12")), 
        new Diff(Operation::EQUAL, std::string("13")));
 std::vector<Diff*> * r6 = SplicePP<Diff*, Diff*>(r1, 1, 2, nullptr); 
 std::vector<Diff*> * r6 = SplicePP<Diff*, Diff*>(r1, 1, 2); //!--error
 return 0;
}

28 มิ.ย. 2557

lightweight proxy cache with nginx + ngx_cache_purge [centos 6.5]

yum install zlib-devel
yum install pcre-devel
wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz
tar -zxvf  ngx_cache_purge-2.1.tar.gz
wget http://nginx.org/download/nginx-1.6.0.tar.gz
tar -zxvf  nginx-1.6.0.tar.gz
cd  nginx-1.6.0
 ./configure --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/nginx/tmp --add-module=../ngx_cache_purge-2.1
 make && make install
 vi /etc/nginx/nginx.conf
#example
proxy_cache_path /var/nginx/cache levels=1:2 keys_zone=static:10m max_size=1000m inactive=720m;
proxy_temp_path /var/nginx/cache/tmp;
proxy_cache_key "$scheme://$host$request_uri";
server {
        listen   80; 
        server_name caznova.org *.caznova.org; 
index index.html index.htm index.php;
access_log  off; 
proxy_cache static;
proxy_set_header X-Real-IP  $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_ignore_headers Set-Cookie; 
proxy_ignore_headers Cache-Control; 
add_header X-Cache-Status $upstream_cache_status;
location ~* ^/(jQueryAssets|images|css|js|jscripts|flash)/(.*)$ {
            proxy_cache_min_uses 1;
            proxy_cache_valid 200 301 302 120m;
            proxy_cache_valid 404 1m;
            expires max;
proxy_pass http://backend:80;
}

location ~ /purge(/.*) {
proxy_cache_purge static "$scheme://$host$1";
}
        location / {
proxy_no_cache 1;
proxy_cache_bypass 1;
proxy_pass http://backend:80;
        }
}
#init.d autostart
http://wiki.nginx.org/InitScripts 
#example clear cache
origin path : http://caznova.org/css/text.css
url =  http://caznova.org/purge/css/text.css
 

26 มี.ค. 2557

Build boost 1.55 (shared) VS2013 fix

1.change BOOST_WORKAROUND(_MSC_VER, <= 1700) to BOOST_WORKAROUND(_MSC_VER, <= 1800) in named_slot_map.cpp and named_slot_map.hpp to successfully compile under msvc-12.0

2.
while compiling with VS 2013 RC, the following error occurs:

boost/archive/iterators/transform_width.hpp(151) : error C2039: 'min' : is not a member of 'std' boost/archive/iterators/transform_width.hpp(134) : while compiling class template member function 'void

boost::archive::iterators::transform_width<boost::archive::iterators::binary_from_base64<boost::archive::iterators::remove_whitespace<boost::archive::iterators::istr

eam_iterator<CharType?>>,CharType?>,8,6,CharType?>::fill(void)'

Fix: Add #include <algorithm> to the four files "libs\serialization\src\basic_text_*primitive.cpp"


b2.exe -j8 toolset=msvc-13.0 variant=debug,release link=shared threading=multi runtime-link=shared

http://www.swganh.com/anh_community/viewtopic.php?f=71&t=1412

https://svn.boost.org/trac/boost/ticket/1499