/*
* =====================================================================================
*
* Filename:
*
* Description: Common function
*
* Version: 1.0
* Created: 2015年11月25日 16时11分13秒
* Revision: none
* Compiler: g++
*
* Author: betachen
* Company:
*
* =====================================================================================
*/ #ifndef __X__H__20151026__
#define __X__H__20151026__ #include <iostream>
#include <string>
#include <cstdarg>
#include <cstdio>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <assert.h> // MutexLock
class MutexLock
{
public:
MutexLock(): holder_(0)
{
pthread_mutex_init(&mutex_, NULL);
}
~MutexLock()
{
assert(holder_ == 0);
pthread_mutex_destroy(&mutex_);
}
bool isLockedBySelf() {return holder_ == pthread_self();}
void lock()
{
pthread_mutex_lock(&mutex_);
holder_ = pthread_self();
}
void unlock()
{
holder_ = 0;
pthread_mutex_unlock(&mutex_);
}
pthread_mutex_t* getPthreadMutex(){return &mutex_;} private:
MutexLock(const MutexLock& rx);
MutexLock& operator=(const MutexLock&); private:
pthread_mutex_t mutex_;
pthread_t holder_;
}; // MutexLockGuard
class MutexLockGuard
{
public:
explicit MutexLockGuard(MutexLock& lock):mutex_lock_(lock)
{
mutex_lock_.lock();
}
~MutexLockGuard()
{
mutex_lock_.unlock();
} private:
MutexLockGuard();
MutexLockGuard(const MutexLockGuard& rx);
MutexLockGuard& operator=(const MutexLockGuard& rx); private:
MutexLock& mutex_lock_;
}; //Condtion for threading
class Condition
{
public:
explicit Condition(MutexLock& mutex):mutex_(mutex)
{
pthread_cond_init(&cond_, NULL);
}
~Condition(){pthread_cond_destroy(&cond_);} void wait()
{
pthread_cond_wait(&cond_, mutex_.getPthreadMutex());
} void notify()
{
pthread_cond_signal(&cond_);
}
void notifyAll()
{
pthread_cond_broadcast(&cond_);
} private:
MutexLock& mutex_;
pthread_cond_t cond_;
}; //CountDownLatch
class CountDownLatch
{
public:
explicit CountDownLatch(int count):count_(count), cond_(mutex_){}
void wait()
{
MutexLockGuard lock(mutex_);
while (count_ > 0)
{
cond_.wait();
}
}
void countDown()
{
MutexLockGuard lock(mutex_);
count_--;
if (count_ == 0)
{
cond_.notifyAll();
}
}
private:
int count_;
mutable MutexLock mutex_;
Condition cond_; }; // FileHandle
class FileHandle
{
public:
FileHandle(const char* s, const char* sMode):file_name_(s)
{
file_ = fopen(s, sMode);
if (file_ == NULL)
{
std::cerr<<s<<" open failed"<<std::endl;
}
}
FileHandle(const std::string& s, const char* sMode):file_name_(s)
{
std::cout<<file_name_<<" opened"<<std::endl;
file_ = fopen(s.c_str(), sMode);
if (file_ == NULL)
{
std::cerr<<s<<" open failed"<<std::endl;
}
}
~FileHandle()
{
if (file_)
fclose(file_);
}
FILE* get(){return file_;}
std::string& get_name(){return file_name_;}; private:
FileHandle();
FileHandle(const FileHandle& rx);
FileHandle& operator = (const FileHandle& rx); protected:
FILE* file_;
std::string file_name_;
}; // has lock for threading
class Loger:public FileHandle
{
public:
Loger(const char* s):FileHandle(s, "a+"){}
Loger(const std::string& s):FileHandle(s, "a+"){}
void _(const char* sFile, const int iLine, const char* sFormat, ...)
{
va_list sList;
va_start(sList, sFormat);
gettimeofday(&tt, NULL);
ttt = localtime(&tt.tv_sec);
//time pid file line
fprintf(file_, "%04d%02d%02d-%02d:%02d:%02d.%03ld|%6d|%15s|% 5d|-> ",
ttt->tm_year+1900,ttt->tm_mon,ttt->tm_mday, ttt->tm_hour, ttt->tm_min, ttt->tm_sec,
tt.tv_usec/1000, getpid() ,sFile, iLine);
vfprintf(file_, sFormat, sList);
fprintf(file_, "\n");
va_end(sList);
}
void _(int lock_flag, const char* sFile, const int iLine, const char* sFormat, ...)
{
MutexLockGuard lock(lock_);
va_list sList;
va_start(sList, sFormat);
gettimeofday(&tt, NULL);
ttt = localtime(&tt.tv_sec);
fprintf(file_, "%04d%02d%02d-%02d:%02d:%02d.%03ld|%6d|%15s|% 5d|-> ",
ttt->tm_year+1900,ttt->tm_mon,ttt->tm_mday, ttt->tm_hour, ttt->tm_min, ttt->tm_sec,
tt.tv_usec/1000, getpid() ,sFile, iLine);
vfprintf(file_, sFormat, sList);
fprintf(file_, "\n");
va_end(sList);
} private:
Loger();
Loger(const Loger& rx);
Loger& operator = (const Loger& rx);
private:
struct tm* ttt;
struct timeval tt;
mutable MutexLock lock_;
}; #define NO_LOCK __FILE__,__LINE__
#define LOCK 1,__FILE__,__LINE__ //Loger log("webtest.log");
//#define _ log._ // To String
#include <algorithm>
template <typename T>
inline std::string toString(const T& value)
{
T i = value;
static const char* sDigits = "0123456789"; char buf[64] = "";
char* p = buf;
do{
int lsd = static_cast<int>(i % 10);
i /= 10;
*p++ = sDigits[lsd];
}while(i != 0); if (value < 0)
{
*p++ = '-';
}
*p = '\0';
std::reverse(buf, p); return buf;
} #endif

  

