#ifndef GMP_MPZ_VAL_H #define GMP_MPZ_VAL_H #include #include #include #include #include #include class CGmpZEntryImpl; class CGmpZValAdaptBase; class CGmpZVal_ROAdapt; class CGmpZVal_WOAdapt; class CGmpZValAdapt; typedef shared_ptr GmpZEntryImpl; typedef shared_ptr GmpZValAdaptBase; typedef shared_ptr GmpZVal_ROAdapt; typedef shared_ptr GmpZVal_WOAdapt; typedef shared_ptr GmpZValAdapt; // Async-IO completion support class CGmpZAioCtxt : public CAsyncIOCompletion { private: GmpZValAdaptBase adapt_; mpz_t *mpz_; unsigned nelms_; unsigned size_; int ord_; bool rNw_; uint8_t *buf_; public: CGmpZAioCtxt(GmpZValAdaptBase adapt, AsyncIO aio, mpz_t *mpz, unsigned nelms, ByteOrder byteOrder, bool doRead = true); virtual void complete(); virtual uint8_t * getBufp() const { return buf_; } virtual ~CGmpZAioCtxt (); }; // Entry subclass class CGmpZEntryImpl : public CIntEntryImpl { public: CGmpZEntryImpl(Key &k, YamlState &n); CGmpZEntryImpl(const CGmpZEntryImpl &orig, Key &k); static const char *_getClassName() { return "GmpZField"; } virtual const char * getClassName() const { return _getClassName(); } virtual uint64_t dumpMyConfigToYaml(Path p, YAML::Node &n) const; virtual uint64_t loadMyConfigFromYaml(Path p, YAML::Node &n) const; virtual CGmpZEntryImpl *clone(Key &k) { return new CGmpZEntryImpl( *this, k ); } virtual EntryAdapt createAdapter(IEntryAdapterKey &k, Path p, const std::type_info &interfaceType) const; }; // Adapter class class CGmpZValAdaptBase : public IEntryAdapt { protected: shared_ptr e_; public: CGmpZValAdaptBase(Key &k, Path p, shared_ptr e); typedef IScalVal_Base::Encoding Encoding; // override getNelms and getSizeBits to infer GMP from an array of bytes virtual unsigned getNelms(); virtual uint64_t getSizeBits() const; virtual bool isSigned() const { return e_->isSigned(); } virtual int getLsBit() const { return e_->getLsBit(); } virtual unsigned getWordSwap() const { return e_->getWordSwap(); } virtual Encoding getEncoding() const { return e_->getEncoding(); } virtual Mode getMode() const { return e_->getMode(); } virtual Enum getEnum() const { return e_->getEnum(); } protected: virtual bool isEncodedMPZ()const { return e_->getSizeBits() == 8; } }; class CGmpZVal_ROAdapt : public virtual CGmpZValAdaptBase, public virtual IGmpZVal_RO { public: CGmpZVal_ROAdapt(Key &k, Path p, shared_ptr e); virtual unsigned getVal(AsyncIO aio, mpz_t *dst, unsigned nelms, SlicedPathIterator *it); virtual unsigned getVal(mpz_t *p, unsigned nelms = 1, IndexRange *range = 0); virtual unsigned getVal(AsyncIO aio, mpz_t *p, unsigned nelms = 1, IndexRange *range = 0); }; class CGmpZVal_WOAdapt : public virtual CGmpZValAdaptBase, public virtual IGmpZVal_WO { public: CGmpZVal_WOAdapt(Key &k, Path p, shared_ptr e); virtual unsigned setVal(mpz_t *p, unsigned nelms = 1, IndexRange *range = 0); }; class CGmpZValAdapt : public virtual CGmpZVal_ROAdapt, public virtual CGmpZVal_WOAdapt, public virtual IGmpZVal { public: CGmpZValAdapt(Key &k, Path p, shared_ptr e); }; namespace YAML { template <> struct convert { static bool decode(const Node &node, MPZ &rhs) { bool rval; if ( ! node.IsScalar() ) return false; std::string str = node.Scalar(); rval = ( 0 == mpz_set_str( rhs.m_, str.c_str(), 0 ) ); return rval; } static Node encode(const MPZ &rhs) { std::vector buf; buf.reserve( mpz_sizeinbase( rhs.m_, 16 ) + 4 ); buf[0] = '0'; buf[1] = 'x'; mpz_get_str( &buf[2], 16, rhs.m_ ); return Node( &buf[0] ); } }; }; #endif