text:
#include "bootsector.hpp"

// for numeric_limits::max
#define NOMINMAX

#include <vector>
#include <string>
#include <utility>
#include <limits>
#include <sstream>
#include <type_traits>
#include <Poco/Process.h>
#include <Poco/Pipe.h>
#include <Poco/PipeStream.h>
#include <boost/format.hpp>
#include <boost/regex.hpp>
#include "read.hpp"
#include "show.hpp"
#include "tempfile.hpp"

using namespace std;
using namespace boost;
using namespace readutil;
using namespace showutil;

namespace
{
    const char dbwd_table[4] = {'b', 'w', 0, 'd'};

    template <typename T>
    uint32_t dbwd(ostream& os, uint32_t current_address, uint32_t limit_address,
        T value, typename std::enable_if<sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4>::type* = 0)
    {
        if (current_address + sizeof(T) < limit_address)
        {
            os << "    d" << dbwd_table[sizeof(T) - 1] << " " << value << '\n';
            return current_address + sizeof(T);
        }
        return current_address;
    }

    uint32_t db_str(ostream& os, uint32_t current_address, uint32_t limit_address,
        const uint8_t* value, size_t length)
    {
        if (current_address + length < limit_address)
        {
            os << "    db \"" << string(reinterpret_cast<const char*>(value), length) << "\"\n";
            return current_address + length;
        }
        return current_address;
    }
}

PbrFat::PbrFat(const std::array<uint8_t, 512>& data)
    : BootSector(data)
{
    const uint8_t* data_ = data.data();

    data_ = read(this->BS_jmpBoot, data_);
    data_ = read(this->BS_OEMName, data_);
    data_ = read16(this->BPB_BytsPerSec, data_);
    data_ = read8(this->BPB_SecPerClus, data_);
    data_ = read16(this->BPB_RsvdSecCnt, data_);
    data_ = read8(this->BPB_NumFATs, data_);
    data_ = read16(this->BPB_RootEntCnt, data_);
    data_ = read16(this->BPB_TotSec16, data_);
    data_ = read8(this->BPB_Media, data_);
    data_ = read16(this->BPB_FATSz16, data_);
    data_ = read16(this->BPB_SecPerTrk, data_);
    data_ = read16(this->BPB_NumHeads, data_);
    data_ = read32(this->BPB_HiddSec, data_);
    data_ = read32(this->BPB_TotSec32, data_);

    const uint8_t* data0 = data_;

    data_ = read8(this->fat12_16.BS_DrvNum, data_);
    data_ = read8(this->fat12_16.BS_Reserved1, data_);
    data_ = read8(this->fat12_16.BS_BootSig, data_);
    data_ = read32(this->fat12_16.BS_VolID, data_);
    data_ = read(this->fat12_16.BS_VolLab, data_);
    data_ = read(this->fat12_16.BS_FilSysType, data_);
    data_ = read(this->fat12_16.boot_program, data_);

    data_ = data0;

    data_ = read32(this->fat32.BPB_FATSz32, data_);
    data_ = read16(this->fat32.BPB_ExtFlags, data_);
    data_ = read(this->fat32.BPB_FSVer, data_);
    data_ = read32(this->fat32.BPB_RootClus, data_);
    data_ = read16(this->fat32.BPB_FSInfo, data_);
    data_ = read16(this->fat32.BPB_BkBootSec, data_);
    data_ = read(this->fat32.BPB_Reserved, data_);
    data_ = read8(this->fat32.BS_DrvNum, data_);
    data_ = read8(this->fat32.BS_Reserved1, data_);
    data_ = read8(this->fat32.BS_BootSig, data_);
    data_ = read32(this->fat32.BS_VolID, data_);
    data_ = read(this->fat32.BS_VolLab, data_);
    data_ = read(this->fat32.BS_FilSysType, data_);
    data_ = read(this->fat32.boot_program, data_);

    data_ = read(this->last_signature, data_);
}

