| 1 |
// |
| 2 |
// Copyright (c) 2010-2017 Benjamin Kaufmann |
| 3 |
// |
| 4 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
// of this software and associated documentation files (the "Software"), to |
| 6 |
// deal in the Software without restriction, including without limitation the |
| 7 |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 |
// sell copies of the Software, and to permit persons to whom the Software is |
| 9 |
// furnished to do so, subject to the following conditions: |
| 10 |
// |
| 11 |
// The above copyright notice and this permission notice shall be included in |
| 12 |
// all copies or substantial portions of the Software. |
| 13 |
// |
| 14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 |
// IN THE SOFTWARE. |
| 21 |
// |
| 22 |
// |
| 23 |
// NOTE: ProgramOptions is inspired by Boost.Program_options |
| 24 |
// see: www.boost.org/libs/program_options |
| 25 |
// |
| 26 |
#ifndef PROGRAM_OPTIONS_MAPPED_VALUE_H_INCLUDED |
| 27 |
#define PROGRAM_OPTIONS_MAPPED_VALUE_H_INCLUDED |
| 28 |
#ifdef _MSC_VER |
| 29 |
#pragma warning (disable : 4786) |
| 30 |
#pragma warning (disable : 4503) |
| 31 |
#endif |
| 32 |
#include <potassco/program_opts/typed_value.h> |
| 33 |
#include <potassco/program_opts/value_store.h> |
| 34 |
#include <string> |
| 35 |
#include <cstddef> |
| 36 |
#include <map> |
| 37 |
#if defined(_MSC_VER) && _MSC_VER <= 1200 |
| 38 |
namespace std { using ::size_t; } |
| 39 |
#endif |
| 40 |
|
| 41 |
namespace Potassco { namespace ProgramOptions { |
| 42 |
/////////////////////////////////////////////////////////////////////////////// |
| 43 |
// ValueMap |
| 44 |
/////////////////////////////////////////////////////////////////////////////// |
| 45 |
//! Type for storing anonymous values. |
| 46 |
/*! |
| 47 |
* Maps option names to their values. |
| 48 |
*/ |
| 49 |
class ValueMap { |
| 50 |
public: |
| 51 |
ValueMap() {} |
| 52 |
~ValueMap() {} |
| 53 |
bool empty() const { return map_.empty(); } |
| 54 |
size_t size() const { return map_.size(); } |
| 55 |
size_t count(const std::string& name) const { return map_.count(name); } |
| 56 |
void clear() { map_.clear(); } |
| 57 |
const ValueStore& operator[](const std::string& name) const { |
| 58 |
MapType::const_iterator it = map_.find(name); |
| 59 |
if (it == map_.end()) { |
| 60 |
throw UnknownOption("ValueMap", name); |
| 61 |
} |
| 62 |
return it->second; |
| 63 |
} |
| 64 |
template <class T> |
| 65 |
static bool add(ValueMap* this_, const std::string& name, const T* value) { |
| 66 |
MapType::iterator it = this_->map_.find(name); |
| 67 |
if (it == this_->map_.end()) { |
| 68 |
it = this_->map_.insert(it, MapType::value_type(name, ValueStore())); |
| 69 |
} |
| 70 |
if (it->second.extract_raw() != value) { |
| 71 |
it->second.assimilate(const_cast<T*>(value)); |
| 72 |
} |
| 73 |
return true; |
| 74 |
} |
| 75 |
private: |
| 76 |
ValueMap(const ValueMap&); |
| 77 |
ValueMap& operator=(const ValueMap&); |
| 78 |
typedef std::map<std::string, ValueStore> MapType; |
| 79 |
MapType map_; |
| 80 |
}; |
| 81 |
|
| 82 |
/*! |
| 83 |
* Creates a value that is created on demand and stored in a given value map. |
| 84 |
* |
| 85 |
* \see OptionGroup::addOptions() |
| 86 |
*/ |
| 87 |
template <class T> |
| 88 |
inline NotifiedValue<T>* store(ValueMap& map, typename detail::Parser<T>::type p = &string_cast<T>) { |
| 89 |
return notify<T>(&map, &ValueMap::add<T>, p); |
| 90 |
} |
| 91 |
|
| 92 |
inline NotifiedValue<bool>* flag(ValueMap& map, FlagAction a = store_true) { |
| 93 |
return flag(&map, &ValueMap::add<bool>, a); |
| 94 |
} |
| 95 |
|
| 96 |
}} |
| 97 |
#endif |