| 1 | /* |
|---|
| 2 | * Kazuhiki |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2007-2008 FURUHASHI Sadayuki |
|---|
| 5 | * |
|---|
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
|---|
| 7 | * of this software and associated documentation files (the "Software"), to deal |
|---|
| 8 | * in the Software without restriction, including without limitation the rights |
|---|
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|---|
| 10 | * copies of the Software, and to permit persons to whom the Software is |
|---|
| 11 | * furnished to do so, subject to the following conditions: |
|---|
| 12 | * |
|---|
| 13 | * The above copyright notice and this permission notice shall be included in |
|---|
| 14 | * all copies or substantial portions of the Software. |
|---|
| 15 | * |
|---|
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|---|
| 22 | * THE SOFTWARE. |
|---|
| 23 | */ |
|---|
| 24 | |
|---|
| 25 | #ifndef KAZUHIKI_BASIC_IMPL_H__ |
|---|
| 26 | #define KAZUHIKI_BASIC_IMPL_H__ |
|---|
| 27 | |
|---|
| 28 | namespace Kazuhiki { |
|---|
| 29 | namespace IMPL { |
|---|
| 30 | |
|---|
| 31 | bool ConvertBoolean(const char* arg, bool& data) { |
|---|
| 32 | if( std::strcmp("true", arg) == 0 || |
|---|
| 33 | std::strcmp("yes", arg) == 0 || |
|---|
| 34 | std::strcmp("y", arg) == 0 || |
|---|
| 35 | std::strcmp("on", arg) == 0 ) { |
|---|
| 36 | data = true; |
|---|
| 37 | return true; |
|---|
| 38 | } else if( |
|---|
| 39 | std::strcmp("false", arg) == 0 || |
|---|
| 40 | std::strcmp("no", arg) == 0 || |
|---|
| 41 | std::strcmp("n", arg) == 0 || |
|---|
| 42 | std::strcmp("off", arg) == 0 ) { |
|---|
| 43 | data = false; |
|---|
| 44 | return true; |
|---|
| 45 | } |
|---|
| 46 | return false; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | template <typename T> |
|---|
| 50 | bool ConvertNumeric(const char* arg, T& data) { |
|---|
| 51 | std::istringstream stream(arg); |
|---|
| 52 | stream >> data; |
|---|
| 53 | if( stream.fail() || stream.bad() ) { return false; } |
|---|
| 54 | return true; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | template <typename T> |
|---|
| 58 | struct NumericIMPL : Acceptable { |
|---|
| 59 | NumericIMPL(T& d) : data(d) {} |
|---|
| 60 | unsigned int operator() (int argc, char* argv[]) { |
|---|
| 61 | if( argc < 1 ) { throw LackOfArgumentsError("A number is required"); } |
|---|
| 62 | if( !IMPL::ConvertNumeric(argv[0], data) ) { |
|---|
| 63 | throw InvalidArgumentError("Unexpected number"); |
|---|
| 64 | } |
|---|
| 65 | return 1; |
|---|
| 66 | } |
|---|
| 67 | private: |
|---|
| 68 | T& data; |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | template <typename F> |
|---|
| 72 | struct Action : Acceptable { |
|---|
| 73 | Action(F f) : function(f) {} |
|---|
| 74 | unsigned int operator() (int argc, char* argv[]) { |
|---|
| 75 | return function(argc, argv); |
|---|
| 76 | } |
|---|
| 77 | private: |
|---|
| 78 | F function; |
|---|
| 79 | }; |
|---|
| 80 | |
|---|
| 81 | } // namespace IMPL |
|---|
| 82 | } // namespace Kazuhiki |
|---|
| 83 | |
|---|
| 84 | #endif /* kazuhiki/basic_impl.h */ |
|---|
| 85 | |
|---|