Skip to content

Commit 6763312

Browse files
committed
Make C++98 compatible again
The main reason is Python 2 (also 3.5).
1 parent 4c832aa commit 6763312

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+478
-750
lines changed

atari_py/ale_interface/src/ale_interface.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ void ALEInterface::disableBufferedIO() {
6868
std::cout.sync_with_stdio();
6969
}
7070

71-
void ALEInterface::createOSystem(std::unique_ptr<OSystem> &theOSystem,
72-
std::unique_ptr<Settings> &theSettings) {
71+
void ALEInterface::createOSystem(scoped_ptr<OSystem>& theOSystem,
72+
scoped_ptr<Settings>& theSettings) {
7373
#if (defined(_WIN32) || defined(__MINGW32__))
7474
theOSystem.reset(new OSystemWin32());
7575
theSettings.reset(new SettingsWin32(theOSystem.get()));
@@ -81,8 +81,8 @@ void ALEInterface::createOSystem(std::unique_ptr<OSystem> &theOSystem,
8181
theOSystem->settings().loadConfig();
8282
}
8383

84-
void ALEInterface::checkForUnsupportedRom(std::unique_ptr<OSystem>& theOSystem) {
85-
const Properties properties = theOSystem->console().properties();
84+
void ALEInterface::checkForUnsupportedRom(OSystem& theOSystem) {
85+
const Properties properties = theOSystem.console().properties();
8686
const std::string md5 = properties.get(Cartridge_MD5);
8787
bool found = false;
8888
std::ifstream ss("md5.txt");
@@ -105,27 +105,27 @@ void ALEInterface::checkForUnsupportedRom(std::unique_ptr<OSystem>& theOSystem)
105105
}
106106

107107
void ALEInterface::loadSettings(const std::string& rom, const std::string& name,
108-
std::unique_ptr<OSystem> &theOSystem) {
108+
OSystem& theOSystem) {
109109
// Load the configuration from a config file (passed on the command
110110
// line), if provided
111-
std::string configFile = theOSystem->settings().getString("config", false);
111+
std::string configFile = theOSystem.settings().getString("config", false);
112112

113113
if (!configFile.empty()) {
114-
theOSystem->settings().loadConfig(configFile.c_str());
114+
theOSystem.settings().loadConfig(configFile.c_str());
115115
}
116116

117-
theOSystem->settings().validate();
118-
theOSystem->create();
117+
theOSystem.settings().validate();
118+
theOSystem.create();
119119

120120
// Attempt to load the ROM
121121
if (rom.empty()) {
122122
Logger::Error << "Empty ROM File specified"
123123
<< std::endl;
124124
exit(1);
125-
} else if (theOSystem->createConsole(rom, name)) {
125+
} else if (theOSystem.createConsole(rom, name)) {
126126
checkForUnsupportedRom(theOSystem);
127127
Logger::Info << "Running ROM " << name << "..." << std::endl;
128-
theOSystem->settings().setString("rom_name", name);
128+
theOSystem.settings().setString("rom_name", name);
129129
} else {
130130
Logger::Error << "Unable to create console for " << name << std::endl;
131131
exit(1);
@@ -134,11 +134,11 @@ void ALEInterface::loadSettings(const std::string& rom, const std::string& name,
134134
// Must force the resetting of the OSystem's random seed, which is set before we change
135135
// choose our random seed.
136136
Logger::Info << "Random seed is "
137-
<< theOSystem->settings().getInt("random_seed") << std::endl;
138-
theOSystem->resetRNGSeed();
137+
<< theOSystem.settings().getInt("random_seed") << std::endl;
138+
theOSystem.resetRNGSeed();
139139

140-
std::string currentDisplayFormat = theOSystem->console().getFormat();
141-
theOSystem->colourPalette().setPalette("standard", currentDisplayFormat);
140+
std::string currentDisplayFormat = theOSystem.console().getFormat();
141+
theOSystem.colourPalette().setPalette("standard", currentDisplayFormat);
142142
}
143143

144144
ALEInterface::ALEInterface() {
@@ -163,7 +163,7 @@ ALEInterface::~ALEInterface() {
163163
// load.
164164
bool ALEInterface::loadROM(std::string rom_file, std::string name) {
165165
assert(theOSystem.get());
166-
loadSettings(rom_file, name, theOSystem);
166+
loadSettings(rom_file, name, *theOSystem);
167167
romSettings.reset(buildRomRLWrapper(name));
168168
if (romSettings.get() == NULL) return false;
169169
environment.reset(new StellaEnvironment(theOSystem.get(), romSettings.get()));

atari_py/ale_interface/src/ale_interface.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
#include "common/Log.hpp"
4747

4848
#include <string>
49-
#include <memory>
49+
#include "common/scoped_ptr.hpp"
5050

5151
static const std::string Version = "0.6.0";
5252

@@ -180,24 +180,24 @@ class ALEInterface {
180180
//Kojoley ScreenExporter *createScreenExporter(const std::string &path) const;
181181

182182
public:
183-
std::unique_ptr<OSystem> theOSystem;
184-
std::unique_ptr<Settings> theSettings;
185-
std::unique_ptr<RomSettings> romSettings;
186-
std::unique_ptr<StellaEnvironment> environment;
183+
scoped_ptr<OSystem> theOSystem;
184+
scoped_ptr<Settings> theSettings;
185+
scoped_ptr<RomSettings> romSettings;
186+
scoped_ptr<StellaEnvironment> environment;
187187
int max_num_frames; // Maximum number of frames for each episode
188188

189189
public:
190190
// Display ALE welcome message
191191
static std::string welcomeMessage();
192192
static void disableBufferedIO();
193-
static void createOSystem(std::unique_ptr<OSystem> &theOSystem,
194-
std::unique_ptr<Settings> &theSettings);
193+
static void createOSystem(scoped_ptr<OSystem>& theOSystem,
194+
scoped_ptr<Settings>& theSettings);
195195
static void loadSettings(const std::string& romfile,
196196
const std::string& name,
197-
std::unique_ptr<OSystem> &theOSystem);
197+
OSystem& theOSystem);
198198

199199
private:
200-
static void checkForUnsupportedRom(std::unique_ptr<OSystem>& theOSystem);
200+
static void checkForUnsupportedRom(OSystem& theOSystem);
201201
};
202202

203203
#endif
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/* Copyright (C) 2019 by Nikita Kniazev
2+
*
3+
* Permission to use, copy, modify, and/or distribute this software for any
4+
* purpose with or without fee is hereby granted.
5+
*
6+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
7+
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
8+
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
9+
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
10+
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
11+
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
12+
* PERFORMANCE OF THIS SOFTWARE.
13+
*/
14+
15+
#ifndef NK_MT19937
16+
#define NK_MT19937
17+
18+
#include <stddef.h>
19+
20+
#if defined(_MSC_VER) && (_MSC_VER < 1600)
21+
typedef unsigned __int32 uint32_t;
22+
#else
23+
# include <stdint.h>
24+
# include <inttypes.h>
25+
#endif
26+
27+
class mt19937
28+
{
29+
public:
30+
typedef uint32_t result_type;
31+
32+
static const size_t word_size = 32;
33+
static const size_t state_size = 624;
34+
static const size_t shift_size = 397;
35+
static const size_t mask_bits = 31;
36+
static const uint32_t xor_mask = 0x9908b0df;
37+
static const size_t tempering_u = 11;
38+
static const uint32_t tempering_d = 0xffffffff;
39+
static const size_t tempering_s = 7;
40+
static const uint32_t tempering_b = 0x9d2c5680;
41+
static const size_t tempering_t = 15;
42+
static const uint32_t tempering_c = 0xefc60000;
43+
static const size_t tempering_l = 18;
44+
static const uint32_t initialization_multiplier = 1812433253;
45+
static const uint32_t default_seed = 5489u;
46+
47+
48+
explicit mt19937(uint32_t value = default_seed) { seed(value); }
49+
50+
void seed(uint32_t value = default_seed)
51+
{
52+
state[0] = value;
53+
for (uint32_t i = 1; i < state_size; ++i)
54+
state[i] = i + initialization_multiplier * (state[i - 1] ^ (state[i - 1] >> 30));
55+
index = 0;
56+
}
57+
58+
result_type operator()()
59+
{
60+
uint32_t next = (index + 1) % state_size;
61+
62+
uint32_t y = (0x80000000 & state[index]) | (0x7fffffff & state[next]);
63+
uint32_t r = state[index] = state[(index + shift_size) % state_size] ^ (y >> 1) ^ ((-(y & 1)) & xor_mask);
64+
r ^= r >> tempering_u & tempering_d;
65+
r ^= r << tempering_s & tempering_b;
66+
r ^= r << tempering_t & tempering_c;
67+
r ^= r >> tempering_l;
68+
69+
index = next;
70+
71+
return r;
72+
}
73+
74+
result_type (min)() const { return 0; }
75+
result_type (max)() const { return 0xffffffffUL; }
76+
77+
template <typename OStream>
78+
friend OStream& operator<<(OStream& os, mt19937 const& g)
79+
{
80+
typename OStream::fmtflags saved_flags = os.flags(os.dec);
81+
82+
for (uint32_t i = g.index; i < state_size; ++i)
83+
os << g.state[i] << ' ';
84+
for (uint32_t i = 0; i < g.index; ++i)
85+
os << g.state[i] << ' ';
86+
87+
os.flags(saved_flags);
88+
89+
return os;
90+
}
91+
92+
template <typename IStream>
93+
friend IStream& operator>>(IStream& is, mt19937& g)
94+
{
95+
typename IStream::fmtflags saved_flags = is.flags(is.dec | is.skipws);
96+
97+
uint32_t tmp[state_size];
98+
for (uint32_t i = 0; i < state_size; ++i)
99+
is >> tmp[i];
100+
101+
is.flags(saved_flags);
102+
103+
if (is.good()) {
104+
for (uint32_t i = 0; i < state_size; ++i)
105+
g.state[i] = tmp[i];
106+
g.index = 0;
107+
}
108+
109+
return is;
110+
}
111+
112+
private:
113+
uint32_t state[state_size];
114+
uint32_t index;
115+
};
116+
117+
#endif // NK_MT19937
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* Copyright (C) 2019 by Nikita Kniazev
2+
*
3+
* Permission to use, copy, modify, and/or distribute this software for any
4+
* purpose with or without fee is hereby granted.
5+
*
6+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
7+
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
8+
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
9+
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
10+
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
11+
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
12+
* PERFORMANCE OF THIS SOFTWARE.
13+
*/
14+
15+
#ifndef NK_SCOPED_PTR
16+
#define NK_SCOPED_PTR
17+
18+
namespace utils {
19+
20+
struct bool_conversion { int padding; int truth; };
21+
typedef int bool_conversion::* explicit_bool;
22+
23+
}
24+
25+
template<class T>
26+
class scoped_ptr {
27+
public:
28+
typedef T* pointer;
29+
typedef T element_type;
30+
31+
scoped_ptr() throw() : p_() {}
32+
explicit scoped_ptr(pointer p) throw() : p_(p) {}
33+
~scoped_ptr() { destroy(); }
34+
35+
element_type& operator*() const { return *get(); }
36+
pointer operator->() const throw() { return get(); }
37+
pointer get() const throw() { return p_; }
38+
operator bool() const throw() { return get() != pointer(); }
39+
operator utils::explicit_bool() const
40+
{
41+
return get() != pointer() ? &utils::bool_conversion::truth
42+
: utils::explicit_bool(0);
43+
}
44+
45+
pointer release() throw() { pointer p = p_; p_ = pointer(); return p; }
46+
void reset(pointer p = pointer()) throw() { destroy(); p_ = p; }
47+
void swap(scoped_ptr& u) throw()
48+
{
49+
pointer p = release();
50+
p_ = u.release();
51+
u.p_ = p;
52+
}
53+
54+
private:
55+
scoped_ptr(const scoped_ptr&);
56+
scoped_ptr& operator=(const scoped_ptr&);
57+
58+
void destroy() { delete get(); }
59+
60+
pointer p_;
61+
};
62+
63+
#endif // ALE_SCOPED_PTR

atari_py/ale_interface/src/controllers/ale_controller.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
#include "../emucore/OSystem.hxx"
2222
#include "../emucore/m6502/src/System.hxx"
2323
#include "../environment/stella_environment.hpp"
24-
25-
#include <memory>
24+
#include "../common/scoped_ptr.hpp"
2625

2726
class ALEController {
2827
public:
@@ -42,7 +41,7 @@ class ALEController {
4241

4342
protected:
4443
OSystem* m_osystem;
45-
std::unique_ptr<RomSettings> m_settings;
44+
scoped_ptr<RomSettings> m_settings;
4645
StellaEnvironment m_environment;
4746
};
4847

atari_py/ale_interface/src/emucore/Random.cxx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
#include "Serializer.hxx"
2222
#include "Deserializer.hxx"
2323

24-
// This uses C++11.
25-
#include <random>
24+
#include "../common/mt19937.hpp"
2625
#include <sstream>
2726

2827
// A static Random object for compatibility purposes. Don't use this.
@@ -31,7 +30,7 @@ Random Random::s_random;
3130
// Implementation of Random's random number generator wrapper.
3231
class Random::Impl {
3332

34-
typedef std::mt19937 randgen_t;
33+
typedef mt19937 randgen_t;
3534

3635
public:
3736

atari_py/ale_interface/src/environment/stella_environment.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ void StellaEnvironment::reset() {
7979

8080
// reset the rom (after emulating, in case the NOOPs led to reward)
8181
m_settings->reset();
82-
82+
83+
StellaEnvironmentWrapper wrapper(*this);
84+
8385
// Apply mode that was previously defined, then soft reset with this mode
84-
m_settings->setMode(m_state.getCurrentMode(), m_osystem->console().system(), getWrapper());
86+
m_settings->setMode(m_state.getCurrentMode(), m_osystem->console().system(), wrapper);
8587
softReset();
8688

8789
// Apply necessary actions specified by the rom itself
@@ -267,10 +269,6 @@ const ALEState& StellaEnvironment::getState() const {
267269
return m_state;
268270
}
269271

270-
std::unique_ptr<StellaEnvironmentWrapper> StellaEnvironment::getWrapper() {
271-
return std::unique_ptr<StellaEnvironmentWrapper>(new StellaEnvironmentWrapper(*this));
272-
}
273-
274272
void StellaEnvironment::processScreen() {
275273
if (m_colour_averaging) {
276274
// Perform phosphor averaging; the blender stores its result in the given screen

atari_py/ale_interface/src/environment/stella_environment.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
//Kojoley #include "../common/ScreenExporter.hpp"
3232

3333
#include <stack>
34-
#include <memory>
3534

3635
class StellaEnvironment {
3736
public:
@@ -96,9 +95,6 @@ class StellaEnvironment {
9695
int getFrameNumber() const { return m_state.getFrameNumber(); }
9796
int getEpisodeFrameNumber() const { return m_state.getEpisodeFrameNumber(); }
9897

99-
/** Returns a wrapper providing #include-free access to our methods. */
100-
std::unique_ptr<StellaEnvironmentWrapper> getWrapper();
101-
10298
private:
10399
/** This applies an action exactly one time step. Helper function to act(). */
104100
reward_t oneStepAct(Action player_a_action, Action player_b_action);

0 commit comments

Comments
 (0)