Skip to content

Commit

Permalink
Added support for caching in card ( variant and type sequence ) (Boos…
Browse files Browse the repository at this point in the history
  • Loading branch information
gopi487krishna authored Sep 18, 2020
1 parent 8824c60 commit ac8be8f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
27 changes: 16 additions & 11 deletions include/boost/astronomy/io/card.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/*=============================================================================
Copyright 2018 Pranam Lashkari <[email protected]>
Copyright 2020 Gopi Krishna Menon <[email protected]>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file License.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
Expand All @@ -14,29 +16,27 @@ file License.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
#include <boost/algorithm/string/trim.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/type.hpp>
#include <boost/variant.hpp>

#include <boost/astronomy/io/default_card_policy.hpp>
#include <boost/astronomy/exception/fits_exception.hpp>

/**
* @file card.hpp
* @author Pranam Lashkari
* @details Contains the definition for <strong>card</strong> structure
*/


namespace boost { namespace astronomy { namespace io {

/**
* @brief Represents the concept of <strong>card</strong> associated with the FITS standard.
* @details This structure provides functions for storage, manipulation and access of FITS cards.
* @author Pranam Lashkari
* @author Pranam Lashkari, Gopi Krishna Menon
*/
template<typename CardPolicy>
struct card: CardPolicy
{
private:
std::string card_;
typedef typename boost::make_variant_over< typename CardPolicy::supported_types>::type cache;
mutable cache card_cache;

public:

Expand Down Expand Up @@ -146,15 +146,15 @@ struct card: CardPolicy
/**
* @brief Returns the value associated with the card
* @tparam ReturnType The type to which the value should be converted and returned
* @pre The ReturnType can be int, long long( If more than 10 digits) ,double,long double,float ,bool,string
* @return The value associated with the card
* @throws boost::bad_lexical_cast If the conversion could not succeed
* @todo Support for complex numbers and date is required
*/
template <typename ReturnType>
ReturnType value() const
{
return value_imp(boost::type<ReturnType>());
if (!value_cached()) {
card_cache =value_imp(boost::type<ReturnType>());
}
return boost::get<ReturnType>(card_cache);
}

/**
Expand All @@ -173,14 +173,14 @@ struct card: CardPolicy
template<typename DataType>
void set_value(DataType value)
{

std::string serialized_value = this->serialize_to_fits_format(value);

if (this->is_card_valid(this->keyword(), serialized_value, "")) {
std::string key = this->keyword(true);
this->card_.clear();
this->card_.append(key);
this->card_.append(serialized_value);
card_cache = value;
}
}

Expand All @@ -190,6 +190,11 @@ struct card: CardPolicy
const std::string& raw_card() { return card_; }

private:
/**
* @brief Checks if the value is internally cached
*/
bool value_cached() const { return card_cache.which() != 0; }


/**
* @brief Creates a card from key, value and comment supplied to the method
Expand Down
17 changes: 17 additions & 0 deletions include/boost/astronomy/io/default_card_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ file License.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
#include <boost/astronomy/exception/fits_exception.hpp>
#include <boost/astronomy/io/string_conversion_utility.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/mpl/vector.hpp>

namespace boost{namespace astronomy{namespace io{

Expand All @@ -25,6 +26,22 @@ class card_policy {
std::vector<std::string> reserved_keywords;

public:
typedef boost::mpl::vector<
boost::blank,
bool,
int,
float,
double,
unsigned int,
long long,
std::size_t,
std::string,
std::complex<int>,
std::complex<double>,
std::complex<float>,
std::complex<std::size_t>
> supported_types;


/**
* @brief Constructs a default object of card and initializes the reserved keyword list
Expand Down

0 comments on commit ac8be8f

Please sign in to comment.