void PbrFat::print_info(std::ostream& os) const
{
    const int fat_type = determine_fat_type();

    os << "Calculated fat type is FAT" << fat_type << endl;

    // Check file system string
    string fs_str_12_16(reinterpret_cast<const char *>(fat12_16.BS_FilSysType), 8);
    string fs_str_32(reinterpret_cast<const char *>(fat32.BS_FilSysType), 8);

    if ((fs_str_12_16 == "FAT12   "
        || fs_str_12_16 == "FAT16   "
        || fs_str_12_16 == "FAT     ") && fat_type == 32)
    {
        os << "But there is a string indicating file system is FAT12 or 16." << endl;
    }

    if (fs_str_32 == "FAT32   " && (fat_type == 12 || fat_type == 16))
    {
        os << "But there is a string indicating file system is FAT32." << endl;
    }

    os << fixlen("BS_jmpBoot:", 20) << bytes(BS_jmpBoot) << endl;
    os << fixlen("BS_OEMName:", 20)
        << string(reinterpret_cast<const char *>(BS_OEMName), 8) << endl;
    os << fixlen("BPB_BytsPerSec:", 20) << BPB_BytsPerSec << endl;
    os << fixlen("BPB_SecPerClus:", 20) << BPB_SecPerClus << endl;
    os << fixlen("BPB_RsvdSecCnt:", 20) << BPB_RsvdSecCnt << endl;
    os << fixlen("BPB_NumFATs:", 20) << BPB_NumFATs << endl;
    os << fixlen("BPB_RootEntCnt:", 20) << BPB_RootEntCnt << endl;
    os << fixlen("BPB_TotSec16:", 20) << BPB_TotSec16 << endl;
    os << fixlen("BPB_Media:", 20) << format("%02x") % (int)BPB_Media << endl;
    os << fixlen("BPB_FATSz16:", 20) << BPB_FATSz16 << endl;
    os << fixlen("BPB_SecPerTrk:", 20) << BPB_SecPerTrk << endl;
    os << fixlen("BPB_NumHeads:", 20) << BPB_NumHeads << endl;
    os << fixlen("BPB_HiddSec:", 20) << BPB_HiddSec << endl;
    os << fixlen("BPB_TotSec32:", 20) << BPB_TotSec32 << endl;

    if (fat_type == 12 || fat_type == 16)
    {
        os << fixlen("BS_DrvNum:", 20) << fat12_16.BS_DrvNum << endl;
        os << fixlen("BS_Reserved1:", 20) << fat12_16.BS_Reserved1 << endl;
        os << fixlen("BS_BootSig:", 20) << fat12_16.BS_BootSig << endl;
        os << fixlen("BS_VolID:", 20) << format("%08x") % fat12_16.BS_VolID << endl;
        os << fixlen("BS_VolLab:", 20)
            << string(reinterpret_cast<const char *>(fat12_16.BS_VolLab), 11) << endl;
        os << fixlen("BS_FilSysType:", 20) << fs_str_12_16 << endl;
    }
    else
    {
        os << fixlen("BPB_FATSz32:", 20) << fat32.BPB_FATSz32 << endl;
        os << fixlen("BPB_ExtFlag:", 20) << fat32.BPB_ExtFlags << endl;
        os << fixlen("BPB_FSVer:", 20)
            << format("%d.%d")
            % (int)fat32.BPB_FSVer[1] % (int)fat32.BPB_FSVer[0]
            << endl;
        os << fixlen("BPB_RootClus:", 20) << fat32.BPB_RootClus << endl;
        os << fixlen("BPB_FSInfo:", 20) << fat32.BPB_FSInfo << endl;
        os << fixlen("BPB_BkBootSec:", 20) << fat32.BPB_BkBootSec << endl;
        os << fixlen("BS_DrvNum:", 20) << fat32.BS_DrvNum << endl;
        os << fixlen("BS_Reserved1:", 20) << fat32.BS_Reserved1 << endl;
        os << fixlen("BS_BootSig:", 20) << fat32.BS_BootSig << endl;
        os << fixlen("BS_VolID:", 20) << format("%08x") % fat32.BS_VolID << endl;
        os << fixlen("BS_VolLab:", 20)
            << string(reinterpret_cast<const char *>(fat32.BS_VolLab), 11) << endl;
        os << fixlen("BS_FilSysType:", 20) << fs_str_32 << endl;
    }
}

