使用Boost :: Serialization使用std :: mt19937_64成员序列化类

时间:2017-08-24 22:38:04

标签: c++ serialization random boost stl

给定一个类定义

class Example {
    std::mt19937_64 rng_;
;}

如何使用Boost序列化它?我试过了

class Example { 
    std::mt19937_64 rng_;
    template <class Archive> void save(Archive& ar, const unsigned int version) const {
        ar << rng_;
    }

    template <class Archive> void load(Archive& ar, const unsigned int version) {
        ar >> rng_;
    }
    BOOST_SERIALIZATION_SPLIT_MEMBER();
;}

但它不起作用 - rng_成员的状态不会从存档中读回。

1 个答案:

答案 0 :(得分:2)

我的评论:

  

好。这显然没有实施。无论如何,是什么给你的印象呢?

当然,这只是提出问题。你走了:

<?php

$ip=$_SERVER["REMOTE_ADDR"];

function flush_buffers()
{ 
    ini_set('output_buffering','on');
    ini_set('zlib.output_compression', 0);
    ini_set('implicit_flush',1);
    ob_implicit_flush();

    echo ("<html><head><head><body>");
    for($i=0;$i<20;$i++) {
        echo $i;
        echo str_repeat(" ", 500);
        ob_flush();
        flush();
        sleep(1);
    }
}

function dnsbllookup($ip)
{
    $dnsbl_lookup=array(
    "access.redhawk.org",
    "b.barracudacentral.org",
    "bl.csma.biz",
    "bl.emailbasura.org",
    "bl.spamcannibal.org",
    "bl.spamcop.net",
    "bl.technovision.dk",
    "blackholes.five-ten-sg.com",
    "blackholes.wirehub.net",
    "blacklist.sci.kun.nl",
    "block.dnsbl.sorbs.net",
    "blocked.hilli.dk",
    "bogons.cymru.com",
    "cart00ney.surriel.com",
    "cbl.abuseat.org",
    "dev.null.dk",
    "dialup.blacklist.jippg.org",
    "dialups.mail-abuse.org",
    "dialups.visi.com",
    "dnsbl.ahbl.org",
    "dnsbl.antispam.or.id",
    "dnsbl.cyberlogic.net",
    "dnsbl.kempt.net",
    "dnsbl.njabl.org",
    "dnsbl.sorbs.net",
    "dnsbl-1.uceprotect.net",
    "dnsbl-2.uceprotect.net",
    "dnsbl-3.uceprotect.net",
    "duinv.aupads.org",
    "dul.dnsbl.sorbs.net",
    "dul.ru",
    "escalations.dnsbl.sorbs.net",
    "hil.habeas.com",
    "http.dnsbl.sorbs.net",
    "intruders.docs.uu.se",
    "ips.backscatterer.org",
    "korea.services.net",
    "mail-abuse.blacklist.jippg.org",
    "misc.dnsbl.sorbs.net",
    "msgid.bl.gweep.ca",
    "new.dnsbl.sorbs.net",
    "no-more-funn.moensted.dk",
    "old.dnsbl.sorbs.net",
    "pbl.spamhaus.org",
    "proxy.bl.gweep.ca",
    "psbl.surriel.com",
    "pss.spambusters.org.ar",
    "rbl.schulte.org",
    "rbl.snark.net",
    "recent.dnsbl.sorbs.net",
    "relays.bl.gweep.ca",
    "relays.bl.kundenserver.de",
    "relays.mail-abuse.org",
    "relays.nether.net",
    "rsbl.aupads.org",
    "sbl.spamhaus.org",
    "smtp.dnsbl.sorbs.net",
    "socks.dnsbl.sorbs.net",
    "spam.dnsbl.sorbs.net",
    "spam.olsentech.net",
    "spamguard.leadmon.net",
    "spamsources.fabel.dk",
    "tor.ahbl.org",
    "web.dnsbl.sorbs.net",
    "whois.rfc-ignorant.org",
    "xbl.spamhaus.org",
    "zen.spamhaus.org",
    "zombie.dnsbl.sorbs.net",
    "bl.tiopan.com",
    "dnsbl.abuse.ch",
    "tor.dnsbl.sectoor.de",
    "ubl.unsubscore.com",
    "cblless.anti-spam.org.cn",
    "dnsbl.tornevall.org",
    "dnsbl.anticaptcha.net",
    "dnsbl.dronebl.org"
    ); // Add your preferred list of DNSBL's
    $AllCount = count($dnsbl_lookup);
    $BadCount = 0;
    if($ip)
    {
        $reverse_ip = implode(".", array_reverse(explode(".", $ip)));
        foreach($dnsbl_lookup as $host)
        {
            if(checkdnsrr($reverse_ip.".".$host.".", "A"))
            {
//                echo "<span color='#339933'>Listed on ".$reverse_ip.'.'.$host."!</span><br/>";
                flush_buffers();
                $BadCount++;
            }
            else
            {
//                echo "Not listed on ".$reverse_ip.'.'.$host."!<br/>";
                flush_buffers();
            }
        }
    }
    else
    {
//        echo "Empty ip!<br/>";
        flush_buffers();
    }

//    echo "This ip has ".$BadCount." bad listings of ".$AllCount."!<br/>";

    flush_buffers();

    if($BadCount==0)
    {
        include("index.php");
    }
    else
    {
        include("default.htm");
    }

}

if(preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/",@$ip) == true)
{
    dnsbllookup($ip);
}?>

参见 Live On Coliru

打印:

namespace boost { namespace serialization {
#define MT_TPARAMS typename UIntType, size_t w, size_t n, size_t m, size_t r, UIntType a, size_t u, UIntType d, size_t s, UIntType b, size_t t, UIntType c, size_t l, UIntType f
#define MT_TARGLIST UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f

    template<typename Ar, MT_TPARAMS>
    void load(Ar& ar, std::mersenne_twister_engine<MT_TARGLIST>& mt, unsigned) {
        std::string text;
        ar & text;
        std::istringstream iss(text);

        if (!(iss >> mt))
            throw std::invalid_argument("mersenne_twister_engine state");
    }

    template<typename Ar, MT_TPARAMS>
    void save(Ar& ar, std::mersenne_twister_engine<MT_TARGLIST> const& mt, unsigned) {
        std::ostringstream oss;
        if (!(oss << mt))
            throw std::invalid_argument("mersenne_twister_engine state");
        std::string text = oss.str();
        ar & text;
    }

    template<typename Ar, MT_TPARAMS>
    void serialize(Ar& ar, std::mersenne_twister_engine<MT_TARGLIST>& mt, unsigned version) {
        if (typename Ar::is_saving())
            save(ar, mt, version);
        else
            load(ar, mt, version);
    }

#undef MT_TPARAMS
#undef MT_TARGLIST

} }