1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     fslow_SafePath.h
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 46347 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_FSLOW_FSLOW_SAFEPATH_H_
17 #define NN_FSLOW_FSLOW_SAFEPATH_H_
18 
19 #include <nn/util/util_AutoObject.h>
20 #include <nn/util/util_StaticAssert.h>
21 #include <nn/fnd/fnd_FixedLengthString.h>
22 #include <nn/fnd/fnd_Interlocked.h>
23 #include <memory>
24 
25 namespace nn { namespace fslow {
26 
27 template <typename TChar, size_t TMaxLength, size_t TSize>
28 class ReferenceCountedString :
29     public nn::util::ReferenceCounter<u32>,
30     public nn::fnd::FixedLengthString<TChar, TMaxLength, TSize - sizeof(nn::util::ReferenceCounter<u32>)>
31 {
32 private:
33     typedef nn::fnd::FixedLengthString<TChar, TMaxLength, TSize - sizeof(nn::util::ReferenceCounter<u32>)> Base;
34 public:
ReferenceCountedString()35     ReferenceCountedString() {}
ReferenceCountedString(const TChar * s)36     ReferenceCountedString(const TChar* s) : Base(s) {}
ReferenceCountedString(const T * s)37     template <class T> explicit ReferenceCountedString(const T* s) : Base(s) {}
38 };
39 
40 NN_UTIL_AUTOOBJECT_BEGIN_DEFINE_T1(SafePath, NN_UTIL_AUTOOBJECT_T0)
41 public:
42     using typename Base::Updater;
43 
44     typedef NN_UTIL_AUTOOBJECT_T0 String;
45     typedef typename String::Char Char;
46 
SafePath()47     SafePath() {}
SafePath(const Char * s)48     SafePath(const Char* s) : Base(s) {}
SafePath(const T * s)49     template <class T> explicit SafePath(const T* s) : Base(s) {}
50 
GetLength()51     size_t GetLength() const { return (*this)->GetLength(); }
52 
GetDataSize()53     size_t GetDataSize() const { return ((*this)->GetLength() + 1) * sizeof(Char); }
GetDataBuffer()54     const void* GetDataBuffer() const { return &((*this)[0]); }
55 
GetString()56     const Char* GetString() const { return (*this)->GetString(); }
57 
58     operator const Char*() const { return (*this)->GetString(); }
59     const Char& operator[](int i) const { return (**this)[i]; }
60     operator bool() const { return (*this)->IsValid(); }
61     bool operator!() const { return !(*this)->IsValid(); }
62 
EraseHead(size_t size)63     void EraseHead(size_t size)
64     {
65         Updater(*this)->EraseHead(size);
66     }
67 
68     template <size_t MaxSize, Char Separator>
GetArchiveNameLength()69     s32 GetArchiveNameLength() const
70     {
71         const Char* s = *this;
72         for (s32 i = 0; i < MaxSize; ++i)
73         {
74             if (s[i] == Separator)
75             {
76                 return i;
77             }
78         }
79         return -1;
80     }
81 
PRINT()82     void PRINT()
83     {
84         nn::dbg::detail::Printf("SafePath::PRINT \"%ls\"\n", (*this)->GetString());
85     }
86 
87 NN_UTIL_AUTOOBJECT_END_DEFINE
88 
89 }}
90 
91 #endif
92