Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1ff35d1

Browse files
Revise logic elements using inheritance. Add graphical visualisation
1 parent c007407 commit 1ff35d1

File tree

7 files changed

+931
-0
lines changed

7 files changed

+931
-0
lines changed

‎projects/CMakeLists.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ add_subdirectory(ch12/koch_snowflake)
2121
add_subdirectory(ch13/cross_wheel)
2222
add_subdirectory(ch13/fibonacci_spiral)
2323
add_subdirectory(ch13/hexagon_tile)
24+
25+
add_subdirectory(ch14/logic_shapes)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
set(TARGET "logic_shapes")
4+
5+
set(HEADERS
6+
logic.h
7+
logic_shapes.h
8+
${LIB_DIR}/Graph_lib/ext/graph.h
9+
${LIB_DIR}/Graph_lib/fltk.h
10+
${LIB_DIR}/Graph_lib/Graph.h
11+
${LIB_DIR}/Graph_lib/GUI.h
12+
${LIB_DIR}/Graph_lib/Point.h
13+
${LIB_DIR}/Graph_lib/Simple_window.h
14+
${LIB_DIR}/Graph_lib/Window.h
15+
)
16+
set(SOURCES
17+
main.cpp
18+
logic.cpp
19+
logic_shapes.cpp
20+
${LIB_DIR}/Graph_lib/ext/graph.cpp
21+
${LIB_DIR}/Graph_lib/Graph.cpp
22+
${LIB_DIR}/Graph_lib/GUI.cpp
23+
${LIB_DIR}/Graph_lib/Window.cpp
24+
)
25+
26+
project(${TARGET} CXX)
27+
28+
set(FLTK_SKIP_FLUID True)
29+
set(FLTK_SKIP_FORMS True)
30+
31+
find_package(FLTK 1.3.8 EXACT REQUIRED)
32+
find_package(OpenGL REQUIRED)
33+
34+
include_directories(SYSTEM ${FLTK_INCLUDE_DIR})
35+
link_directories(${FLTK_INCLUDE_DIR}/../lib)
36+
37+
add_executable(${TARGET} ${HEADERS} ${SOURCES})
38+
39+
target_link_libraries(${TARGET} ${FLTK_LIBRARIES} ${OPENGL_LIBRARIES})
40+
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
41+
target_link_libraries(${TARGET} fltk_jpeg fltk_png fltk_z)
42+
endif()
43+
44+
install(TARGETS ${TARGET})

‎projects/ch14/logic_shapes/logic.cpp‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include <stdexcept>
2+
3+
#include "logic.h"
4+
5+
6+
namespace Logic {
7+
8+
ElemInput::operator bool () const
9+
{
10+
if (elem)
11+
return inv ? !*elem : *elem;
12+
else
13+
return inv;
14+
}
15+
16+
17+
void Element::set (bool value)
18+
{
19+
if (inverted_out)
20+
value = !value;
21+
22+
if (out != value)
23+
{
24+
out = value;
25+
26+
if (cb)
27+
cb(*this);
28+
29+
for (auto elem : outputs)
30+
elem->calc();
31+
}
32+
}
33+
34+
35+
Element& operator>> (Element& lhs, Operation& rhs)
36+
{
37+
check_loop(rhs, lhs);
38+
39+
rhs.inputs.push_back(ElemInput{lhs, false});
40+
lhs.outputs.push_back(&rhs);
41+
42+
rhs.calc();
43+
return rhs;
44+
}
45+
46+
Element& operator>> (Element& lhs, Connection rhs)
47+
{
48+
check_loop(rhs.oper_elem, lhs);
49+
50+
rhs.oper_elem.inputs.push_back(ElemInput{lhs, rhs.inverted});
51+
lhs.outputs.push_back(&rhs.oper_elem);
52+
53+
rhs.oper_elem.calc();
54+
return rhs.oper_elem;
55+
}
56+
57+
58+
void check_loop (const Element& loop, const Element& elem)
59+
{
60+
// checking if elem is the loop
61+
if (&loop == &elem)
62+
throw std::runtime_error{"loop detected in connections of logic elements"};
63+
64+
for (const auto& output : loop.get_outputs())
65+
check_loop(*output, elem);
66+
}
67+
68+
69+
void Source::calc ()
70+
{
71+
// nothing to do here
72+
}
73+
74+
75+
void And::calc ()
76+
{
77+
bool res{ get_inputs().size() != 0 };
78+
79+
for (const auto& input : get_inputs())
80+
{
81+
if (!input)
82+
{
83+
res = false;
84+
break;
85+
}
86+
}
87+
88+
set(res);
89+
}
90+
91+
void Or::calc ()
92+
{
93+
bool res{ false };
94+
95+
for (const auto& input : get_inputs())
96+
{
97+
if (input)
98+
{
99+
res = true;
100+
break;
101+
}
102+
}
103+
104+
set(res);
105+
}
106+
107+
} // namespace Logic

