用户操作
[即时聊天] [发私信] [加为好友]
whinah
最近评论
whinah:客气、客气,这个RPC之后我也再没继续深入。打造一个稳定而强大的基础设施需要太大的精力。目前我们使用ICE,有兴趣可以去关心一下。前不久我在boost sandbox 中看到了一个rpc,没细看,估计在参数传递方面和我的设计思想差不多,不过它底层用的是boost.asio。
zm0011:在开发网络通讯程序中很有用,正想自己写一个,竟然百度出来了。顺便看了一下老兄的其它文章,数量不多但水平比较高,而且涉足的领域很多哦,从WIN到LINUX、从C++壳到数据库都有。希望能看到你奉献更多的好东东,谢谢!
whinah:这里只考虑了简单性,atl 这种手工打造虚表的方法的确可以节省存储空间,但是繁琐了一些。
nico:有没有考虑过 ATL/WTL 里用到模拟虚函数?

template <class T>
class foo
{
int& get_n()
{
T* pT = (T*)this;
return pT->n;
}
}

class bar : foo<……
whinah:如果真要实际使用这种技术,字符串文字量也必须经过地址转换,如:
convert_address("string literal")
文章分类
    收藏
      相册
      我的相册
      存档
      软件项目交易
      订阅我的博客
      XML聚合  FeedSky
      订阅到鲜果
      订阅到Google
      订阅到抓虾
      订阅到BlogLines
      订阅到Yahoo
      订阅到GouGou
      订阅到飞鸽
      订阅到Rojo
      订阅到newsgator
      订阅到netvibes

      原创 一个很强大的Comparator生成器收藏

      新一篇: 通用的 LoserTree | 旧一篇: Comparator 将 M×N 转化成 M+N

       

      /**
        @brief 生成一个比较器(Comparator),兼键提取(KeyExtractor)类
      
        使用这个宏生成的比较器可以作用在不同的对象上,只要这些对象有相同名称的成员,
        并且可以作用在类型为成员类型的对象上。
      
        - 假设:
      
          - 有 n 个类 class[1], class[2], ... class[n],都有类型为 MemberType ,名称为 MemberName 的数据成员
          - 那么以下类型的对象可以使用该类相互比较,并且可以从这些对象中提取出 MemberType 类型的键:
              class[1] ... class[n], MemberType, 以及所有这些类型的任意级别的指针
      
        @param ComparatorName 比较器类的名字
        @param MemberType     要比较的对象的成员类型
        @param MemberName     要比较的对象的成员名字,也可以是一个成员函数调用,
                              前面必须加 '.' 或者 '->', 加 '->' 只是为用于 smart_ptr/iterator/proxy 等重载 '->' 的对象
                              当用于裸指针时,仍使用 '.',这意味着裸指针和 smart_ptr/iterator/proxy
                              不能使用同一个生成的 Comparator,虽然裸指针的语法和它们都相同
        @param ComparePred    比较准则,这个比较准则将被应用到 XXXX MemberName
      
        @note
          - 这个类不是从 ComparePred 继承,为的是可以允许 ComparePred 是个函数,
            但这样(不继承)阻止了编译器进行空类优化
          - 不在内部使用 const MemberType&, 而是直接使用 MemberType, 
            是为了允许 MemberName 是一个函数时,返回一个即时计算出来的 Key;
            - 当为了效率需要使用引用时,将 const MemberType& 作为 MemberType 传进来
          - 当 MemberType 是个指针时,将 Type* 作为 MemberType ,而非 const Type*,即使 MemberType 真的是 const Type*
          - 注意 C++ 参数推导机制:
          @code
            template<T> void f(const T& x) { } // f1
            template<T> void f(const T* x) { } // f2
            template<T> void g(const T& x) { } // g1
            template<T> void g(const T* x) { } // g2
            template<T> void g(      T& x) { } // g3
            template<T> void g(      T* x) { } // g4
            void foo()
            {
               int a;
               const int b;
               f(&a); // call f1, T was deduced as int*, and then convert to 'const int*&', so match f1, not f2
               f(&b); // call f2, T was deduced as int
               g(&a); // call g4, T was deduced as int
               g(&b); // call g2, T was deduced as int
            }
          @endcode
            在上述代码已经表现得比较明白了,这就是要生成四个不同 deref 版本的原因
          - 为了配合上述机制,传入的 MemberType 不要有任何 const 修饰符
       */
      #define SAME_NAME_MEMBER_COMPARATOR_EX(ComparatorName, MemberType, MemberName, ComparePred) \
      class ComparatorName                                        \
      {                                                           \
          ComparePred m_comp;                                     \
      public:                                                     \
          typedef bool        result_type;                        \
          typedef MemberType  key_type;                           \
          typedef boost::integral_constant<bool,                  \
              febird::HasTriCompare<ComparePred>::value           \
          > has_tri_compare;                                      \
                                                                  \
          ComparatorName()  {}                                    \
          ComparatorName(const ComparePred& rhs)                  \
              : m_comp(rhs) {}                                    \
                                                                  \
          template<class T>const T&deref(T*x)const{return*x;}     \
          template<class T>const T&deref(T&x)const{return x;}     \
          template<class T>const T&deref(const T*x)const{return*x;}\
          template<class T>const T&deref(const T&x)const{return x;}\
                                                                  \
          const MemberType operator()(const MemberType x)const{return x;} \
          template<class T>const MemberType operator()(const T&x)const{return deref(x)MemberName;}\
                                                                  \
          template<class Tx, class Ty>                            \
          bool operator()(const Tx&x, const Ty&y) const           \
          {                                                       \
              return m_comp((*this)(x),(*this)(y));               \
          }                                                       \
          template<class Tx, class Ty>                            \
          int compare(const Tx&x, const Ty&y) const               \
          {                                                       \
              return m_comp.compare((*this)(x),(*this)(y));       \
          }                                                       \
      };
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      #endif
      
      // #define SAME_NAME_MEMBER_COMPARATOR_EX(ComparatorName, MemberType, MemberName, ComparePred)  \
      // SAME_NAME_MEMBER_COMPARATOR_EX_NO_TRAITS(ComparatorName, MemberType, MemberName, ComparePred)\
      // BOOST_TT_AUX_BOOL_TRAIT_SPEC1(HasTriCompare, ComparatorName, HasTriCompare<ComparePred>::value)
      
      //! note@ if MemberType must not be a reference, neither const nor non-const
      #define SAME_NAME_MEMBER_COMPARATOR(ComparatorName, MemberType, MemberName) \
          SAME_NAME_MEMBER_COMPARATOR_EX(ComparatorName, MemberType, MemberName, std::less<MemberType>)
      

      发表于 @ 2008年04月22日 15:02:00|评论(loading...)|编辑|收藏

      新一篇: 通用的 LoserTree | 旧一篇: Comparator 将 M×N 转化成 M+N

      评论:没有评论。

      发表评论  


      当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
      Csdn Blog version 3.1a
      Copyright © whinah