utility: boost integration

Added boost components: filesystem and system to the project
environment varibable BOOST_155_DIR targeting the boost_1_55_0 folder is needed

review id = 14

fortune cookie message = when you speak honestly and openly, others truly listen to you
This commit is contained in:
Andreas Stallinger
2014-05-13 17:24:53 +02:00
parent 95e51b1780
commit ac55261af6
19 changed files with 310 additions and 8 deletions
+13 -5
View File
@@ -24,7 +24,7 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
if (UNIX)
add_definitions(-std=c++11)
add_definitions(-std=c++11 -Wno-unknown-warning-option)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin/${CMAKE_BUILD_TYPE})
else ()
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
@@ -43,6 +43,15 @@ find_package(CLANG REQUIRED PATHS "${CMAKE_SOURCE_DIR}/cmake")
add_definitions(${CLANG_DEFINITIONS})
# Boost ------------------------------------------------------------------------
set(BOOST_ROOT $ENV{BOOST_155_DIR})
set(Boost_USE_MULTITHREAD ON)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_STATIC_RUNTIME OFF)
set(BOOST_LIBRARYDIR $ENV{BOOST_155_DIR})
find_package( Boost 1.55 COMPONENTS system filesystem REQUIRED )
# Lib --------------------------------------------------------------------------
@@ -71,12 +80,12 @@ set_property(
"${CMAKE_SOURCE_DIR}/src/external"
${LLVM_INCLUDE_DIRS}
${CLANG_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
link_directories(${LLVM_LIBRARY_DIRS} ${CLANG_LIBRARY_DIRS})
target_link_libraries(${LIB_PROJECT_NAME} ${CLANG_LIBRARIES} ${LLVM_AVAILABLE_LIBS})
link_directories(${LLVM_LIBRARY_DIRS} ${CLANG_LIBRARY_DIRS} ${Boost_LIBRARY_DIRS})
target_link_libraries(${LIB_PROJECT_NAME} ${CLANG_LIBRARIES} ${LLVM_AVAILABLE_LIBS} ${Boost_LIBRARIES})
# App --------------------------------------------------------------------------
@@ -118,7 +127,6 @@ configure_file(
set(CMAKE_AUTOMOC OFF)
# CxxTest ----------------------------------------------------------------------
add_subdirectory(src/test)
+2
View File
@@ -8,12 +8,14 @@
* CxxTest 4.3
* Valgrind 3.9.0(linux, macOS)
* Clang & LLVM (installation guide http://clang.llvm.org/docs/LibASTMatchersTutorial.html)
* Boost 1.55 ()
##### Environment Variables
* QT_DIR - ...\Qt\Qt5.2.1\5.2.1\<IDE>\
* CXX_TEST_DIR - .../cxxtest-4.3/
* CLANG_DIR - .../clang-llvm/
* BOOST_155_DIR - .../boost_1_55_0
For Win32:
* VLD_DIR - .../Visual Leak Detector
+21
View File
@@ -0,0 +1,21 @@
#ifndef _PLAYER_
#define _PLAYER_
#include <string>
#include "field.h"
class Player {
public:
Player( Field::Token token, const std::string& name )
: token_( token )
, name_( name ) {
}
virtual Field::Move Turn( const Field& field ) const = 0;
const Field::Token token_;
const std::string name_;
};
#endif // _PLAYER_
+33
View File
@@ -0,0 +1,33 @@
class Player {
public:
void Do() {}
};
class Base {};
class Game : public Base {
public:
Game() {
Init();
}
void Init() {}
void Run() {
player.Do();
}
private:
Player player;
};
Game* game;
int main() {
game = new Game();
game->Run();
delete game;
return 0;
}
View File
+11
View File
@@ -0,0 +1,11 @@
#include "tictactoe.h"
int main() {
TicTacToe tictactoe;
while ( tictactoe.Start() ) {
tictactoe.Run();
}
return 0;
}
+93
View File
@@ -0,0 +1,93 @@
#ifndef _TIC_TAC_TOE_
#define _TIC_TAC_TOE_
#include "computer.h"
#include "field.h"
#include "human.h"
#include "input.h"
#include "player.h"
class TicTacToe {
public:
TicTacToe() {
players_[0] = NULL;
players_[1] = NULL;
}
~TicTacToe() {
Reset();
}
bool Start() {
Reset();
std::cout << "Tic Tac Toe\n\n[1] Human\n[2] Computer\n[3] Quit\n\n";
players_[0] = SelectPlayer( Field::PlayerA, "PlayerA" );
if ( !players_[0] ) {
return false;
}
players_[1] = SelectPlayer( Field::PlayerB, "PlayerB" );
if ( !players_[1] ) {
return false;
}
std::cout << '\n';
return true;
}
void Run() {
field_.Show();
int playerIndex = 0;
for ( int i = 0; i < 9; i++ ) {
Player& player = *players_[playerIndex];
field_.MakeMove( player.Turn( field_ ), player.token_ );
field_.Show();
if ( field_.SameInRow( player.token_, 3 ) ) {
std::cout << player.name_ << " won!\n\n";
return;
}
playerIndex = ( playerIndex + 1 ) % 2;
}
std::cout << "Game ends in draw!\n\n";
}
private:
void Reset() {
field_.Clear();
for ( int i = 0; i < 2; i++ ) {
if ( players_[i] ) {
delete players_[i];
players_[i] = NULL;
}
}
}
Player* SelectPlayer( Field::Token token, const std::string& name ) const {
int selection;
while ( true ) {
std::cout << "Choose " << name << ": ";
input::number( &selection );
switch ( selection ) {
case 1 : return new HumanPlayer( token, name );
case 2 : return new ArtificialPlayer( token, name );
case 3 : return NULL;
default : std::cout << "Wrong input!\n";
}
}
}
Player* players_[2];
Field field_;
};
#endif // _TIC_TAC_TOE_
+5
View File
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<config>
<SourcePath>the/awesome/path/to/the/code</SourcePath>
</config>
+1 -1
View File
@@ -6,4 +6,4 @@ MY_PATH=`dirname "$0"`
cd $MY_PATH/..
# Build and run target
make -C build/Debug "$1" && cd bin && valgrind --tool=memcheck --leak-check=yes --log-file=../build/Debug/"$1".leaklog Debug/"$1"
ninja -C build/Debug "$1" && cd bin && valgrind --tool=memcheck --leak-check=yes --log-file=../build/Debug/"$1".leaklog Debug/"$1"
+1 -1
View File
@@ -6,4 +6,4 @@ MY_PATH=`dirname "$0"`
cd $MY_PATH/..
# Build and run target
make -C build/Release "$1" && cd bin && valgrind --tool=memcheck --leak-check=yes --log-file=../build/Release/"$1".leaklog Release/"$1"
ninja -C build/Release "$1" && cd bin && valgrind --tool=memcheck --leak-check=yes --log-file=../build/Release/"$1".leaklog Release/"$1"
+3 -1
View File
@@ -98,7 +98,7 @@ add_files(
utility/logging/LogManagerImplementation.cpp
utility/logging/LogManagerImplementation.h
utility/logging/LogMessage.h
utility/math/Color.h
utility/math/Vector2.h
utility/math/Vector4.h
@@ -112,6 +112,8 @@ add_files(
utility/ConfigManager.cpp
utility/ConfigManager.h
utility/FileSystem.cpp
utility/FileSystem.h
utility/Vector.h
Application.cpp
+43
View File
@@ -0,0 +1,43 @@
#include "utility/FileSystem.h"
#include "boost/filesystem.hpp"
#include "utility/logging/logging.h"
std::vector<std::string> FileSystem::getSourceFilesFromDirectory(
const std::string& path, const std::vector<std::string>& extensions
)
{
std::vector<std::string> files;
if (boost::filesystem::is_directory(path))
{
boost::filesystem::recursive_directory_iterator it(path);
boost::filesystem::recursive_directory_iterator endit;
while(it != endit)
{
if (boost::filesystem::is_regular_file(*it) && isValidExtension(it->path().string(), extensions))
{
files.push_back(it->path().string());
}
++it;
}
}
return files;
}
bool FileSystem::isValidExtension(const std::string& filepath, const std::vector<std::string>& extensions)
{
boost::filesystem::path path(filepath);
for(std::string extension : extensions)
{
if(path.extension() == extension)
{
return true;
}
}
return false;
}
bool FileSystem::exists(const std::string& path)
{
return boost::filesystem::exists(boost::filesystem::path(path));
}
+18
View File
@@ -0,0 +1,18 @@
#ifndef FILE_SYSTEM_H
#define FILE_SYSTEM_H
#include <string>
#include <vector>
class FileSystem
{
public:
static std::vector<std::string> getSourceFilesFromDirectory(
const std::string& path, const std::vector<std::string>& extensions
);
static bool exists(const std::string& path);
private:
static bool isValidExtension(const std::string& filepath, const std::vector<std::string>& extensions);
};
#endif // FILE_SYSTEM_H
+1
View File
@@ -3,6 +3,7 @@ add_files(
ConfigManagerTestSuite.h
CxxParserTestSuite.h
FileSystemTestSuite.h
LogManagerTestSuite.h
MessageQueueTestSuite.h
TestSuiteFixture.cpp
+65
View File
@@ -0,0 +1,65 @@
#include "cxxtest/TestSuite.h"
#include <algorithm>
#include <string>
#include <vector>
#include "utility/FileSystem.h"
class FileSystemTestSuite : public CxxTest::TestSuite
{
public:
void test_find_cpp_files()
{
std::vector<std::string> extensions;
extensions.push_back(".cpp");
std::vector<std::string> cppFiles = FileSystem::getSourceFilesFromDirectory("data/src",extensions);
TS_ASSERT_EQUALS(cppFiles.size(), 2)
TS_ASSERT(isInVector(cppFiles, "data/src/main.cpp"))
TS_ASSERT(isInVector(cppFiles, "data/src/Settings/sample.cpp"))
}
void test_find_h_files()
{
std::vector<std::string> extensions;
extensions.push_back(".h");
std::vector<std::string> headerFiles = FileSystem::getSourceFilesFromDirectory("data/src",extensions);
TS_ASSERT_EQUALS(headerFiles.size(), 2)
TS_ASSERT(isInVector(headerFiles, "data/src/tictactoe.h"))
TS_ASSERT(isInVector(headerFiles, "data/src/Settings/player.h"))
}
void test_find_all_source_files()
{
std::vector<std::string> extensions;
extensions.push_back(".h");
extensions.push_back(".hpp");
extensions.push_back(".cpp");
std::vector<std::string> sourceFiles = FileSystem::getSourceFilesFromDirectory("data/src",extensions);
TS_ASSERT_EQUALS(sourceFiles.size(), 5)
}
void test_filesystem_finds_existing_files()
{
TS_ASSERT(FileSystem::exists("data/src"))
TS_ASSERT(FileSystem::exists("data/src/tictactoe.h"));
}
void test_filesystem_does_not_find_non_existing_files()
{
TS_ASSERT(!FileSystem::exists("data/foo"));
TS_ASSERT(!FileSystem::exists("data/src/blabla.h"));
}
private:
bool isInVector(const std::vector<std::string>& files, const std::string filename)
{
return std::end(files) != std::find(std::begin(files), std::end(files), filename);
}
};