StarToken.hpp
Go to the documentation of this file.
1/*
2 Copyright 2013 Statoil ASA.
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef STAR_TOKEN_HPP
21#define STAR_TOKEN_HPP
22
23#include <cctype>
24#include <string>
25
28
29namespace Opm {
30 bool isStarToken(const string_view& token,
31 std::string& countString,
32 std::string& valueString);
33
34 template <class T>
36
37class StarToken {
38public:
39 StarToken(const string_view& token)
40 {
41 if (!isStarToken(token, m_countString, m_valueString))
42 throw std::invalid_argument("Token \""+ token +"\" is not a repetition specifier");
43 init_(token);
44 }
45
46 StarToken(const string_view& token, const std::string& countStr, const std::string& valueStr)
47 : m_countString(countStr)
48 , m_valueString(valueStr)
49 {
50 init_(token);
51 }
52
53 std::size_t count() const {
54 return m_count;
55 }
56
57 bool hasValue() const {
58 return !m_valueString.empty();
59 }
60
61 // returns the coubt as rendered in the deck. note that this might be different
62 // than just converting the return value of count() to a string because an empty
63 // count is interpreted as 1...
64 const std::string& countString() const {
65 return m_countString;
66 }
67
68 // returns the value as rendered in the deck. note that this might be different
69 // than just converting the return value of value() to a string because values
70 // might have different representations in the deck (e.g. strings can be
71 // specified with and without quotes and but spaces are only allowed using the
72 // first representation.)
73 const std::string& valueString() const {
74 return m_valueString;
75 }
76
77private:
78 // internal initialization method. the m_countString and m_valueString attributes
79 // must be set before calling this method.
80 void init_(const string_view& token);
81
82 std::size_t m_count;
83 std::string m_countString;
84 std::string m_valueString;
85};
86}
87
88
89#endif
const char *const string
Definition: cJSON.h:170
Definition: StarToken.hpp:37
std::size_t count() const
Definition: StarToken.hpp:53
const std::string & valueString() const
Definition: StarToken.hpp:73
const std::string & countString() const
Definition: StarToken.hpp:64
bool hasValue() const
Definition: StarToken.hpp:57
StarToken(const string_view &token)
Definition: StarToken.hpp:39
StarToken(const string_view &token, const std::string &countStr, const std::string &valueStr)
Definition: StarToken.hpp:46
Definition: Stringview.hpp:48
Definition: A.hpp:4
bool isStarToken(const string_view &token, std::string &countString, std::string &valueString)
T readValueToken(string_view)