diff --git a/Makefile b/Makefile index 52bacc6..0eca841 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,7 @@ COMMON = \ common.cc \ c-camera.cc \ c-filewatcher.cc \ + c-project.cc \ c-utilities.cc \ c-undo.cc \ \ diff --git a/c-project.cc b/c-project.cc new file mode 100644 index 0000000..82e219d --- /dev/null +++ b/c-project.cc @@ -0,0 +1,57 @@ +#include "externals.hh" +#include "c-project.hh" + + +/*= A_ProjectPathListener ====================================================*/ + +A_ProjectPathListener::~A_ProjectPathListener( ) {} + + +/*= T_Project ================================================================*/ + +void T_Project::setBasePath( + T_String const& path ) noexcept +{ + if ( !path || path[ path.length( ) - 1 ] != '/' ) { + setBasePathInternal( path ); + } else { + setBasePathInternal( path.substr( 0 , path.length( ) - 1 ) ); + } +} + +void T_Project::setBasePathInternal( + T_String path ) noexcept +{ + if ( ( path && basePath_ == path ) + || ( !path && basePath_ == "." ) ) { + return; + } + if ( !path ) { + basePath_ = T_String::Pooled( "." ); + } else { + basePath_ = std::move( path ); + } + + const auto n{ listeners_.size( ) }; + for ( auto i = 0u ; i < n ; i ++ ) { + listeners_[ i ]->projectPathChanged( ); + } +} + +/*----------------------------------------------------------------------------*/ + +T_String T_Project::pathOf( + T_String const& file ) const noexcept +{ + T_StringBuilder sb; + sb << basePath_ << '/' << file; + return std::move( sb ); +} + +T_String T_Project::pathOf( + char const* file ) const noexcept +{ + T_StringBuilder sb; + sb << basePath_ << '/' << file; + return std::move( sb ); +} diff --git a/c-project.hh b/c-project.hh new file mode 100644 index 0000000..2207b3e --- /dev/null +++ b/c-project.hh @@ -0,0 +1,38 @@ +#pragma once +#ifndef REAL_BUILD +# include "externals.hh" +#endif + +#include + + +class A_ProjectPathListener +{ + public: + virtual ~A_ProjectPathListener( ) = 0; + virtual void projectPathChanged( ) noexcept = 0; +}; + +struct T_Project +{ + void setBasePath( T_String const& path ) noexcept; + + T_String const& basePath( ) const noexcept + { return basePath_; } + + T_String pathOf( T_String const& file ) const noexcept; + T_String pathOf( char const* file ) const noexcept; + + void addListener( A_ProjectPathListener* listener ) noexcept + { listeners_.add( listener ); } + void removeListener( A_ProjectPathListener* listener ) noexcept + { listeners_.remove( listener ); } + + private: + T_String basePath_{ T_String::Pooled( "." ) }; + ebcl::T_Set< A_ProjectPathListener* > listeners_{ + ebcl::UseTag< ebcl::ArrayBacked< 16 > >( ) + }; + + void setBasePathInternal( T_String path ) noexcept; +};