‎projects/ch14/logic_shapes/logic.h‎

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
#ifndef LOGIC_H
2+
#define LOGIC_H 1
3+
4+
#include <functional>
5+
#include <vector>
6+
7+
8+
namespace Logic {
9+
10+
class Element;
11+
class Source;
12+
class Operation;
13+
struct Connection;
14+
15+
16+
enum class Out_state
17+
{
18+
direct = 0,
19+
inverted = 1,
20+
};
21+
22+
23+
// represents a pair of element and state of inverted property
24+
struct ElemInput
25+
{
26+
ElemInput () = delete;
27+
ElemInput (const ElemInput& input) = default;
28+
29+
ElemInput (const Element& input_elem, bool inverted)
30+
: elem { &input_elem }
31+
, inv { inverted }
32+
{}
33+
34+
ElemInput& operator= (const ElemInput& input) = default;
35+
36+
operator bool () const;
37+
38+
const Element* elem;
39+
bool inv;
40+
};
41+
42+
43+
using Logic_callback = std::function<void(const Element&)>;
44+
45+
using Input_container = std::vector<ElemInput>;
46+
using Output_container = std::vector<Element*>;
47+
48+
49+
// abstract logic element
50+
class Element
51+
{
52+
public:
53+
explicit Element (Out_state st = Out_state::direct, Logic_callback f = nullptr)
54+
: inverted_out { st == Out_state::inverted }
55+
, out { inverted_out }
56+
, cb { f }
57+
{}
58+
59+
operator bool () const { return out; }
60+
bool inverted () const { return inverted_out; }
61+
const Output_container& get_outputs () const { return outputs; }
62+
63+
void set_callback (Logic_callback f) { cb = f; }
64+
65+
protected:
66+
void set (bool value);
67+
// abstract method, set() should be called in calc()
68+
virtual void calc () = 0;
69+
70+
private:
71+
bool inverted_out { false };
72+
bool out { false };
73+
Logic_callback cb;
74+
75+
// connections
76+
Output_container outputs;
77+
78+
friend Element& operator>> (Element& lhs, Operation& rhs);
79+
friend Element& operator>> (Element& lhs, Connection rhs);
80+
friend void check_loop (const Element& loop, const Element& elem);
81+
};
82+
83+
84+
class Source : public Element
85+
{
86+
public:
87+
explicit Source (Out_state st = Out_state::direct, Logic_callback f = nullptr)
88+
: Element{ st, f }
89+
{}
90+
91+
Source& operator= (bool value) { set(value); return *this; }
92+
93+
protected:
94+
virtual void calc () override;
95+
};
96+
97+
98+
// must be used only for connection operation
99+
struct Connection
100+
{
101+
Connection () = delete;
102+
Connection (const Connection&) = default;
103+
104+
// constructor without "explicit" keyword
105+
Connection (Operation& oper, bool inv = false)
106+
: oper_elem { oper }
107+
, inverted { inv }
108+
{}
109+
110+
Connection& operator= (const Connection&) = delete;
111+
112+
Connection& operator~ ()
113+
{
114+
inverted = !inverted;
115+
return *this;
116+
}
117+
118+
Operation& oper_elem;
119+
bool inverted;
120+
};
121+
122+
class Operation : public Element
123+
{
124+
public:
125+
explicit Operation (Out_state st = Out_state::direct, Logic_callback f = nullptr)
126+
: Element{ st, f }
127+
{}
128+
129+
const Input_container& get_inputs () const { return inputs; }
130+
131+
Connection operator~ () { return Connection{ *this, true }; }
132+
133+
private:
134+
Input_container inputs;
135+
136+
friend Element& operator>> (Element& lhs, Operation& rhs);
137+
friend Element& operator>> (Element& lhs, Connection rhs);
138+
};
139+
140+
141+
Element& operator>> (Element& lhs, Operation& rhs);
142+
Element& operator>> (Element& lhs, Connection rhs);
143+
void check_loop (const Element& loop, const Element& elem);
144+
145+
146+
class And : public Operation
147+
{
148+
public:
149+
explicit And (Out_state out_inverted = Out_state::direct, Logic_callback f = nullptr)
150+
: Operation{ out_inverted, f }
151+
{}
152+
153+
protected:
154+
virtual void calc () override;
155+
};
156+
157+
class Or : public Operation
158+
{
159+
public:
160+
explicit Or (Out_state st = Out_state::direct, Logic_callback f = nullptr)
161+
: Operation{ st, f }
162+
{}
163+
164+
protected:
165+
virtual void calc () override;
166+
};
167+
168+
} // namespace Logic
169+
170+
#endif // LOGIC_H

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /