Skip to content

Commit

Permalink
+read::FileVisitorImplementation
Browse files Browse the repository at this point in the history
  • Loading branch information
asherikov committed Nov 29, 2024
1 parent 0ad1a54 commit 9f25e58
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ install-deps:


format:
${FIND_ARILES_SOURCES} | grep -v "better_enum.h" | xargs clang-format15 -verbose -i
${FIND_ARILES_SOURCES} | grep -v "better_enum.h" | xargs clang-format-15 -verbose -i

cppcheck:
# --inconclusive
Expand Down
5 changes: 3 additions & 2 deletions extra_visitors/graphviz/src/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ namespace ariles2
{
namespace impl
{
class ARILES2_VISIBILITY_ATTRIBUTE Visitor : public serialization::NodeStackBase<NodeWrapper>, public write::FileVisitorImplementation
class ARILES2_VISIBILITY_ATTRIBUTE Visitor : public serialization::NodeStackBase<NodeWrapper>,
public write::FileVisitorImplementation
{
public:
std::set<std::string> all_ids_;
Expand All @@ -81,7 +82,7 @@ namespace ariles2


public:
template<class... t_Args>
template <class... t_Args>
explicit Visitor(t_Args &&...args) : FileVisitorImplementation(std::forward<t_Args>(args)...)
{
}
Expand Down
24 changes: 13 additions & 11 deletions extra_visitors/msgpack/src/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ namespace ariles2
{
namespace impl
{
class ARILES2_VISIBILITY_ATTRIBUTE Reader : public serialization::NodeStackBase<NodeWrapper>
class ARILES2_VISIBILITY_ATTRIBUTE Reader : public serialization::NodeStackBase<NodeWrapper>,
public read::FileVisitorImplementation
{
public:
std::string buffer_;
Expand All @@ -42,16 +43,21 @@ namespace ariles2


public:
template <class... t_Args>
explicit Reader(t_Args &&...args) : FileVisitorImplementation(std::forward<t_Args>(args)...)
{
initialize();
}


/**
* @brief open configuration file
*
* @param[in] input_stream
*/
void initialize(std::istream &input_stream)
void initialize()
{
CPPUT_TRACE_FUNCTION;
std::stringstream str_stream;
str_stream << input_stream.rdbuf();
str_stream << input_stream_->rdbuf();
buffer_ = str_stream.str();


Expand Down Expand Up @@ -109,17 +115,13 @@ namespace ariles2
{
Reader::Reader(const std::string &file_name)
{
std::ifstream config_ifs;
read::Visitor::openFile(config_ifs, file_name);
makeImplPtr();
impl_->initialize(config_ifs);
makeImplPtr(file_name);
}


Reader::Reader(std::istream &input_stream)
{
makeImplPtr();
impl_->initialize(input_stream);
makeImplPtr(input_stream);
}


Expand Down
24 changes: 13 additions & 11 deletions extra_visitors/msgpack/src/reader_compact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ namespace ariles2
{
namespace impl
{
class ARILES2_VISIBILITY_ATTRIBUTE Reader : public serialization::NodeStackBase<NodeWrapper>
class ARILES2_VISIBILITY_ATTRIBUTE Reader : public serialization::NodeStackBase<NodeWrapper>,
public read::FileVisitorImplementation
{
public:
std::string buffer_;
Expand All @@ -42,15 +43,20 @@ namespace ariles2


public:
template <class... t_Args>
explicit Reader(t_Args &&...args) : FileVisitorImplementation(std::forward<t_Args>(args)...)
{
initialize();
}


/**
* @brief open configuration file
*
* @param[in] input_stream
*/
void initialize(std::istream &input_stream)
void initialize()
{
std::stringstream str_stream;
str_stream << input_stream.rdbuf();
str_stream << input_stream_->rdbuf();
buffer_ = str_stream.str();

try
Expand Down Expand Up @@ -97,17 +103,13 @@ namespace ariles2
{
Reader::Reader(const std::string &file_name)
{
std::ifstream config_ifs;
read::Visitor::openFile(config_ifs, file_name);
makeImplPtr();
impl_->initialize(config_ifs);
makeImplPtr(file_name);
}


Reader::Reader(std::istream &input_stream)
{
makeImplPtr();
impl_->initialize(input_stream);
makeImplPtr(input_stream);
}


Expand Down
2 changes: 1 addition & 1 deletion extra_visitors/msgpack/src/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace ariles2
void operator=(const Writer &) = delete;


template<class... t_Args>
template <class... t_Args>
explicit Writer(t_Args &&...args) : FileVisitorImplementation(std::forward<t_Args>(args)...)
{
packer_ = std::make_shared<::msgpack::packer<std::ostream>>(*output_stream_);
Expand Down
2 changes: 1 addition & 1 deletion extra_visitors/msgpack/src/writer_compact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace ariles2
void operator=(const Writer &) = delete;


template<class... t_Args>
template <class... t_Args>
explicit Writer(t_Args &&...args) : FileVisitorImplementation(std::forward<t_Args>(args)...)
{
packer_ = std::make_shared<::msgpack::packer<std::ostream>>(*output_stream_);
Expand Down
5 changes: 3 additions & 2 deletions extra_visitors/octave/src/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ namespace ariles2
{
namespace impl
{
class ARILES2_VISIBILITY_ATTRIBUTE Writer : public serialization::NodeStackBase<NodeWrapper>, public write::FileVisitorImplementation
class ARILES2_VISIBILITY_ATTRIBUTE Writer : public serialization::NodeStackBase<NodeWrapper>,
public write::FileVisitorImplementation
{
public:
const std::string separator_ = ".";
Expand All @@ -50,7 +51,7 @@ namespace ariles2


public:
template<class... t_Args>
template <class... t_Args>
explicit Writer(t_Args &&...args) : FileVisitorImplementation(std::forward<t_Args>(args)...)
{
initEmitter();
Expand Down
5 changes: 3 additions & 2 deletions extra_visitors/pugixml/src/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ namespace ariles2
{
namespace impl
{
class ARILES2_VISIBILITY_ATTRIBUTE Writer : public serialization::NodeStackBase<NodeWrapper>, public write::FileVisitorImplementation
class ARILES2_VISIBILITY_ATTRIBUTE Writer : public serialization::NodeStackBase<NodeWrapper>,
public write::FileVisitorImplementation
{
public:
pugi::xml_document document_;


public:
template<class... t_Args>
template <class... t_Args>
explicit Writer(t_Args &&...args) : FileVisitorImplementation(std::forward<t_Args>(args)...)
{
node_stack_.emplace_back(document_);
Expand Down
27 changes: 17 additions & 10 deletions extra_visitors/rapidjson/src/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,26 @@ namespace ariles2
{
namespace impl
{
class ARILES2_VISIBILITY_ATTRIBUTE Reader : public ariles2::ns_rapidjson::ImplBase<const ::rapidjson::Value>
class ARILES2_VISIBILITY_ATTRIBUTE Reader
: public ariles2::ns_rapidjson::ImplBase<const ::rapidjson::Value>,
public read::FileVisitorImplementation
{
public:
std::vector<::rapidjson::Value::ConstMemberIterator> iterator_stack_;

public:
void initialize(std::istream &input_stream)
Reader() = default;

template <class... t_Args>
explicit Reader(t_Args &&...args) : FileVisitorImplementation(std::forward<t_Args>(args)...)
{
initialize();
}


void initialize()
{
ariles2::ns_rapidjson::IStreamWrapper isw(input_stream);
ariles2::ns_rapidjson::IStreamWrapper isw(*input_stream_);
document_.ParseStream(isw);
CPPUT_ASSERT(not document_.HasParseError(), "Parsing failed");
}
Expand All @@ -44,23 +55,19 @@ namespace ariles2
{
Reader::Reader(const std::string &file_name)
{
std::ifstream config_ifs;
read::Visitor::openFile(config_ifs, file_name);
impl_ = ImplPtr(new impl::Reader());
impl_->initialize(config_ifs);
makeImplPtr(file_name);
}


Reader::Reader(std::istream &input_stream)
{
impl_ = ImplPtr(new impl::Reader());
impl_->initialize(input_stream);
makeImplPtr(input_stream);
}


void Reader::constructFromString(const char *input_string)
{
impl_ = ImplPtr(new impl::Reader());
makeImplPtr();
impl_->document_.Parse(input_string);
}

Expand Down
5 changes: 3 additions & 2 deletions extra_visitors/rapidjson/src/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ namespace ariles2
{
namespace impl
{
class ARILES2_VISIBILITY_ATTRIBUTE Writer : public ariles2::ns_rapidjson::ImplBase<::rapidjson::Value>, public write::FileVisitorImplementation
class ARILES2_VISIBILITY_ATTRIBUTE Writer : public ariles2::ns_rapidjson::ImplBase<::rapidjson::Value>,
public write::FileVisitorImplementation
{
public:
template<class... t_Args>
template <class... t_Args>
explicit Writer(t_Args &&...args) : FileVisitorImplementation(std::forward<t_Args>(args)...)
{
document_.SetObject();
Expand Down
2 changes: 1 addition & 1 deletion extra_visitors/yaml_cpp/src/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace ariles2
}

public:
template<class... t_Args>
template <class... t_Args>
explicit Writer(t_Args &&...args) : FileVisitorImplementation(std::forward<t_Args>(args)...)
{
initEmitter();
Expand Down
58 changes: 40 additions & 18 deletions include/ariles2/visitors/read.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,6 @@ namespace ariles2
}


/**
* @brief open configuration file
*
* @param[out] config_ifs
* @param[in] file_name
*/
static void openFile(std::ifstream &config_ifs, const std::string &file_name)
{
config_ifs.open(file_name.c_str());
if (!config_ifs.good())
{
std::string file_name_default = file_name;
config_ifs.open(file_name_default.c_str());
}
CPPUT_PERSISTENT_ASSERT(config_ifs.good(), "Could not open configuration file: ", file_name.c_str());
}


virtual bool startRoot(const std::string &name)
{
CPPUT_TRACE_FUNCTION;
Expand Down Expand Up @@ -504,6 +486,46 @@ namespace ariles2
};


class FileVisitorImplementation
{
protected:
std::ifstream config_ifs_;

std::istream *input_stream_;

protected:
FileVisitorImplementation() = default;

explicit FileVisitorImplementation(const std::string &file_name)
{
openFile(file_name);
input_stream_ = &config_ifs_;
}

explicit FileVisitorImplementation(std::istream &input_stream)
{
input_stream_ = &input_stream;
}


/**
* @brief open configuration file
*
* @param[in] file_name
*/
void openFile(const std::string &file_name)
{
config_ifs_.open(file_name.c_str());
if (!config_ifs_.good())
{
std::string file_name_default = file_name;
config_ifs_.open(file_name_default.c_str());
}
CPPUT_PERSISTENT_ASSERT(config_ifs_.good(), "Could not open configuration file: ", file_name.c_str());
}
};


using Base = entry::Base<read::Visitor>;


Expand Down
1 change: 0 additions & 1 deletion include/ariles2/visitors/write.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ namespace ariles2
/**
* @brief open configuration file
*
* @param[out] config_ofs
* @param[in] file_name
*/
void openFile(const std::string &file_name)
Expand Down

0 comments on commit 9f25e58

Please sign in to comment.