push ex01
This commit is contained in:
parent
f085080c7d
commit
e3ff4a9bc9
|
|
@ -2,23 +2,12 @@
|
|||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
std::cout << std::endl;
|
||||
|
||||
if (argc < 2)
|
||||
std::cout << "Usage: ./convert <value_to_cast1> <value_to_cast2> ..." << std::endl;
|
||||
|
||||
int i = 1;
|
||||
|
||||
while (argv[i])
|
||||
{
|
||||
std::cout << "############## OUTPUT " << i << " ##############" << std::endl;
|
||||
std::cout << "user input:\t\"" << argv[i] << "\"" << std::endl;
|
||||
|
||||
if (argv[i][0])
|
||||
ScalarConverter::convert(argv[i++]);
|
||||
else
|
||||
std::cout << "Argument " << i++ << " is empty." << std::endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
if (argc != 2)
|
||||
{
|
||||
std::cerr << "Usage: ./convert <literal>" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
ScalarConverter::convert(argv[1]);
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@ CXXFLAGS = -Wall -Wextra -Werror -std=c++98
|
|||
OBJDIR = obj
|
||||
SOURCES = Main.cpp ScalarConverter.cpp
|
||||
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
|
||||
NAME = Bureaucrat
|
||||
NAME = ScalarConverter
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
|
|
|
|||
|
|
@ -205,14 +205,14 @@ void ScalarConverter::toOther(const std::string &input)
|
|||
|
||||
void ScalarConverter::convert(const std::string &input)
|
||||
{
|
||||
if (isChar(input))
|
||||
if (isInt(input))
|
||||
toInt(input);
|
||||
else if (isChar(input))
|
||||
toChar(input);
|
||||
else if (isInt(input))
|
||||
toInt(input); ////////////////
|
||||
else if (isFloat(input))
|
||||
toFloat(input); //////////////
|
||||
toFloat(input);
|
||||
else if (isDouble(input))
|
||||
toDouble(input); /////////////
|
||||
toDouble(input);
|
||||
else if (isOther(input))
|
||||
toOther(input);
|
||||
else
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef DATA_HPP
|
||||
#define DATA_HPP
|
||||
|
||||
typedef struct Data
|
||||
{
|
||||
int value;
|
||||
} Data;
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
|
||||
#include "Serializer.hpp"
|
||||
#include "Data.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
Data data;
|
||||
Data* ptr;
|
||||
uintptr_t raw;
|
||||
Data* result;
|
||||
|
||||
// Initialiser Data
|
||||
data.value = 42;
|
||||
|
||||
ptr = &data;
|
||||
|
||||
std::cout << "Original pointer: " << ptr << std::endl;
|
||||
std::cout << "Value: " << ptr->value << std::endl;
|
||||
std::cout << std::endl;
|
||||
|
||||
// Serialize
|
||||
raw = Serializer::serialize(ptr);
|
||||
std::cout << "Serialized: " << raw << std::endl;
|
||||
std::cout << std::endl;
|
||||
|
||||
// Deserialize
|
||||
result = Serializer::deserialize(raw);
|
||||
std::cout << "Deserialized pointer: " << result << std::endl;
|
||||
std::cout << "Value: " << result->value << std::endl;
|
||||
std::cout << std::endl;
|
||||
|
||||
// Verification
|
||||
if (ptr == result)
|
||||
std::cout << "✓ Success: pointers are equal" << std::endl;
|
||||
else
|
||||
std::cout << "✗ Error: pointers differ" << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
CXX = c++
|
||||
CXXFLAGS = -Wall -Wextra -Werror -std=c++98
|
||||
OBJDIR = obj
|
||||
SOURCES = Serializer.cpp Main.cpp
|
||||
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
|
||||
NAME = Serializer
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(OBJDIR):
|
||||
@echo "📁 Creating obj directory..."
|
||||
@mkdir -p $(OBJDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.cpp | $(OBJDIR)
|
||||
@echo "🧠 Compiling $< ..."
|
||||
@$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||
@echo "✅ $@ ready!"
|
||||
|
||||
$(NAME): $(OBJECTS)
|
||||
@echo "🔗 Linking $(NAME) ..."
|
||||
@$(CXX) $(CXXFLAGS) $(OBJECTS) -o $(NAME)
|
||||
@echo "🎉 $(NAME) is ready!"
|
||||
|
||||
clean:
|
||||
@echo "🧹 Cleaning object files..."
|
||||
@rm -rf $(OBJDIR)
|
||||
@echo "✨ Objects cleaned!"
|
||||
|
||||
fclean: clean
|
||||
@echo "🗑️ Removing $(NAME)..."
|
||||
@rm -f $(NAME)
|
||||
@echo "💀 Full clean complete!"
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
#include "Serializer.hpp"
|
||||
|
||||
|
||||
Serializer::Serializer()
|
||||
{
|
||||
}
|
||||
|
||||
Serializer::Serializer(const Serializer &src)
|
||||
{
|
||||
(void)src;
|
||||
}
|
||||
|
||||
Serializer& Serializer::operator=(const Serializer &src)
|
||||
{
|
||||
(void)src;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Serializer::~Serializer()
|
||||
{
|
||||
}
|
||||
|
||||
uintptr_t Serializer::serialize(Data* ptr)
|
||||
{
|
||||
return (reinterpret_cast<uintptr_t>(ptr));
|
||||
}
|
||||
|
||||
Data* Serializer::deserialize(uintptr_t raw)
|
||||
{
|
||||
return (reinterpret_cast<Data*>(raw));
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef SERIALIZER_HPP
|
||||
#define SERIALIZER_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
#include "Data.hpp"
|
||||
|
||||
class Serializer
|
||||
{
|
||||
private:
|
||||
Serializer(void);
|
||||
Serializer(Serializer const &src);
|
||||
~Serializer(void);
|
||||
Serializer& operator=(Serializer const &rhs);
|
||||
|
||||
public:
|
||||
static uintptr_t serialize(Data* ptr);
|
||||
static Data* deserialize(uintptr_t raw);
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
Loading…
Reference in New Issue