NAH 1.0.6
Native Application Host - Library API Reference
Loading...
Searching...
No Matches
/home/runner/work/nah/nah/include/nah/nahhost.hpp
#include <nah/nahhost.hpp>
auto host = nah::NahHost::create("/nah");
auto contract = host->getLaunchContract("com.example.myapp");
if (contract.isOk()) {
// Use contract.value() to launch the app
}
static std::unique_ptr< NahHost > create(const std::string &root_path)
Create a NahHost instance for a NAH root directory.
Main NAH library interface for contract composition.
#pragma once
#include "nah/types.hpp"
#include "nah/contract.hpp"
#include <memory>
#include <optional>
#include <string>
#include <vector>
namespace nah {
// ============================================================================
// App Info
// ============================================================================
struct AppInfo {
std::string id;
std::string version;
std::string instance_id;
std::string install_root;
std::string record_path;
};
// ============================================================================
// Error Handling (per SPEC L1692-L1738)
// ============================================================================
enum class ErrorCode {
// System / IO
// Contract composition critical errors (normative)
// Profile load failures
};
class Error {
public:
Error(ErrorCode code, std::string message)
: code_(code), message_(std::move(message)) {}
Error& withContext(const std::string& context) {
message_ = context + ": " + message_;
return *this;
}
ErrorCode code() const { return code_; }
const std::string& message() const { return message_; }
std::string toString() const { return message_; }
private:
ErrorCode code_;
std::string message_;
};
// ============================================================================
// Result Type (per SPEC L1692-L1710)
// ============================================================================
template<typename T, typename E = Error>
class Result {
public:
static Result ok(T value) { return Result(std::move(value)); }
static Result err(E error) { return Result(std::move(error)); }
bool isOk() const { return has_value_; }
bool isErr() const { return !has_value_; }
T& value() { return value_.value(); }
const T& value() const { return value_.value(); }
E& error() { return error_.value(); }
const E& error() const { return error_.value(); }
T valueOr(T default_value) const {
if (has_value_) return value_.value();
return default_value;
}
template<typename F>
auto map(F func) -> Result<decltype(func(std::declval<T>())), E> {
if (has_value_) {
return Result<decltype(func(std::declval<T>())), E>::ok(func(value_.value()));
}
return Result<decltype(func(std::declval<T>())), E>::err(error_.value());
}
template<typename F>
auto flatMap(F func) -> decltype(func(std::declval<T>())) {
if (has_value_) {
return func(value_.value());
}
return decltype(func(std::declval<T>()))::err(error_.value());
}
private:
explicit Result(T value) : has_value_(true), value_(std::move(value)) {}
explicit Result(E error) : has_value_(false), error_(std::move(error)) {}
bool has_value_;
std::optional<T> value_;
std::optional<E> error_;
};
template<typename E>
class Result<void, E> {
public:
static Result ok() { return Result(true, std::nullopt); }
static Result err(E error) { return Result(false, std::move(error)); }
bool isOk() const { return has_value_; }
bool isErr() const { return !has_value_; }
void value() const {}
E& error() { return error_.value(); }
const E& error() const { return error_.value(); }
private:
Result(bool hv, std::optional<E> err) : has_value_(hv), error_(std::move(err)) {}
bool has_value_;
std::optional<E> error_;
};
// ============================================================================
// NahHost Class (per SPEC L1660-L1684)
// ============================================================================
class NahHost {
public:
static std::unique_ptr<NahHost> create(const std::string& root_path);
const std::string& root() const { return root_; }
std::vector<AppInfo> listApplications() const;
Result<AppInfo> findApplication(const std::string& id,
const std::string& version = "") const;
Result<HostProfile> getActiveHostProfile() const;
Result<void> setActiveHostProfile(const std::string& name);
std::vector<std::string> listProfiles() const;
Result<HostProfile> loadProfile(const std::string& name) const;
Result<void> validateProfile(const HostProfile& profile) const;
Result<ContractEnvelope> getLaunchContract(
const std::string& app_id,
const std::string& version = "",
const std::string& profile = "",
bool enable_trace = false) const;
Result<ContractEnvelope> composeContract(
const CompositionInputs& inputs) const;
private:
explicit NahHost(std::string root) : root_(std::move(root)) {}
Result<HostProfile> resolveActiveProfile(const std::string& explicit_name = "") const;
std::string root_;
};
} // namespace nah
std::string toString() const
Definition nahhost.hpp:89
Error & withContext(const std::string &context)
Definition nahhost.hpp:82
const std::string & message() const
Definition nahhost.hpp:88
ErrorCode code() const
Definition nahhost.hpp:87
Result< HostProfile > getActiveHostProfile() const
Get the currently active host profile.
Result< AppInfo > findApplication(const std::string &id, const std::string &version="") const
Find an installed application by ID.
Result< ContractEnvelope > composeContract(const CompositionInputs &inputs) const
Low-level contract composition from explicit inputs.
Result< void > validateProfile(const HostProfile &profile) const
Validate a host profile.
Result< void > setActiveHostProfile(const std::string &name)
Set the active host profile by name.
std::vector< std::string > listProfiles() const
List all available profile names.
Result< HostProfile > loadProfile(const std::string &name) const
Load a specific profile by name.
const std::string & root() const
Get the NAH root path.
Definition nahhost.hpp:210
Result< ContractEnvelope > getLaunchContract(const std::string &app_id, const std::string &version="", const std::string &profile="", bool enable_trace=false) const
Generate a launch contract for an application.
std::vector< AppInfo > listApplications() const
List all installed applications.
E & error()
Definition nahhost.hpp:119
auto map(F func) -> Result< decltype(func(std::declval< T >())), E >
Definition nahhost.hpp:128
T & value()
Definition nahhost.hpp:117
T valueOr(T default_value) const
Definition nahhost.hpp:122
bool isErr() const
Definition nahhost.hpp:115
static Result ok(T value)
Definition nahhost.hpp:111
static Result err(E error)
Definition nahhost.hpp:112
bool isOk() const
Definition nahhost.hpp:114
auto flatMap(F func) -> decltype(func(std::declval< T >()))
Definition nahhost.hpp:136
ErrorCode
Error codes for NAH operations.
Definition nahhost.hpp:57
std::string version
Definition nahhost.hpp:44
std::string install_root
Definition nahhost.hpp:46
std::string id
Definition nahhost.hpp:43
std::string instance_id
Definition nahhost.hpp:45
std::string record_path
Definition nahhost.hpp:47