root/tlate/string_value.hh
| Revision c04e3c7b94fa30438cb7c897e866588a2a959589, 2.2 KB (checked in by Antti-Juhani Kaijanaho <antti-juhani@…>, 2 years ago) | |
|---|---|
|
|
| Line | |
|---|---|
| 1 | /* This file is part of Alue, the multiprotocol Internet discussion daemon |
| 2 | |
| 3 | Copyright © 2009, 2010 Antti-Juhani Kaijanaho |
| 4 | |
| 5 | Alue is free software: you can redistribute it and/or modify it |
| 6 | under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | Alue is distributed in the hope that it will be useful, but |
| 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with Alue. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | */ |
| 19 | |
| 20 | #ifndef GUARD_TLATE_STRING_VALUE_HH |
| 21 | #define GUARD_TLATE_STRING_VALUE_HH |
| 22 | |
| 23 | #include "value.hh" |
| 24 | |
| 25 | #include <algorithm> |
| 26 | #include <string> |
| 27 | |
| 28 | namespace tlate |
| 29 | { |
| 30 | class string_value : public value |
| 31 | { |
| 32 | std::string val; |
| 33 | public: |
| 34 | typedef boost::shared_ptr<string_value> ptr; |
| 35 | typedef boost::shared_ptr<const string_value> const_ptr; |
| 36 | |
| 37 | explicit string_value(std::string val) : val(val) {} |
| 38 | explicit string_value(int val_, unsigned min_digits = 1) { |
| 39 | bool sign = val_ < 0; |
| 40 | if (sign) val_ = -val_; |
| 41 | while (val_ > 0) { |
| 42 | val += std::string(1, (val_ % 10) + '0'); |
| 43 | val_ = val_ / 10; |
| 44 | } |
| 45 | while (val.length() < min_digits) val += "0"; |
| 46 | if (sign) val += "-"; |
| 47 | std::reverse(val.begin(), val.end()); |
| 48 | } |
| 49 | |
| 50 | std::string get() const { return val; } |
| 51 | |
| 52 | static std::string get(value::const_ptr v) { |
| 53 | const_ptr p = boost::dynamic_pointer_cast |
| 54 | <const string_value> |
| 55 | (v); |
| 56 | return p ? p->get() : ""; |
| 57 | } |
| 58 | |
| 59 | void print(std::ostream & os) const { os << val; } |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | #endif /* GUARD_TLATE_STRING_VALUE_HH */ |
Note: See TracBrowser
for help on using the browser.