C++ RAII手法实例,不使用智能指针的更多相关文章

  1. enote笔记法使用范例(2)——指针(1)智能指针

    要知道什么是智能指针,首先了解什么称为 “资源分配即初始化” what RAII:RAII—Resource Acquisition Is Initialization,即“资源分配即初始化” 在&l ...

  2. 使用智能指针来管理对象 (基于RAII)

    ////一个简单的防止内存泄露的例子//void test() { //使用RAII的特性管理资源 //当智能指针unique_ptr被销毁时,它指向的对象也将被销毁 //这里test函数返回后 p将 ...

  3. 你应该掌握的C++ RAII手法:Scopegaurd

    C++作为一门Native Langueages,在C++98/03时代,资源管理是个大问题.而内存管理又是其中最大的问题.申请的堆内存需要手动分配和释放,为了确保内存正确释放,一般原则是" ...

  4. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

  5. [5] 智能指针boost::shared_ptr

    [1]boost::shared_ptr简介 boost::shared_ptr属于boost库,定义在namespace boost中,包含头文件#include<boost/shared_p ...

  6. c++ 智能指针(转)

    智能指针的使用 智能指针是在 <memory> 标头文件中的 std 命名空间中定义的. 它们对 RAII 或“获取资源即初始化”编程惯用法至关重要. 此习惯用法的主要目的是确保资源获取与 ...

  7. RAII手法封装互斥锁

    RAII手法是 Resource Acquisition is Initialization 的缩写,意为“资源获取即初始化”,在使用智能指针时也使用,下面是针对互斥量时的实现, #include & ...

  8. Boost智能指针-基础知识

    简单介绍 内存管理一直是 C++ 一个比較繁琐的问题,而智能指针却能够非常好的解决问题,在初始化时就已经预定了删除.排解了后顾之忧.1998年修订的第一版C++标准仅仅提供了一种智能指针:std::a ...

  9. C++ 中的智能指针-基础

    简介 在现代 C++ 编程中,标准库包含了智能指针(Smart pointers). 智能指针用来确保程序不会出现内存和资源的泄漏,并且是"异常安全"(exception-safe ...

随机推荐

  1. 英文版firefox显示中文字体丑的问题

    在Options里面选择Content,在Fonts&Colors区域的Default font中,选择Times New Roman 如下图1: 在旁边的Advanced中选择,Fonts ...

  2. 探索 Java 同步机制[Monitor Object 并发模式在 Java 同步机制中的实现]

    探索 Java 同步机制[Monitor Object 并发模式在 Java 同步机制中的实现] https://www.ibm.com/developerworks/cn/java/j-lo-syn ...

  3. KnockoutJS(4)-控制文本和外观绑定

    控制文本和外观绑定 依赖关系:除了Knockout核心库之外,无依赖关系. 类别 目的 Visible绑定 Visible绑定通过绑定一个值来确定DOM元素显示或隐藏 Text绑定 Text绑定主要是 ...

  4. DTCMS中文章增加tags标签和关键词时中文,替换为英文状态,

    DTCMS.Web\admin\article\article_edit.aspx 找到添加和修改的方法 model.tags = txtTags.Text.Trim()model.seo_keywo ...

  5. [分享]好用的Markdown编辑器

  6. Python Generators vs Iterators

    http://stackoverflow.com/questions/2776829/difference-between-python-generators-vs-iterators iterato ...

  7. delphi调用 java 的 WebService服务端.

    // InvRegistry.RegisterInvokeOptions(TypeInfo(ModelADServicePortType), ioLiteral); InvRegistry.Regis ...

  8. (转)Memcached

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据 库驱动网站的速度.Memcache ...

  9. python 练习题

    python是一个功能很强大的语言,他可以解决各种在数学问题,下面我分享一些练习题供大家参考: 有关正态分布的问题: # -*- coding: cp936 -*- import math a=0 u ...

  10. Dapper使用方法

    这里记下Dapper容易忘的使用方法: 返回的数据可以有相同名称的列,会分配到不同的类上,默认使用Id这个列作为分割列 connection.Open();  //手动打开的话会保持长连接,否则每次查 ...