void PbrFat::print_asm(std::ostream& os) const
{
    const int fat_type = determine_fat_type();

    unsigned int skipStart = 0, skipBytes = 0;
    if (BS_jmpBoot[0] == 0xeb)
    {
        // short jump
        skipBytes = BS_jmpBoot[1] - 1;
    }
    else if (BS_jmpBoot[0] == 0xe9)
    {
        // near jump
        skipBytes = BS_jmpBoot[1]
            | (static_cast<unsigned int>(BS_jmpBoot[2]) << 8);
    }

    // write bs to temporary file.
    // it will be removed at the end of this function automatically by TemporaryFile's destructor.
    TemporaryFile temp_file;
    ofstream temp_ofs(temp_file.path().native(), ios::out | ios::binary);
    const auto& bs_data = this->data();
    temp_ofs.write(reinterpret_cast<const char*>(bs_data.data()), bs_data.size());
    temp_ofs.flush();
    temp_ofs.close();

    Poco::Process::Args args;
    args.push_back("-b");
    args.push_back("16");
    args.push_back("-k");
    args.push_back(str(format("3,%d") % skipBytes)); // skip bpb section
    args.push_back("-k");
    args.push_back("510,2"); // skip last signature
    args.push_back(temp_file.path().string());

    Poco::Pipe stdout_pipe;
    auto proc = Poco::Process::launch("ndisasm", args, nullptr, &stdout_pipe, nullptr);
    Poco::PipeInputStream is(stdout_pipe);

    vector<pair<uint32_t, string>> instructions;
    int skip_index = -1; // index of "skipping" line.
    uint32_t zero_region_address = numeric_limits<uint32_t>::max(); // start address of the last all zero region.

    const regex disasm_pattern("^([0-9A-Fa-f]+)\\s+([0-9A-Fa-f]+)\\s+(.+)$");
    const regex skip_pattern("^[0-9A-Fa-f]+\\s+skipping.*$");

    string line;
    while (getline(is, line))
    {
        smatch m;
        if (regex_match(line, m, disasm_pattern))
        {
            // convert hex string to integer
            uint32_t address;
            stringstream ss;
            ss << hex << m[1];
            ss >> address;

            if (m[2] == "0000")
            {
                if (zero_region_address == numeric_limits<uint32_t>::max())
                {
                    zero_region_address = address;
                }
            }
            else
            {
                zero_region_address = numeric_limits<uint32_t>::max();
                instructions.push_back(make_pair(address, m[3]));
            }
        }
        else if (regex_match(line, m, skip_pattern))
        {
            if (skip_index == -1)
            {
                skip_index = instructions.size();
            }
        }
    }

    for (int i = 0; i < instructions.size(); ++i)
    {
        if (i == skip_index)
        {
            uint32_t current = i > 0 ? instructions[i - 1].first : 0;
            const uint32_t lim = instructions[i].first;

            current = db_str(os, current, lim, BS_OEMName, 8);
            current = dbwd(os, current, lim, BPB_BytsPerSec);
            current = dbwd(os, current, lim, BPB_SecPerClus);
            current = dbwd(os, current, lim, BPB_RsvdSecCnt);
            current = dbwd(os, current, lim, BPB_NumFATs);
            current = dbwd(os, current, lim, BPB_RootEntCnt);
            current = dbwd(os, current, lim, BPB_TotSec16);
            current = dbwd(os, current, lim, BPB_Media);
            current = dbwd(os, current, lim, BPB_FATSz16);
            current = dbwd(os, current, lim, BPB_SecPerTrk);
            current = dbwd(os, current, lim, BPB_NumHeads);
            current = dbwd(os, current, lim, BPB_HiddSec);
            current = dbwd(os, current, lim, BPB_TotSec32);

            if (fat_type == 12 || fat_type == 16)
            {
                current = dbwd(os, current, lim, fat12_16.BS_DrvNum);
                current = dbwd(os, current, lim, fat12_16.BS_Reserved1);
                current = dbwd(os, current, lim, fat12_16.BS_BootSig);
                current = dbwd(os, current, lim, fat12_16.BS_VolID);
                current = db_str(os, current, lim, fat12_16.BS_VolLab, 11);
                current = db_str(os, current, lim, fat12_16.BS_FilSysType, 8);
            }
            else
            {
                current = dbwd(os, current, lim, fat32.BPB_FATSz32);
                current = dbwd(os, current, lim, fat32.BPB_ExtFlags);
                current = dbwd(os, current, lim, fat32.BPB_FSVer);
                current = dbwd(os, current, lim, fat32.BPB_RootClus);
                current = dbwd(os, current, lim, fat32.BPB_FSInfo);
                current = dbwd(os, current, lim, fat32.BPB_BkBootSec);
                current = dbwd(os, current, lim, fat32.BPB_Reserved);
                current = dbwd(os, current, lim, fat32.BS_DrvNum);
                current = dbwd(os, current, lim, fat32.BS_Reserved1);
                current = dbwd(os, current, lim, fat32.BS_BootSig);
                current = dbwd(os, current, lim, fat32.BS_VolID);
                current = db_str(os, current, lim, fat32.BS_VolLab, 11);
                current = db_str(os, current, lim, fat32.BS_FilSysType, 8);
            }

            if (current < lim)
            {
                os << "    times " << format("0x%02x") % lim << " - ($ - $$) db 0" << endl;
            }

            os << "entry:" << endl;
        }

        if (i == 0)
        {
            if (BS_jmpBoot[0] == 0xeb)
            {
                os << "    jmp short entry" << endl;
            }
            else
            {
                os << "    jmp near entry" << endl;
            }
        }
        else
        {
            os << "    " << instructions[i].second << endl;
        }
    }

    if (zero_region_address != numeric_limits<uint32_t>::max())
    {
        os << "    times 0x1fe - ($ - $$) db 0" << endl;
    }

    os << "    db 0x55, 0xaa" << endl;
}

