ReadyDev
... bootstrap.bat b2 b2 install ...
C: |-- Boost | |-- include | | |-- boost-1_88 | | | |-- lib | | |-- cmake
... cmake_minimum_required(VERSION 3.10.0) project(ReadyBOOST VERSION 0.1.0 LANGUAGES C CXX) ...
... set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD 11) ...
...
if(POLICY CMP0167)
cmake_policy(SET CMP0167 NEW)
endif()
......
if (NOT CMAKE_BUILD_TYPE)
if(MSVC)
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Build type" FORCE)
endif()
endif()
message(STATUS "Build type: " ${CMAKE_BUILD_TYPE})
...... list(APPEND CMAKE_PREFIX_PATH "C:/Boost/lib/cmake") ...
...
find_package(Boost REQUIRED COMPONENTS
program_options
)
...... add_subdirectory(src) ...
... add_subdirectory(program_options_base) add_subdirectory(program_options_short) ...
... project(program_options_base) ...
...
set(SRC_FILES
main.cpp
)
......
set(RES_FILES
res/config/options.cfg
)
if (NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
set(RES_DIR "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}")
message(STATUS "RES_DIR : " ${RES_DIR})
foreach(RES_FILE IN LISTS RES_FILES)
message(STATUS "RES_FILE : " ${RES_FILE})
add_custom_command(
OUTPUT "${RES_DIR}/${RES_FILE}"
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/${RES_FILE}" "${RES_DIR}/${RES_FILE}"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${RES_FILE}"
)
endforeach()
endif()
add_custom_target(data_${PROJECT_NAME} ALL DEPENDS ${RES_FILES})
......
add_executable(${PROJECT_NAME}
${SRC_FILES}
)
......
target_link_libraries(${PROJECT_NAME}
${Boost_LIBRARIES}
)
......
{
...
}
......
{
...
"cmake.debugConfig": {
"args": [
"--apples=10",
"--oranges=20"
]
}
...
}
...... @echo off ...
... @echo off set "root_dir=..\..\.." set "program_name=program_options_base" set "config_mode=Debug" set "build_dir=%root_dir%\build" set "program_dir=%root_dir%\build\src\%program_name%\%config_mode%" set "setup=%program_name%.exe" set cmake_exe="C:\Program Files\CMake\bin\cmake.exe" ...
... @echo off call cmd-config.bat pushd %program_dir% %setup% --help popd ...
... @echo off call cmd-config.bat pushd %program_dir% %setup% popd ...
... @echo off call cmd-config.bat pushd %program_dir% %setup% --oranges 5 --apples 10 popd ...
... @echo off call cmd-config.bat pushd %program_dir% %setup% --oranges 5 --apples 10 --help popd ...
... #include <boost/program_options.hpp> #include <iostream> ...
... namespace opt = boost::program_options; ...
...
int main(int argc, char **argv)
{
opt::options_description desc("All options");
...
return 0 ;
}
......
int main(int argc, char **argv)
{
...
desc.add_options()
("apples", opt::value<int>(), "how many apples do you have")
("oranges", opt::value<int>(), "how many oranges do you have")
("help", "produce help message");
...
}
......
int main(int argc, char **argv)
{
...
opt::variables_map vm;
opt::store(opt::parse_command_line(argc, argv, desc), vm);
opt::notify(vm);
...
}
......
int main(int argc, char **argv)
{
...
if (vm.count("help"))
{
std::cout << desc << "\n";
return 1;
}
...
}
......
int main(int argc, char **argv)
{
...
std::cout << "Fruits count: "
<< vm["apples"].as<int>() + vm["oranges"].as<int>()
<< std::endl;
...
}
...... setup.exe --help ... All options: --apples arg how many apples do you have --oranges arg how many oranges do you have --help produce help message ...
... setup.exe --oranges 5 --apples 10 ... ... Fruits count: 15 ...
... setup.exe --oranges 5 --apples 10 --help ... All options: --apples arg how many apples do you have --oranges arg how many oranges do you have --help produce help message ...
... #include <boost/program_options.hpp> #include <iostream> ...
... namespace opt = boost::program_options; ...
...
int main(int argc, char **argv)
{
opt::options_description desc("All options");
...
return 0 ;
}
......
int main(int argc, char **argv)
{
...
int oranges_var = 0;
desc.add_options()
("oranges,o", opt::value<int>(&oranges_var)->required(), "oranges that you have")
("name", opt::value<std::string>(), "your name")
("apples,a", opt::value<int>()->default_value(10), "apples that you have")
("help", "produce help message");
...
}
......
int main(int argc, char **argv)
{
...
opt::variables_map vm;
opt::store(opt::parse_command_line(argc, argv, desc), vm);
...
}
......
int main(int argc, char **argv)
{
...
if (vm.count("help"))
{
std::cout << desc << "\n";
return 1;
}
...
}
......
int main(int argc, char **argv)
{
...
const char *configFile = "res/config/options.cfg";
try
{
opt::store(opt::parse_config_file<char>(configFile, desc), vm);
}
catch (const opt::reading_file &e)
{
std::cout << "Error: " << e.what() << std::endl;
}
...
}
......
int main(int argc, char **argv)
{
...
try
{
opt::notify(vm);
}
catch (const opt::required_option &e)
{
std::cout << "Error: " << e.what() << std::endl;
return 2;
}
...
}
......
int main(int argc, char **argv)
{
...
if (vm.count("name"))
{
std::cout << "Hi," << vm["name"].as<std::string>() << "!\n";
}
...
}
......
int main(int argc, char **argv)
{
...
std::cout << "Fruits count: "
<< vm["apples"].as<int>() + vm["oranges"].as<int>()
<< std::endl;
...
}
...... # Fichier de configuration des options oranges=20 name=ReadyDEV ...
... setup.exe --help ... All options: -o [ --oranges ] arg oranges that you have --name arg your name -a [ --apples ] arg (=10) apples that you have --help produce help message ...
... setup.exe ... Hi,ReadyDEV! Fruits count: 30 ...
... setup.exe --oranges 5 --apples 10 --name ReadyTEAM ... Hi,ReadyTEAM! Fruits count: 15 ...
... setup.exe --oranges 5 --apples 10 --name ReadyTEAM --help ... All options: -o [ --oranges ] arg oranges that you have --name arg your name -a [ --apples ] arg (=10) apples that you have --help produce help message ...
... #include <boost/any.hpp> #include <iostream> #include <vector> #include <string> ...
...
int main(int argc, char **argv)
{
std::vector<boost::any> some_values;
...
return 0 ;
}
......
int main(int argc, char **argv)
{
...
some_values.push_back(10);
const char *c_str = "Hello there!";
some_values.push_back(c_str);
some_values.push_back(std::string("Wow!"));
...
}
......
int main(int argc, char **argv)
{
...
std::string &s = boost::any_cast<std::string &>(some_values.back());
...
}
......
int main(int argc, char **argv)
{
...
s += " That is great!\n";
std::cout << s;
std::cout << boost::any_cast<std::string &>(some_values.back());
...
}
...... setup.exe ... Wow! That is great! Wow! That is great! ... Le pointeur de référence fonctionne correctement. ...
... #include <boost/any.hpp> #include <vector> #include <string> #include <typeinfo> #include <algorithm> #include <functional> #include <iostream> ...
... using cell_t = boost::any; using db_row_t = std::vector<cell_t>; ...
...
db_row_t get_row(const char *query)
{
std::cout << "Executing query: " << query << std::endl;
db_row_t row;
row.push_back(10);
row.push_back(10.1f);
row.push_back(std::string("hello again"));
return row;
}
......
struct db_sum : public std::function<void(cell_t)>
{
private:
double &sum_;
public:
explicit db_sum(double &sum)
: sum_(sum)
{
}
void operator()(const cell_t &value)
{
const std::type_info &ti = value.type();
if (ti == typeid(int))
{
sum_ += boost::any_cast<int>(value);
}
else if (ti == typeid(float))
{
sum_ += boost::any_cast<float>(value);
}
}
};
......
int main(int argc, char **argv)
{
db_row_t row = get_row("Query: Give me some row, please.");
...
return 0 ;
}
......
int main(int argc, char **argv)
{
...
double res = 0.0;
std::for_each(row.begin(), row.end(), db_sum(res));
std::cout << "Sum of arithmetic types in database row is: " << res << std::endl;
...
}
...... setup.exe ... Executing query: Query: Give me some row, please. Sum of arithmetic types in database row is: 20.1 ...
... #include <boost/variant.hpp> #include <vector> #include <string> #include <iostream> ...
...
int main(int argc, char **argv)
{
typedef boost::variant<int, const char *, std::string> my_var_t;
...
return 0 ;
}
......
int main(int argc, char **argv)
{
...
std::vector<my_var_t> some_values;
...
}
......
int main(int argc, char **argv)
{
...
some_values.push_back(10);
some_values.push_back("Hello there!");
some_values.push_back(std::string("Wow!"));
...
}
......
int main(int argc, char **argv)
{
...
s += " That is great!\n";
std::cout << s;
std::cout << boost::get<std::string>(some_values.back());
...
}
...... setup.exe ... Wow! That is great! Wow! That is great! ... Le pointeur de référence fonctionne correctement. ...
... #include <boost/variant.hpp> #include <iostream> #include <vector> #include <string> ...
... using cell_t = boost::variant<int, float, std::string>; using db_row_t = std::vector<cell_t>; ...
...
db_row_t get_row(const char *query)
{
std::cout << "Executing query: " << query << std::endl;
db_row_t row;
row.push_back(10);
row.push_back(10.1f);
row.push_back("hello again");
return row;
}
......
struct db_sum_visitor : public boost::static_visitor<double>
{
double operator()(int value) const
{
return value;
}
double operator()(float value) const
{
return value;
}
double operator()(const std::string &value) const
{
return 0.0;
}
};
......
int main(int argc, char **argv)
{
db_row_t row = get_row("Query: Give me some row, please.");
...
return 0 ;
}
......
int main(int argc, char **argv)
{
...
double res = 0.0;
for (db_row_t::const_iterator it = row.begin(), end = row.end(); it != end; ++it)
{
res += boost::apply_visitor(db_sum_visitor(), *it);
}
std::cout << "Sum of arithmetic types in database row is: " << res << std::endl;
...
}
...... setup.exe ... Executing query: Query: Give me some row, please. Sum of arithmetic types in database row is: 20.1 ...