TBTK
Need a break? Support the development by playing Polarity Puzzles
Boolean.h
Go to the documentation of this file.
1 /* Copyright 2019 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_BOOLEAN
24 #define COM_DAFER45_TBTK_BOOLEAN
25 
27 
28 #ifndef TBTK_DISABLE_NLOHMANN_JSON
29 # include "TBTK/json.hpp"
30 #endif
31 
32 namespace TBTK{
33 
34 #if TBTK_WRAP_PRIMITIVE_TYPES
35 
38 public:
40  Boolean(){};
41 
45  constexpr Boolean(bool value) : value(value) {}
46 
53  Boolean(const std::string &serialization, Serializable::Mode mode);
54 
56  constexpr operator bool() const{ return value; };
57 
63  Boolean& operator=(bool rhs){
64  value = rhs;
65 
66  return *this;
67  }
68 
76  std::string serialize(Serializable::Mode mode) const;
77 
78 #ifndef TBTK_DISABLE_NLOHMANN_JSON
79 
83  friend void to_json(nlohmann::json &j, const Boolean &boolean){
84  nlohmann::to_json(j, boolean.value);
85  }
86 
91  friend void from_json(const nlohmann::json &j, Boolean &boolean){
92  nlohmann::from_json(j, boolean.value);
93  }
94 #endif
95 private:
97  bool value;
98 };
99 
101  const std::string &serialization,
102  Serializable::Mode mode
103 ){
104  switch(mode){
105  case Serializable::Mode::JSON:
106  value = stoi(serialization);
107  break;
108  default:
109  TBTKExit(
110  "Boolean::Boolean()",
111  "Only Serializable::Mode::JSON is supported yet.",
112  ""
113  );
114  }
115 }
116 
117 inline std::string Boolean::serialize(Serializable::Mode mode) const{
118  switch(mode){
119  case Serializable::Mode::JSON:
120  return std::to_string(value);
121  default:
122  TBTKExit(
123  "Boolean::serialize()",
124  "Only Serializable::Mode::JSON is supported yet.",
125  ""
126  );
127  }
128 }
129 
130 #else
131  typedef int Boolean;
132 #endif
133 
134 }; //End of namespace TBTK
135 
136 #endif
Boolean()
Definition: Boolean.h:40
Boolean number.
Definition: Boolean.h:37
Base class for psudo-serializable objects.
Definition: PseudoSerializable.h:31
std::string serialize(Serializable::Mode mode) const
Definition: Boolean.h:117
Definition: Boolean.h:32
Boolean & operator=(bool rhs)
Definition: Boolean.h:63
constexpr Boolean(bool value)
Definition: Boolean.h:45
Mode
Definition: Serializable.h:47
friend void from_json(const nlohmann::json &j, Boolean &boolean)
Definition: Boolean.h:91
friend void to_json(nlohmann::json &j, const Boolean &boolean)
Definition: Boolean.h:83