int PbrFat::determine_fat_type() const
{
    // Determin FAT type
    size_t root_dir_sectors =
        (BPB_RootEntCnt * 32 + BPB_BytsPerSec - 1) / BPB_BytsPerSec;

    const size_t fat_size =
        BPB_FATSz16 != 0 ? BPB_FATSz16 : fat32.BPB_FATSz32;

    const size_t total_sectors =
        BPB_TotSec16 != 0 ? BPB_TotSec16 : BPB_TotSec32;

    const size_t data_sectors = total_sectors
        - (BPB_RsvdSecCnt + BPB_NumFATs * fat_size + root_dir_sectors);

    const size_t count_of_clusters = data_sectors / BPB_SecPerClus;

    const int fat_type =
        count_of_clusters < 4085 ? 12
        : count_of_clusters < 65525 ? 16
        : 32;

    return fat_type;
}


text:
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// test_helper_support.cpp

// (C) Copyright 2008 Joaquin M Lopez Munoz.
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// should pass compilation and execution

#include <algorithm>
#include <cstddef>
#include <fstream>

#include <cstdio> // remove
#include <boost/config.hpp>
#if defined(BOOST_NO_STDC_NAMESPACE)
namespace std{
    using ::remove;
}
#endif

#include "test_tools.hpp"
#include <boost/lexical_cast.hpp>
#include <boost/serialization/split_free.hpp>
#include <boost/serialization/vector.hpp>
#include <string>
#include <vector>

// this test uses a special string (my_string) whose contents are shared
// and hence saved in the archive only once.  We need a helper in order
// to convert my_string into a serializable type

class my_string:public std::string
{
    typedef std::string super;

public:
    my_string(){}
    my_string(const super & str): super(str){}
    my_string & operator=(const super& rhs) {
      super::operator=(rhs);
      return *this;
    }
};

struct my_string_helper
{
  typedef std::vector<my_string> table;
  table m_t;
};

BOOST_SERIALIZATION_SPLIT_FREE(my_string)

