Advertisement
rockdrilla

lazy<T>

Jun 9th, 2015
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. ////////////////////////////////////////////
  2. // lazy<T> definition
  3. ////////////////////////////////////////////
  4. template<typename T>
  5. class lazy final {
  6.     lazy() {}
  7.  
  8. public:
  9.     class interface {
  10.     public:
  11.         virtual const T & get () const = 0;
  12.         virtual operator const T & () const = 0;
  13.     };
  14.  
  15.     class proxy: public virtual interface {
  16.         mutable T _value;
  17.         mutable bool _state;
  18.         std::function<void()> _init;
  19.  
  20.         template<typename ... Types>
  21.         static void Tctor(T* dst, Types ... args) {
  22.             new (dst) T(args ...);
  23.         }
  24.  
  25.     public:
  26.         template<typename ... Types>
  27.         proxy(Types ... args) {
  28.             _init = std::bind(proxy::Tctor<Types ...>, &_value, args ...);
  29.             _state = false;
  30.         }
  31.  
  32.         const T & get () const override {
  33.             if (!_state) {
  34.                 _init();
  35.                 _state = true;
  36.             }
  37.             return _value;
  38.         }
  39.  
  40.         inline operator const T & () const override {
  41.             return get();
  42.         }
  43.     };
  44.  
  45.     class func: public virtual interface {
  46.         mutable T _value;
  47.         mutable bool _state;
  48.         std::function<T()> _init;
  49.  
  50.     public:
  51.         template<typename X>
  52.         func(X init) {
  53.             _init = static_cast<std::function<T()>>(init);
  54.             _state = false;
  55.         }
  56.  
  57.         const T & get () const override {
  58.             if (!_state) {
  59.                 _value = _init();
  60.                 _state = true;
  61.             }
  62.             return _value;
  63.         }
  64.  
  65.         inline operator const T & () const override {
  66.             return get();
  67.         }
  68.     };
  69. };
  70.  
  71. ////////////////////////////////////////////
  72. // lazy<T> usage example
  73. ////////////////////////////////////////////
  74.  
  75. class oid {
  76. public:
  77. //<include>/net-snmp/library/oid.h
  78. #ifndef EIGHTBIT_SUBIDS
  79. typedef u_long atom;
  80. #else
  81. typedef uint8_t atom;
  82. #endif
  83.  
  84.     oid();
  85.     oid(const char * oid_string);
  86.     ~oid();
  87.  
  88.     size_t length() const;
  89.     const atom* data() const;
  90.  
  91.     const oid& operator =(const oid& other);
  92.  
  93.     template<typename T = atom>
  94.     operator T*() const { return reinterpret_cast<T*>(static_cast<atom*>(_ptr)); }
  95.  
  96.     static constexpr int max_length = 128;
  97.  
  98. private:
  99.     boost::shared_array<atom> _ptr;
  100.     size_t _len;
  101. };
  102.  
  103. int main(int argc, char* argv[]) {
  104.     lazy<oid>::proxy system_object_id("1.3.6.1.2.1.1.2.0");
  105.     lazy<oid>::func system_uptime( [] () -> oid { return oid("1.3.6.1.2.1.1.3.0"); } );
  106.  
  107.     oid x1, x2;
  108.     x1 = system_object_id;
  109.     x2 = system_uptime;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement