#ifndef CPSW_ENUM_IMPL_BASE_H #define CPSW_ENUM_IMPL_BASE_H #include #include #include using std::vector; // for now we just use a vector (assuming there are // no huge maps). class CEnumImpl : public IMutableEnum, public vector { private: unsigned nelms_; TransformFunc xfrm_; protected: virtual uint64_t transform(uint64_t in) { return xfrm_ ? xfrm_(in) : in; } CEnumImpl(TransformFunc xfrm); public: virtual ~CEnumImpl(); // implement the abstract iterator using // vector<>::iterator class CIteratorImpl : public IIterator { private: vector::const_iterator iter_; // allocate this iterator inside the IEnum::iterator object's 'rawmem' // area. IEnum::iterator is commonly allocated on the stack and using // placement new avoids using the heap for iterators... void *operator new(std::size_t sz, void *p, std::size_t rawsz) { if ( sz > rawsz ) throw InternalError("CEnumImpl::CIteratorImpl raw memory area too small"); return p; } friend class IEnum::iterator; // allow iterator to use this allocator public: CIteratorImpl( vector::const_iterator iter ) : iter_(iter) { } CIteratorImpl() { } virtual IIterator & operator++() { ++iter_; return *this; } virtual Item operator*() { return *iter_; } virtual bool operator==(IIterator *other) { return iter_ == static_cast(other)->iter_; } virtual ~CIteratorImpl() { } }; virtual IEnum::iterator begin() const; virtual IEnum::iterator end() const; virtual unsigned getNelms() const { return nelms_; } virtual Item map(uint64_t) ; virtual Item map(const char*) ; virtual void add(const char*, uint64_t); static MutableEnum create(TransformFunc f); }; #endif