namespace boost {
namespace serialization {

template<class Archive>
void save(Archive & ar, const my_string & str, const unsigned int /* version */)
{
    void (* const idx)(Archive &, const my_string &, const unsigned int) = & save;
    void * const id = reinterpret_cast<void * const>(idx);
    my_string_helper & msh = ar.template get_helper<my_string_helper>(id);

    my_string_helper::table t = msh.m_t;
    my_string_helper::table::iterator it = std::find(t.begin(), t.end(), str);
    if(it == t.end()){
        my_string_helper::table::size_type s = t.size();
        ar << make_nvp("index", s);
        t.push_back(str);
        ar << make_nvp("string", static_cast<const std::string &>(str));
    }
    else{
        my_string_helper::table::size_type s = it - t.begin();
        ar << make_nvp("index", s);
    }
}

template<class Archive>
void load(Archive & ar, my_string & str, const unsigned int /* version */)
{
    void (* const idx)(Archive &, my_string &, const unsigned int) = & load;
    void * const id = reinterpret_cast<void * const>(idx);
    my_string_helper & msh = ar.template get_helper<my_string_helper>(id);

    my_string_helper::table t = msh.m_t;

    my_string_helper::table::size_type s;
    ar >> make_nvp("index", s);
    t.reserve(s);
    if(s >= t.size()){
        std::string tmp;
        ar >> make_nvp("string", tmp);
        str = tmp;
        t.push_back(str);
    }
    else{
        str = t[s];
    }
}

} // namespace serialization
} // namespace boost

int test_main( int /* argc */, char* /* argv */[] ){
    const char * testfile = boost::archive::tmpnam(NULL);
    BOOST_REQUIRE(NULL != testfile);

    std::vector<my_string> v1;
    for(int i=0; i<1000; ++i){
        v1.push_back(my_string(boost::lexical_cast<std::string>(i % 100)));
    }
    {
        test_ostream os(testfile, TEST_STREAM_FLAGS);
        test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
        oa << boost::serialization::make_nvp("vector", v1);
    }
    {
        std::vector<my_string> v2;
        test_istream is(testfile, TEST_STREAM_FLAGS);
        test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
        ia >> boost::serialization::make_nvp("vector", v2);
        BOOST_CHECK(v1 == v2);
    }
    std::remove(testfile);
    return EXIT_SUCCESS;
}

// EOF

ext: cpp


text:
/*
 * serialize.cpp
 * Date: 2016-02-29
 * Author: Karsten Ahnert (karsten.ahnert@gmx.de)
 * Copyright: Karsten Ahnert
 *
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE_1_0.txt or
 * copy at http://www.boost.org/LICENSE_1_0.txt)
 */

#include "serialize.hpp"

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

#include <sstream>
#include <functional>
#include <unordered_map>

#include <iostream>

namespace dynsys {

std::string serialize_winner( std::array< dynsys::tree_type , dim > const& winner , norm_type const& xnorm , norm_type const& ynorm )
{
    using namespace std;

    boost::property_tree::ptree pt;
    pt.put( "winner.w_1" , gpcxx::polish_string( winner[0] ) );
    pt.put( "winner.w_2" , gpcxx::polish_string( winner[1] ) );
    pt.put( "winner.w_3" , gpcxx::polish_string( winner[2] ) );

    pt.put( "norm.x_1.mean" , xnorm[0].first );
    pt.put( "norm.x_1.stdev" , xnorm[0].second );
    pt.put( "norm.x_2.mean" , xnorm[1].first );
    pt.put( "norm.x_2.stdev" , xnorm[1].second );
    pt.put( "norm.x_3.mean" , xnorm[2].first );
    pt.put( "norm.x_3.stdev" , xnorm[2].second );

    pt.put( "norm.y_1.mean" , ynorm[0].first );
    pt.put( "norm.y_1.stdev" , ynorm[0].second );
    pt.put( "norm.y_2.mean" , ynorm[1].first );
    pt.put( "norm.y_2.stdev" , ynorm[1].second );
    pt.put( "norm.y_3.mean" , ynorm[2].first );
    pt.put( "norm.y_3.stdev" , ynorm[2].second );

    std::ostringstream str;
    boost::property_tree::write_json( str , pt );
    return str.str();
}

deserialized_system deserialize_winner(const std::string& str )
{
    using namespace gpcxx;

    using mapped_func = std::pair< size_t , std::function< node_type() > > ;
    using map_type = std::unordered_map< std::string , mapped_func > ;
    // using mapper_type = std::function< mapped_func( std::string const& ) > ;

    auto map = map_type {
        std::make_pair( "+" , std::make_pair( 2 , []() { return node_type( plus_func {} , "+" ); } ) ) ,
        std::make_pair( "-" , std::make_pair( 2 , []() { return node_type( minus_func {} , "-" ); } ) ) ,
        std::make_pair( "*" , std::make_pair( 2 , []() { return node_type( multiplies_func {} , "*" ); } ) ) ,
        std::make_pair( "/" , std::make_pair( 2 , []() { return node_type( divides_func {} , "/" ); } ) ) ,
        std::make_pair( "c" , std::make_pair( 1 , []() { return node_type( cos_func {} , "c" ); } ) ) ,
        std::make_pair( "s" , std::make_pair( 1 , []() { return node_type( sin_func {} , "s" ); } ) ) ,
        std::make_pair( "e" , std::make_pair( 1 , []() { return node_type( exp_func {} , "e" ); } ) ) ,
        std::make_pair( "l" , std::make_pair( 1 , []() { return node_type( log_func {} , "l" ); } ) ) ,
        std::make_pair( "x" , std::make_pair( 0 , []() { return node_type( array_terminal<0> {} , "x" ); } ) ) ,
        std::make_pair( "y" , std::make_pair( 0 , []() { return node_type( array_terminal<1> {} , "y" ); } ) ) ,
        std::make_pair( "z" , std::make_pair( 0 , []() { return node_type( array_terminal<2> { } , "z" ); } ) )
    };

    auto mapper = [map]( std::string const& str ) {
        auto iter = map.find( str );
        if( iter != map.end() )
        {
            return iter->second;
        }
        else
        {
            auto value = std::stod( str );
            auto func = [str,value]() { return node_type( double_terminal< double > { value } , str ); };
            return mapped_func { std::make_pair( 0 , func ) };
        }
    };

    deserialized_system winner;
    boost::property_tree::ptree pt;
    std::istringstream istr { str };
    boost::property_tree::read_json( istr , pt );
    gpcxx::read_polish( pt.get< std::string >( "winner.w_1" ) , winner.trees[0] , mapper );
    gpcxx::read_polish( pt.get< std::string >( "winner.w_2" ) , winner.trees[1] , mapper );
    gpcxx::read_polish( pt.get< std::string >( "winner.w_3" ) , winner.trees[2] , mapper );

    winner.xnorm[0].first = pt.get< double >( "norm.x_1.mean" );
    winner.xnorm[0].second = pt.get< double >( "norm.x_1.stdev" );
    winner.xnorm[1].first = pt.get< double >( "norm.x_2.mean" );
    winner.xnorm[1].second = pt.get< double >( "norm.x_2.stdev" );
    winner.xnorm[2].first = pt.get< double >( "norm.x_3.mean" );
    winner.xnorm[2].second = pt.get< double >( "norm.x_3.stdev" );

    winner.ynorm[0].first = pt.get< double >( "norm.y_1.mean" );
    winner.ynorm[0].second = pt.get< double >( "norm.y_1.stdev" );
    winner.ynorm[1].first = pt.get< double >( "norm.y_2.mean" );
    winner.ynorm[1].second = pt.get< double >( "norm.y_2.stdev" );
    winner.ynorm[2].first = pt.get< double >( "norm.y_3.mean" );
    winner.ynorm[2].second = pt.get< double >( "norm.y_3.stdev" );

    return winner;
}

} // namespace dynsys

max_stars_repo_name: gchoinka/gpcxx

max_issues_repo_name: gchoinka/gpcxx

max_forks_repo_name: gchoinka/gpcxx


text:
/*
 * This file is part of the Geneva library collection.
 *
 * See the NOTICE file in the top-level directory of the Geneva library
 * collection for a list of contributors and copyright information.
 *
 * The following license applies to the code IN THIS FILE:
 *
 * ***************************************************************************
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * ***************************************************************************
 *
 * NOTE THAT THE BOOST-LICENSE DOES NOT APPLY TO ANY OTHER FILES OF THE
 * GENEVA LIBRARY, UNLESS THIS IS EXPLICITLY STATED IN THE CORRESPONDING FILE!
 */

#pragma once

#include "common/GGlobalDefines.hpp"

// Standard headers go here
#include <string>
#include <istream>
#include <ostream>
#include <cstdio>
#include <cstdlib>
#include <csignal>
#include <cmath>

// Boost headers go here

#include <boost/cast.hpp>

// Geneva headers go here

/******************************************************************************/
/**
 * We need local signals, so we can act both on Windows and POSIX-OSs
 */
#if defined(_MSC_VER)  && (_MSC_VER >= 1020)
#define G_SIGHUP CTRL_CLOSE_EVENT
#else
#define G_SIGHUP SIGHUP
#endif
/******************************************************************************/

namespace Gem {
namespace Common {

/******************************************************************************/
/**
 * By default GBoundedBufferT will have this size. As the buffer
 * dynamically grows and shrinks, we choose a very high value. This
 * is a safeguard against errors like endless loops that might keep
 * filling the buffer until memory is exhausted. In normal work
 * conditions, however, the buffer should never reach its upper
 * limit.
 */
#ifdef GENEVA_COMMON_DEFAULT_BUFFER_SIZE
	const std::size_t DEFAULTBUFFERSIZE = GENEVA_COMMON_DEFAULT_BUFFER_SIZE;
#else
	const std::size_t DEFAULTBUFFERSIZE = 5000;
#endif

/******************************************************************************/
// A basetype used for all enums in Geneva
using ENUMBASETYPE = std::uint16_t;

/******************************************************************************/
/**
 * Indicates the different sources for the GMultiSourceParamater
 */
enum class parameter_source : Gem::Common::ENUMBASETYPE {
   NETWORK = 1
	, COMMAND_LINE = 2
   , ENVIRONMENT_VARIABLE = 3
   , CONFIGURATION_FILE = 4
	, ASSIGNMENT = 5
	, SIZE = parameter_source::ASSIGNMENT
};

/** @brief Puts a Gem::Common::parameter_source into a stream. Needed also for boost::lexical_cast<> */
G_API_COMMON std::ostream &operator<<(std::ostream &, Gem::Common::parameter_source const &);

/** @brief Reads a Gem::Common::parameter_source item from a stream. Needed also for boost::lexical_cast<> */
G_API_COMMON std::istream &operator>>(std::istream &, Gem::Common::parameter_source &);

/******************************************************************************/
/**
 * This enum denotes different dimensions (used particularly by GDecoratorCollection
 */
enum class dimensions : Gem::Common::ENUMBASETYPE {
	Dim1 = 1
	, Dim2 = 2
	, Dim3 = 3
	, Dim4 = 4
};

/** @brief Puts a Gem::Common::dimensions into a stream. Needed also for boost::lexical_cast<> */
G_API_COMMON std::ostream &operator<<(std::ostream &, Gem::Common::dimensions const &);

/** @brief Reads a Gem::Common::dimensions item from a stream. Needed also for boost::lexical_cast<> */
G_API_COMMON std::istream &operator>>(std::istream &, Gem::Common::dimensions &);

/******************************************************************************/
/**
 * The limit for similarity checks of floating point numbers
 */
const double CE_DEF_SIMILARITY_DIFFERENCE = pow(10., -5);

/******************************************************************************/
/**
 * Indicates whether higher or lower values are considered better. Needed e.g.
 * in conjunction with the sorting in priority queues.
 */
enum class sortOrder : Gem::Common::ENUMBASETYPE {
	 LOWERISBETTER = 0
	 , HIGHERISBETTER = 1
};

/** @brief Puts a Gem::Common::sortOrder into a stream. Needed also for boost::lexical_cast<> */
G_API_COMMON std::ostream &operator<<(std::ostream &, Gem::Common::sortOrder const &);

/** @brief Reads a Gem::Common::sortOrder item from a stream. Needed also for boost::lexical_cast<> */
G_API_COMMON std::istream &operator>>(std::istream &, Gem::Common::sortOrder &);

/******************************************************************************/
/**
 * Different log and exception types
 */
enum class logType : Gem::Common::ENUMBASETYPE {
	EXCEPTION = 0
	, TERMINATION = 1
	, WARNING = 2
	, LOGGING = 3
	, FILE = 4
	, STDOUT = 5
	, STDERR = 6
};

/** @brief Puts a Gem::Common::logType into a stream. Needed also for boost::lexical_cast<> */
G_API_COMMON std::ostream &operator<<(std::ostream &, Gem::Common::logType const &);

/** @brief Reads a Gem::Common::logType item from a stream. Needed also for boost::lexical_cast<> */
G_API_COMMON std::istream &operator>>(std::istream &, Gem::Common::logType &);

/******************************************************************************/
/**
 * The default number of bins in histograms, used in GPlotDesigner
 */
const std::size_t DEFAULTNBINSGPD = 100;

/******************************************************************************/
/**
 * Used in parameter definitions (GParserBuilder)
 */
const bool VAR_IS_ESSENTIAL = true;
const bool VAR_IS_SECONDARY = false;

/******************************************************************************/
/**
 * Needed for the serialization of boost::logic::tribool
 */
enum class triboolStates : Gem::Common::ENUMBASETYPE {
	TBS_FALSE
	, TBS_INDETERMINATE
	, TBS_TRUE
};

/** @brief Puts a Gem::Common::triboolStates into a stream. Needed also for boost::lexical_cast<> */
G_API_COMMON std::ostream &operator<<(std::ostream &, Gem::Common::triboolStates const &);

/** @brief Reads a Gem::Common::triboolStates item from a stream. Needed also for boost::lexical_cast<> */
G_API_COMMON std::istream &operator>>(std::istream &, Gem::Common::triboolStates &);

/******************************************************************************/
/**
 * The serialization modes that are currently allowed
 */
enum class serializationMode : Gem::Common::ENUMBASETYPE {
	TEXT = 0
	, XML = 1
	, BINARY = 2
};

/** @brief Puts a Gem::Common::serializationMode into a stream. Needed also for boost::lexical_cast<> */
G_API_COMMON std::ostream &operator<<(std::ostream &, Gem::Common::serializationMode const &);

/** @brief Reads a Gem::Common::serializationMode item from a stream. Needed also for boost::lexical_cast<> */
G_API_COMMON std::istream &operator>>(std::istream &, Gem::Common::serializationMode &);

/** @brief Converts a serializationMode to a string representation for debugging purposes */
G_API_COMMON std::string serModeToString(Gem::Common::serializationMode);

/******************************************************************************/
/**
 * Specification of whether checkExpectation should emit messages
 */
const bool CE_SILENT = false;
const bool CE_WITH_MESSAGES = true;

/******************************************************************************/
/**
 * Needed to express expectations in testing framework.
 */
enum class expectation : Gem::Common::ENUMBASETYPE {
	EQUALITY = 0 // bitwise equality of all checked components
	, FP_SIMILARITY = 1 // equality for non-floating point components, similarity for floating point
	, INEQUALITY = 2// at least one checked component differs
};

/** @brief Puts a Gem::Common::expectation into a stream. Needed also for boost::lexical_cast<> */
G_API_COMMON std::ostream &operator<<(std::ostream &, Gem::Common::expectation const &);

/** @brief Reads a Gem::Common::expectation item from a stream. Needed also for boost::lexical_cast<> */
G_API_COMMON std::istream &operator>>(std::istream &, Gem::Common::expectation &);

/******************************************************************************/
/**
 * Specification of the default number of threads, when no information about hardware
 * concurrency can be determined.
 */
const unsigned int DEFAULTNHARDWARETHREADS = 2;
/**
 * Specification of the default maximum number of threads
 */
const unsigned int DEFAULTMAXNHARDWARETHREADS = 4;

/******************************************************************************/

} /* namespace Common */
} /* namespace Gem */

ext: hpp

max_stars_repo_name: denisbertini/geneva

max_issues_repo_name: denisbertini/geneva

max_forks_repo_name: denisbertini/geneva